Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion switcher_client/client.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand All @@ -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)
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
15 changes: 15 additions & 0 deletions switcher_client/lib/globals/global_snapshot.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions switcher_client/lib/snapshot.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 19 additions & 1 deletion switcher_client/lib/types.py
Original file line number Diff line number Diff line change
@@ -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
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


3 changes: 3 additions & 0 deletions switcher_client/lib/utils.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion tests/test_client_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,23 @@ def test_context_with_optionals():
options = Client.context.options

assert options.local == True
assert options.snapshot_location == './snapshots'
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()
Loading