refactor(config): abstract config storage and enable dependency injection
Abstract the configuration storage mechanism to allow using custom configurations, such as in-memory setups, when using the tool as a library. This aligns the configuration architecture with the session cache abstraction. - Define ConfigInterface protocol and InMemoryConfig class - Make CacheFile, InMemoryCache, ConfigFile, and InMemoryConfig explicitly inherit their interfaces - Update MdrsService and MdrsClient to accept customizable config_class and config instances - Add validation to check remote parameter consistency in create_connection - Remove unused imports across command files
This commit is contained in:
+13
-4
@@ -5,7 +5,7 @@ from unicodedata import normalize
|
||||
|
||||
from mdrsclient.api import DoiApi, FilesApi, FoldersApi, LaboratoriesApi, UsersApi
|
||||
from mdrsclient.cache import CacheInterface
|
||||
from mdrsclient.config import ConfigFile
|
||||
from mdrsclient.config import ConfigFile, ConfigInterface
|
||||
from mdrsclient.connection import MDRSConnection
|
||||
from mdrsclient.exceptions import (
|
||||
IllegalArgumentException,
|
||||
@@ -18,12 +18,21 @@ from mdrsclient.utils import page_num_from_url
|
||||
|
||||
|
||||
class MdrsService:
|
||||
def __init__(self, connection: MDRSConnection):
|
||||
config_class: type[ConfigInterface] = ConfigFile
|
||||
|
||||
def __init__(self, connection: MDRSConnection, config_class: type[ConfigInterface] | None = None):
|
||||
self.connection = connection
|
||||
if config_class is not None:
|
||||
self.config_class = config_class
|
||||
|
||||
@classmethod
|
||||
def create_connection(cls, remote: str, cache: CacheInterface | None = None) -> MDRSConnection:
|
||||
config = ConfigFile(remote)
|
||||
def create_connection(
|
||||
cls, remote: str, cache: CacheInterface | None = None, config: ConfigInterface | None = None
|
||||
) -> MDRSConnection:
|
||||
if config is None:
|
||||
config = ConfigFile(remote)
|
||||
elif config.remote != remote:
|
||||
raise IllegalArgumentException("Remote host parameter mismatch.")
|
||||
if config.url is None:
|
||||
raise MissingConfigurationException(f"Remote host `{remote}` is not found.")
|
||||
return MDRSConnection(config.remote, config.url, cache=cache)
|
||||
|
||||
Reference in New Issue
Block a user