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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"python.testing.pytestArgs": [
"tests", "-s"
"tests", "-s", "-v"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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**
Expand Down Expand Up @@ -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
```

Expand Down
4 changes: 2 additions & 2 deletions switcher_client/lib/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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}')

@staticmethod
def do_post(url, data, headers) -> requests.Response:
""" Perform a POST request """
print(data)
return requests.post(url, json=data, headers=headers)

@staticmethod
Expand Down
5 changes: 3 additions & 2 deletions switcher_client/lib/types.py
Original file line number Diff line number Diff line change
@@ -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
self.reason = reason
self.metadata = metadata
17 changes: 13 additions & 4 deletions switcher_client/switcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 """
Expand Down Expand Up @@ -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
return response_criteria
24 changes: 24 additions & 0 deletions tests/test_switcher_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down