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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ coverage.xml
*,cover
cover
.pytest_cache
.env

# PyBuilder
target/
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
6 changes: 5 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -61,35 +61,35 @@ 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',
url='https://api.switcherapi.com',
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.<br>

Expand Down
2 changes: 2 additions & 0 deletions switcher_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .client import Client
from .switcher import Switcher
from .lib.globals.global_context import ContextOptions

__all__ = [
'Client',
'Switcher',
'ContextOptions',
]
4 changes: 2 additions & 2 deletions switcher_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
5 changes: 1 addition & 4 deletions switcher_client/switcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return Remote.check_criteria(token, self._context, self)
Empty file added tests/playground/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions tests/playground/index.py
Original file line number Diff line number Diff line change
@@ -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...")
14 changes: 14 additions & 0 deletions tests/playground/util.py
Original file line number Diff line number Diff line change
@@ -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)
Loading