diff --git a/.gitignore b/.gitignore index 36675a0..3b3052a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ coverage.xml *,cover cover .pytest_cache +.env # PyBuilder target/ \ No newline at end of file diff --git a/Pipfile b/Pipfile index 1d32a66..709c50e 100644 --- a/Pipfile +++ b/Pipfile @@ -13,3 +13,4 @@ urllib3 = ">=2.5.0" pytest = "==8.4.1" pytest-cov = "==6.2.1" responses = "==0.25.8" +switcher-client = {file = ".", editable = true} diff --git a/Pipfile.lock b/Pipfile.lock index e564c43..2871bb7 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "d0e7b44cbef4f6903ff6d8344b65d073b62bbddf3dc1efc1c346ad7eebeb5bba" + "sha256": "e448721c9d47614d23dde79507e3619cad88076113f423692c0420a34b165677" }, "pipfile-spec": 6, "requires": {}, @@ -468,6 +468,10 @@ "markers": "python_version >= '3.8'", "version": "==0.25.8" }, + "switcher-client": { + "editable": true, + "file": "." + }, "urllib3": { "hashes": [ "sha256:3fc47733c7e419d4bc3f6b3dc2b4f890bb743906a30d56ba4a5bfa4bbff92760", diff --git a/README.md b/README.md index de4aad0..2db37bd 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ https://github.com/switcherapi/switcher-api The context properties stores all information regarding connectivity. ```python -from switcher.client import Client +from switcher_client import Client Client.build_context( domain='My Domain', @@ -61,7 +61,7 @@ switcher = Client.get_switcher() You can also activate features such as local and silent mode: ```python -from switcher.client import Client +from switcher_client import Client, ContextOptions Client.build_context( domain='My Domain', @@ -69,27 +69,27 @@ Client.build_context( api_key='[API_KEY]', component='MyApp', environment='default', - options={ - 'local': True, - 'logger': True, - 'snapshotLocation': './snapshot/', - 'snapshotAutoUpdateInterval': 3, - 'silentMode': '5m', - 'certPath': './certs/ca.pem' - }) + options=ContextOptions( + local: True, + logger: True, + snapshot_location: './snapshot/', + snapshot_auto_update_interval: 3, + silent_mode: '5m', + cert_path: './certs/ca.pem' + )) switcher = Client.get_switcher() ``` - **local**: If activated, the client will only fetch the configuration inside your snapshot file. The default value is 'false' - **logger**: If activated, it is possible to retrieve the last results from a given Switcher key using Client.getLogger('KEY') -- **snapshotLocation**: Location of snapshot files. The default value is './snapshot/' -- **snapshotAutoUpdateInterval**: Enable Snapshot Auto Update given an interval in seconds (default: 0 disabled). -- **silentMode**: Enable contigency given the time for the client to retry - e.g. 5s (s: seconds - m: minutes - h: hours) -- **regexSafe**: Enable REGEX Safe mode - Prevent agaist reDOS attack (default: true). -- **regexMaxBlackList**: Number of entries cached when REGEX Strategy fails to perform (reDOS safe) - default: 50 -- **regexMaxTimeLimit**: Time limit (ms) used by REGEX workers (reDOS safe) - default - 3000ms -- **certPath**: Path to the certificate file used to establish a secure connection with the API. +- **snapshot_location**: Location of snapshot files. The default value is './snapshot/' +- **snapshot_auto_update_interval**: Enable Snapshot Auto Update given an interval in seconds (default: 0 disabled). +- **silent_mode**: Enable contigency given the time for the client to retry - e.g. 5s (s: seconds - m: minutes - h: hours) +- **regex_safe**: Enable REGEX Safe mode - Prevent agaist reDOS attack (default: true). +- **regex_max_black_list**: Number of entries cached when REGEX Strategy fails to perform (reDOS safe) - default: 50 +- **regex_max_time_limit**: Time limit (ms) used by REGEX workers (reDOS safe) - default - 3000ms +- **cert_path**: Path to the certificate file used to establish a secure connection with the API. (*) regexSafe is a feature that prevents your application from being exposed to a reDOS attack. It is recommended to keep this feature enabled.
diff --git a/switcher_client/__init__.py b/switcher_client/__init__.py index 2cf7436..7e33312 100644 --- a/switcher_client/__init__.py +++ b/switcher_client/__init__.py @@ -1,7 +1,9 @@ from .client import Client +from .switcher import Switcher from .lib.globals.global_context import ContextOptions __all__ = [ 'Client', + 'Switcher', 'ContextOptions', ] \ No newline at end of file diff --git a/switcher_client/client.py b/switcher_client/client.py index 2d331c9..e66dce8 100644 --- a/switcher_client/client.py +++ b/switcher_client/client.py @@ -71,7 +71,7 @@ def load_snapshot(options: Optional[LoadSnapshotOptions] = None) -> int: get(Client.context.environment, DEFAULT_ENVIRONMENT) )) - if Client._is_check_snapshot_available(snapshot_options.fetch_remote): + if Client.__is_check_snapshot_available(snapshot_options.fetch_remote): Client.check_snapshot() return Client.snapshot_version() @@ -107,5 +107,5 @@ def snapshot_version() -> int: return snapshot.data.domain.version @staticmethod - def _is_check_snapshot_available(fetch_remote = False) -> bool: + def __is_check_snapshot_available(fetch_remote = False) -> bool: return Client.snapshot_version() == 0 and (fetch_remote or not Client.context.options.local) \ No newline at end of file diff --git a/switcher_client/switcher.py b/switcher_client/switcher.py index 98eb34e..703c68f 100644 --- a/switcher_client/switcher.py +++ b/switcher_client/switcher.py @@ -67,7 +67,4 @@ def __execute_api_checks(self): def __execute_remote_criteria(self): """ Execute remote criteria """ token = GlobalAuth.get_token() - GlobalAuth.get_exp() - - response_criteria = Remote.check_criteria(token, self._context, self) - return response_criteria \ No newline at end of file + return Remote.check_criteria(token, self._context, self) \ No newline at end of file diff --git a/tests/playground/__init__.py b/tests/playground/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/playground/index.py b/tests/playground/index.py new file mode 100644 index 0000000..249fd5c --- /dev/null +++ b/tests/playground/index.py @@ -0,0 +1,36 @@ +import threading +import time + +from util import monitor_run +from switcher_client import Client, ContextOptions + +SWITCHER_KEY = 'CLIENT_PYTHON_FEATURE' + +def setup_context(options: ContextOptions = ContextOptions()): + Client.build_context( + domain='Switcher API', + url='https://api.switcherapi.com', + api_key='[API_KEY]', + component='switcher-client-python', + environment='default', + options=options + ) + +def simple_api_call(): + """ Use case: Check Switcher using remote API """ + setup_context(ContextOptions( + local=False + )) + + switcher = Client.get_switcher(SWITCHER_KEY) + + monitor_thread = threading.Thread(target=monitor_run, args=(switcher,), daemon=True) + monitor_thread.start() + +try: + # Replace with use case + simple_api_call() + while True: + time.sleep(1) +except KeyboardInterrupt: + print("\nStopping...") \ No newline at end of file diff --git a/tests/playground/util.py b/tests/playground/util.py new file mode 100644 index 0000000..d32ca2d --- /dev/null +++ b/tests/playground/util.py @@ -0,0 +1,14 @@ +import json +import time + +from switcher_client import Switcher + +def monitor_run(switcher: Switcher): + while True: + start_time = time.time() * 1000 + result = switcher.is_on() + end_time = time.time() * 1000 + + elapsed_time = int(end_time - start_time) + print(f"- {elapsed_time} ms - {json.dumps(result)}") + time.sleep(1.0) \ No newline at end of file