diff --git a/.vscode/settings.json b/.vscode/settings.json index 1bdc1a0..c242809 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "python.testing.pytestArgs": [ - "tests", "-s" + "tests", "-s", "-v" ], "python.testing.unittestEnabled": false, "python.testing.pytestEnabled": true diff --git a/README.md b/README.md index 59df495..de4aad0 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,10 @@ Invoking the API can be done by instantiating the switcher and calling *is_on* p switcher = Client.get_switcher() switcher.is_on('FEATURE01') # or -result, reason, metadata = switcher.detail().is_on('FEATURE01') +response = switcher.is_on_with_details('FEATURE01') +print(response.result) # True or False +print(response.reason) # Reason for the result +print(response.metadata) # Additional metadata if available ``` 2. **Strategy validation - preparing input** @@ -120,7 +123,7 @@ switcher.is_on() All-in-one method is fast and include everything you need to execute a complex call to the API. ```python -result, reason, metadata = switcher.detail().check_value('User 1').check_network('192.168.0.1').is_on('FEATURE01') +switcher.check_value('User 1').check_network('192.168.0.1').is_on('FEATURE01') ``` 4. **Throttle** @@ -156,7 +159,7 @@ Client.forget('FEATURE01') switcher.is_on('FEATURE01') # Now, it's going to return the result retrieved from the API or the Snaopshot file Client.assume('FEATURE01').false().with_metadata({ 'message': 'Feature is disabled' }) # Include metadata to emulate Relay response -response = switcher.detail().is_on('FEATURE01') # False +response = switcher.is_on_with_details('FEATURE01') # False print(response.metadata['message']) # Feature is disabled ``` diff --git a/switcher_client/lib/remote.py b/switcher_client/lib/remote.py index 25cd563..2e9cdec 100644 --- a/switcher_client/lib/remote.py +++ b/switcher_client/lib/remote.py @@ -41,7 +41,8 @@ def check_criteria( json_response = response.json() return ResultDetail( result=json_response['result'], - reason=json_response.get('reason', None) + reason=json_response.get('reason', None), + metadata=json_response.get('metadata', {}) ) raise RemoteCriteriaError(f'[check_criteria] failed with status: {response.status_code}') @@ -49,7 +50,6 @@ def check_criteria( @staticmethod def do_post(url, data, headers) -> requests.Response: """ Perform a POST request """ - print(data) return requests.post(url, json=data, headers=headers) @staticmethod diff --git a/switcher_client/lib/types.py b/switcher_client/lib/types.py index c957462..29c8a36 100644 --- a/switcher_client/lib/types.py +++ b/switcher_client/lib/types.py @@ -1,4 +1,5 @@ class ResultDetail: - def __init__(self, result: bool, reason: str): + def __init__(self, result: bool, reason: str, metadata: dict): self.result = result - self.reason = reason \ No newline at end of file + self.reason = reason + self.metadata = metadata \ No newline at end of file diff --git a/switcher_client/switcher.py b/switcher_client/switcher.py index 5922709..ea3d9f2 100644 --- a/switcher_client/switcher.py +++ b/switcher_client/switcher.py @@ -4,6 +4,7 @@ from switcher_client.lib.remote_auth import RemoteAuth from switcher_client.lib.globals import GlobalAuth from switcher_client.lib.remote import Remote +from switcher_client.lib.types import ResultDetail from switcher_client.switcher_data import SwitcherData class Switcher(SwitcherData): @@ -13,15 +14,23 @@ def __init__(self, context: Context, key: Optional[str] = None): def is_on(self, key: Optional[str] = None) -> bool: """ Execute criteria """ + self._show_details = False + self.__validate_args(key) + return self.__submit().result + + def is_on_with_details(self, key: Optional[str] = None) -> ResultDetail: + """ Execute criteria with details """ + self._show_details = True self.__validate_args(key) return self.__submit() - def __submit(self) -> bool: + def __submit(self) -> ResultDetail: """ Submit criteria for execution (local or remote) """ - self.__validate() self.__execute_api_checks() - return self.__execute_remote_criteria() + response = self.__execute_remote_criteria() + + return response def __validate(self) -> 'Switcher': """ Validates client settings for remote API calls """ @@ -49,4 +58,4 @@ def __execute_remote_criteria(self): GlobalAuth.get_exp() response_criteria = Remote.check_criteria(token, self._context, self) - return response_criteria.result \ No newline at end of file + return response_criteria \ No newline at end of file diff --git a/tests/test_switcher_remote.py b/tests/test_switcher_remote.py index 235e9e6..9031fd0 100644 --- a/tests/test_switcher_remote.py +++ b/tests/test_switcher_remote.py @@ -41,6 +41,30 @@ def test_remote_with_input(): assert switcher \ .check_value('user_id') \ .is_on('MY_SWITCHER') + +@responses.activate +def test_remote_with_details(): + """ Should call the remote API with success using detailed response """ + + # given + given_auth() + given_check_criteria( + response={ + 'result': True, + 'reason': 'Success', + 'metadata': {'key': 'value'} + }, + show_details=True) + given_context() + + switcher = Client.get_switcher() + + # test + response = switcher.is_on_with_details('MY_SWITCHER') + assert response + assert response.reason == 'Success' + assert response.result is True + assert response.metadata == {'key': 'value'} def test_remote_err_no_key(): """ Should raise an exception when no key is provided """