Skip to content

Commit

Permalink
feat(cache): request tokens with cache config (#91)
Browse files Browse the repository at this point in the history
Co-authored-by: Fran García Salomón <[email protected]>
  • Loading branch information
acostapazo and fgsalomon authored Oct 19, 2023
1 parent f947627 commit a110b66
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
10 changes: 8 additions & 2 deletions alice/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def from_config(config: Config) -> "Auth":
url=config.onboarding_url, # type: ignore
timeout=config.timeout,
verbose=config.verbose,
use_cache=config.use_cache,
)

def __init__(
Expand All @@ -34,10 +35,15 @@ def __init__(
session: Session,
url: str = DEFAULT_URL,
timeout: Union[float, None] = None,
verbose: Optional[bool] = False,
verbose: bool = False,
use_cache: bool = False,
):
self._auth_client = AuthClient(
url=url, api_key=api_key, session=session, timeout=timeout
url=url,
api_key=api_key,
session=session,
timeout=timeout,
use_cache=use_cache,
)
self.url = url
self.verbose = verbose
Expand Down
10 changes: 10 additions & 0 deletions alice/auth/auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(
api_key: str,
session: Session,
timeout: Union[float, None] = None,
use_cache: bool = False,
):
self.url = url
self._api_key = api_key
Expand All @@ -38,6 +39,7 @@ def __init__(
self._cached_user_token_stack = CachedTokenStack()
self.session = session
self.timeout = timeout
self.use_cache = use_cache

@timeit
def create_user_token(
Expand All @@ -56,6 +58,8 @@ def create_user_token(

url = f"{self.url}/user_token/{user_id}"
headers = {"Authorization": f"Bearer {login_token}"}
if self.use_cache:
headers["Cache-Control"] = "use-cache"
try:
response = self.session.get(url, headers=headers, timeout=self.timeout)
if response.status_code == 200:
Expand Down Expand Up @@ -92,6 +96,8 @@ def _create_backend_token(self, verbose: Optional[bool] = False) -> Response:

url = f"{self.url}/backend_token"
headers = {"Authorization": f"Bearer {login_token}"}
if self.use_cache:
headers["Cache-Control"] = "use-cache"
try:
response = self.session.get(url, headers=headers, timeout=self.timeout)
if response.status_code == 200:
Expand Down Expand Up @@ -124,6 +130,8 @@ def _create_backend_token_with_user_id(

url = f"{self.url}/backend_token/{user_id}"
headers = {"Authorization": f"Bearer {login_token}"}
if self.use_cache:
headers["Cache-Control"] = "use-cache"
try:
response = self.session.get(url, headers=headers, timeout=self.timeout)
if response.status_code == 200:
Expand All @@ -150,6 +158,8 @@ def _get_login_token(self) -> Result[str, Response]:
def _create_login_token(self) -> Response:
final_url = f"{self.url}/login_token"
headers = {"apikey": self._api_key}
if self.use_cache:
headers["Cache-Control"] = "use-cache"
try:
response = self.session.get(
final_url, headers=headers, timeout=self.timeout
Expand Down
4 changes: 4 additions & 0 deletions alice/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Config(BaseSettings):
sandbox_token: Union[str, None] = Field(
default=None, description="This token is only used for trials"
)
use_cache: bool = Field(
default=True,
description="This optional feature allows users to configure specific caches during service invocation, optimizing the performance of the application.",
)

@model_validator(mode="after")
def validate_urls(self) -> "Config":
Expand Down
10 changes: 5 additions & 5 deletions examples/onboarding_with_screening.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def screening_onboarding(api_key: str, verbose: Optional[bool] = False) -> None:
assert isinstance(detailed_screening, dict)

# Add user to monitoring list
onboarding.screening_monitor_add(user_id=user_id).unwrap_or_raise()

onboarding.screening_monitor_delete(
user_id=user_id, verbose=verbose
).unwrap_or_raise()
# onboarding.screening_monitor_add(user_id=user_id).unwrap_or_raise()
#
# onboarding.screening_monitor_delete(
# user_id=user_id, verbose=verbose
# ).unwrap_or_raise()


def given_any_document_front_media_data() -> bytes:
Expand Down
28 changes: 12 additions & 16 deletions tests/test_integration_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,33 @@ def do_complete_flow() -> Result[bool, Error]:
config = Config(api_key=given_valid_api_key)
onboarding = Onboarding.from_config(config)

user_id = onboarding.create_user(
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=False,
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,
steps=[OnboardingSteps.SELFIE, OnboardingSteps.IDCARD],
default=True,
name="alice-flow-test-onboarding-python-updated",
).unwrap_or_return()
_ = onboarding.retrieve_flows().unwrap_or_return()

_ = onboarding.update_user_flow(
user_id = onboarding.create_user(
user_info=UserInfo(first_name="Alice", last_name="Biometrics"),
flow_id=flow_id,
user_id=user_id,
device_info=DeviceInfo(device_platform="Android"),
).unwrap_or_return()

_ = onboarding.get_user_flow(
user_id=user_id,
).unwrap_or_return()

_ = onboarding.update_flow(
flow_id=flow_id,
steps=[OnboardingSteps.SELFIE, OnboardingSteps.IDCARD],
default=False,
name="alice-flow-test-onboarding-python-updated",
).unwrap_or_return()

_ = onboarding.retrieve_flows().unwrap_or_return()

onboarding.delete_flow(flow_id).unwrap_or_return()
onboarding.delete_user(user_id).unwrap_or_return()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_integration_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_should_execute_all_webhook_lifecycle(given_valid_api_key):
webhook_id = webhooks_client.create_webhook(webhook=webhook).unwrap_or_return()

# Update an existent Webhook
new_post_url = "http://alicebiometrics.com"
new_post_url = "http://alicebiometrics.com/"
webhook_to_update = Webhook(
webhook_id=webhook_id, # Needed if we want to update
active=False,
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_should_execute_all_webhook_lifecycle(given_valid_api_key):
# Retrieve an existent Webhook
retrieved_webhook = webhooks_client.get_webhook(webhook_id).unwrap()
assert retrieved_webhook.active
assert retrieved_webhook.post_url == "http://alicebiometrics.com"
assert retrieved_webhook.post_url == new_post_url

# Retrieve all configured webhooks
result = webhooks_client.get_webhooks()
Expand Down

0 comments on commit a110b66

Please sign in to comment.