Skip to content

Commit

Permalink
feat(flows): retrieve default flow if flow_id is not passed (#76)
Browse files Browse the repository at this point in the history
* feat(flows): retrieve default flow if flow_id is not passed

* fix(tests): retrieve default flow after creation
  • Loading branch information
acostapazo committed Aug 17, 2023
1 parent 8ca0ae4 commit 337decb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
4 changes: 2 additions & 2 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,15 +1720,15 @@ 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]:
"""
Retrieves a 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
Expand Down
10 changes: 7 additions & 3 deletions alice/onboarding/onboarding_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand All @@ -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
Expand All @@ -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,
)
Expand Down
32 changes: 32 additions & 0 deletions examples/onboarding_with_flows.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 2 additions & 4 deletions examples/onboarding_with_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/test_integration_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 337decb

Please sign in to comment.