From 337decb576f0296004aeadc3dd1dbd22c89d90c4 Mon Sep 17 00:00:00 2001 From: acostapazo Date: Thu, 17 Aug 2023 16:51:40 +0200 Subject: [PATCH] feat(flows): retrieve default flow if flow_id is not passed (#76) * feat(flows): retrieve default flow if flow_id is not passed * fix(tests): retrieve default flow after creation --- alice/onboarding/onboarding.py | 4 ++-- alice/onboarding/onboarding_client.py | 10 ++++++--- examples/onboarding_with_flows.py | 32 +++++++++++++++++++++++++++ examples/onboarding_with_webhooks.py | 6 ++--- tests/test_integration_flows.py | 3 +++ 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 examples/onboarding_with_flows.py diff --git a/alice/onboarding/onboarding.py b/alice/onboarding/onboarding.py index 71c6557..1bd1be1 100644 --- a/alice/onboarding/onboarding.py +++ b/alice/onboarding/onboarding.py @@ -1720,7 +1720,7 @@ def update_user_state( @early_return def retrieve_flow( self, - flow_id: str, + flow_id: Union[str, None] = None, verbose: bool = False, ) -> Result[List[Dict[str, Any]], OnboardingError]: """ @@ -1728,7 +1728,7 @@ def retrieve_flow( Parameters ---------- flow_id - Flow identifier + Flow identifier (if none return default flow) verbose Used for print service response as well as the time elapsed Returns diff --git a/alice/onboarding/onboarding_client.py b/alice/onboarding/onboarding_client.py index ae1e71b..526856b 100644 --- a/alice/onboarding/onboarding_client.py +++ b/alice/onboarding/onboarding_client.py @@ -1973,7 +1973,7 @@ def update_user_state( @early_return @timeit def retrieve_flow( - self, flow_id: str, verbose: bool = False + self, flow_id: Union[str, None] = None, verbose: bool = False ) -> Result[Response, Error]: """ @@ -1982,7 +1982,7 @@ def retrieve_flow( Parameters ---------- flow_id - Flow identifier + Flow identifier (if none return default flow) verbose Used for print service response as well as the time elapsed Returns @@ -1996,9 +1996,13 @@ def retrieve_flow( headers = self._auth_headers(backend_token) + url = f"{self.url}/flow" + if flow_id: + url = f"{self.url}/flow?flow_id={flow_id}" + try: response = requests.get( - f"{self.url}/flow?flow_id={flow_id}", + url, headers=headers, timeout=self.timeout, ) diff --git a/examples/onboarding_with_flows.py b/examples/onboarding_with_flows.py new file mode 100644 index 0000000..a0a2557 --- /dev/null +++ b/examples/onboarding_with_flows.py @@ -0,0 +1,32 @@ +import os +from typing import Optional + +from alice import Config, Onboarding + +RESOURCES_PATH = f"{os.path.dirname(os.path.abspath(__file__))}/../resources" + + +def onboarding_configure_flows(api_key: str, verbose: Optional[bool] = False) -> None: + config = Config(api_key=api_key, verbose=verbose) + onboarding = Onboarding.from_config(config) + + flows = onboarding.retrieve_flows().unwrap_or_raise() + print(f"{flows=}") + + flow_id = flows[0].get("id") + + flow = onboarding.retrieve_flow(flow_id).unwrap_or_raise() + print(f"{flow=}") + + default_flow = onboarding.retrieve_flow().unwrap_or_raise() + print(f"{default_flow=}") + + +if __name__ == "__main__": + api_key = os.environ.get("ONBOARDING_API_KEY") + if api_key is None: + raise AssertionError( + "Please configure your ONBOARDING_API_KEY to run the example" + ) + print("Running configure flows example...") + onboarding_configure_flows(api_key=api_key, verbose=True) diff --git a/examples/onboarding_with_webhooks.py b/examples/onboarding_with_webhooks.py index d40b299..5f8f7d0 100644 --- a/examples/onboarding_with_webhooks.py +++ b/examples/onboarding_with_webhooks.py @@ -11,7 +11,6 @@ def configure_webhooks(api_key: str, verbose: Optional[bool] = False) -> None: config = Config(api_key=api_key, verbose=verbose) - config.onboarding_url = "https://apis.staging.alicebiometrics.com/onboarding" webhooks_client = Webhooks.from_config(config) # Check Available events @@ -35,9 +34,9 @@ def configure_webhooks(api_key: str, verbose: Optional[bool] = False) -> None: active=False, post_url="http://alicebiometrics.com", api_key="b0b905d6-228f-44bf-a130-c85d7aecd765", - event_name="user.created", + event_name="user_created", event_version="1", - algorithm="sha512", + algorithm="sha256", secret=str(secrets.token_hex(20)), ) webhooks_client.update_webhook(webhook_to_update).unwrap_or_raise() @@ -53,7 +52,6 @@ def configure_webhooks(api_key: str, verbose: Optional[bool] = False) -> None: # Retrieve an existent Webhook retrieved_webhook = webhooks_client.get_webhook(webhook_id).unwrap_or_raise() assert retrieved_webhook.active - breakpoint() assert retrieved_webhook.post_url == "http://alicebiometrics.com" # Retrieve all configured webhooks diff --git a/tests/test_integration_flows.py b/tests/test_integration_flows.py index 0575d91..5e22cea 100644 --- a/tests/test_integration_flows.py +++ b/tests/test_integration_flows.py @@ -46,12 +46,15 @@ def do_complete_flow() -> Result[bool, Error]: user_info=UserInfo(first_name="Alice", last_name="Biometrics"), device_info=DeviceInfo(device_platform="Android"), ).unwrap_or_return() + flow_id = onboarding.create_flow( steps=[OnboardingSteps.SELFIE], default=True, name="alice-flow-test-onboarding-python", ).unwrap_or_return() + _ = onboarding.retrieve_flow().unwrap_or_return() + _ = onboarding.retrieve_flow(flow_id=flow_id).unwrap_or_return() _ = onboarding.update_flow( flow_id=flow_id,