diff --git a/switcher_client/client.py b/switcher_client/client.py index 9c113aa..0e9e55e 100644 --- a/switcher_client/client.py +++ b/switcher_client/client.py @@ -1,8 +1,11 @@ from typing import Optional +from switcher_client.lib.globals.global_snapshot import GlobalSnapshot from switcher_client.lib.remote_auth import RemoteAuth from switcher_client.lib.globals.global_context import Context, ContextOptions from switcher_client.lib.globals.global_context import DEFAULT_ENVIRONMENT +from switcher_client.lib.snapshot import load_domain +from switcher_client.lib.utils import get from switcher_client.switcher import Switcher class Client: @@ -28,6 +31,11 @@ def build_context( """ Client.context = Context(domain, url, api_key, component, environment, options) + + # Default values + GlobalSnapshot.clear() + + # Initialize Auth RemoteAuth.init(Client.context) @staticmethod @@ -50,4 +58,26 @@ def verify_context(): def get_switcher(key: Optional[str] = None) -> Switcher: """ Get a switcher by key """ Client.verify_context() - return Switcher(Client.context, key) \ No newline at end of file + return Switcher(Client.context, key) + + + @staticmethod + def load_snapshot() -> int: + """ Load Domain from snapshot """ + + GlobalSnapshot.init(load_domain( + get(Client.context.options.snapshot_location, ''), + get(Client.context.environment, DEFAULT_ENVIRONMENT) + )) + + return Client.snapshot_version() + + @staticmethod + def snapshot_version() -> int: + """ Get the version of the snapshot """ + snapshot = GlobalSnapshot.snapshot() + + if snapshot is None: + return 0 + + return snapshot.data.domain.version \ No newline at end of file diff --git a/switcher_client/lib/globals/global_snapshot.py b/switcher_client/lib/globals/global_snapshot.py new file mode 100644 index 0000000..b808170 --- /dev/null +++ b/switcher_client/lib/globals/global_snapshot.py @@ -0,0 +1,15 @@ +from switcher_client.lib.types import Snapshot + +class GlobalSnapshot: + + @staticmethod + def init(snapshot: Snapshot | None): + GlobalSnapshot.snapshotStore = snapshot + + @staticmethod + def clear(): + GlobalSnapshot.snapshotStore = None + + @staticmethod + def snapshot() -> Snapshot | None: + return GlobalSnapshot.snapshotStore \ No newline at end of file diff --git a/switcher_client/lib/snapshot.py b/switcher_client/lib/snapshot.py new file mode 100644 index 0000000..8b76c89 --- /dev/null +++ b/switcher_client/lib/snapshot.py @@ -0,0 +1,16 @@ +from switcher_client.lib.types import Snapshot, SnapshotData, Domain + + +def load_domain(snpahsot_location: str, environment: str): + """ Load Domain from snapshot file """ + + snapshot_file = f"{snpahsot_location}/{environment}.json" + + print(f"Loading snapshot from {snapshot_file}") + snapshot_data = SnapshotData() + snapshot_data.domain = Domain() + snapshot_data.domain.version = 1 + + snapshot = Snapshot() + snapshot.data = snapshot_data + return snapshot \ No newline at end of file diff --git a/switcher_client/lib/types.py b/switcher_client/lib/types.py index 29c8a36..cb5679d 100644 --- a/switcher_client/lib/types.py +++ b/switcher_client/lib/types.py @@ -1,5 +1,23 @@ +from typing import Optional, List + class ResultDetail: def __init__(self, result: bool, reason: str, metadata: dict): self.result = result self.reason = reason - self.metadata = metadata \ No newline at end of file + self.metadata = metadata + +class Snapshot: + def __init__(self): + self.data: SnapshotData + +class SnapshotData: + def __init__(self): + self.domain: Domain + +class Domain: + def __init__(self): + self.name: Optional[str] = None + self.version: int = 0 + self.activated: Optional[bool] = None + + diff --git a/switcher_client/lib/utils.py b/switcher_client/lib/utils.py new file mode 100644 index 0000000..ffa8072 --- /dev/null +++ b/switcher_client/lib/utils.py @@ -0,0 +1,3 @@ +def get(value, default_value): + """ Return value if not None, otherwise return default_value """ + return value if value is not None else default_value diff --git a/tests/test_client_context.py b/tests/test_client_context.py index ff386f2..b477b4d 100644 --- a/tests/test_client_context.py +++ b/tests/test_client_context.py @@ -51,4 +51,23 @@ def test_context_with_optionals(): options = Client.context.options assert options.local == True - assert options.snapshot_location == './snapshots' \ No newline at end of file + assert options.snapshot_location == './snapshots' + +def test_load_from_snapshot(): + """ Should load Domain from snapshot file """ + + Client.build_context( + domain='My Domain', + options=ContextOptions( + local=True, + snapshot_location='./snapshots' + ) + ) + + # verify initial snapshot version + assert Client.snapshot_version() == 0 + + # test + version = Client.load_snapshot() + assert Client.snapshot_version() == 1 + assert version == Client.snapshot_version() \ No newline at end of file