From 1bc7b28479db65898b251e9e60e62c7b3fc7b2c8 Mon Sep 17 00:00:00 2001 From: Miguel Lorenzo Date: Mon, 4 Sep 2023 09:15:42 +0200 Subject: [PATCH 1/5] chore(refactor): rename sandbox token to trial token --- alice/config.py | 2 +- alice/sandbox/sandbox.py | 6 +++--- alice/sandbox/sandbox_client.py | 12 ++++++------ examples/sandbox.py | 10 +++++----- tests/conftest.py | 8 ++++---- tests/test_integration_sandbox.py | 6 +++--- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/alice/config.py b/alice/config.py index a3a02b7..a7490bc 100644 --- a/alice/config.py +++ b/alice/config.py @@ -13,7 +13,7 @@ class Config: default="https://apis.alicebiometrics.com/onboarding/sandbox" ) api_key: Union[str, None] = Field(default=None) - sandbox_token: Union[str, None] = Field(default=None) + trial_token: Union[str, None] = Field(default=None) timeout: Union[float, None] = Field( default=None, description="Timeout for every request in seconds", ge=0, le=100 ) diff --git a/alice/sandbox/sandbox.py b/alice/sandbox/sandbox.py index 478d89b..37f2055 100644 --- a/alice/sandbox/sandbox.py +++ b/alice/sandbox/sandbox.py @@ -17,18 +17,18 @@ class Sandbox: @staticmethod def from_config(config: Config) -> "Sandbox": return Sandbox( - sandbox_token=config.sandbox_token, # type: ignore + trial_token=config.trial_token, # type: ignore url=config.sandbox_url, verbose=config.verbose, ) def __init__( self, - sandbox_token: str, + trial_token: str, url: str = DEFAULT_URL, verbose: Optional[bool] = False, ) -> None: - self.sandbox_client = SandboxClient(sandbox_token=sandbox_token, url=url) + self.sandbox_client = SandboxClient(trial_token=trial_token, url=url) self.url = url self.verbose = verbose diff --git a/alice/sandbox/sandbox_client.py b/alice/sandbox/sandbox_client.py index 277e5f3..de41198 100644 --- a/alice/sandbox/sandbox_client.py +++ b/alice/sandbox/sandbox_client.py @@ -10,8 +10,8 @@ class SandboxClient: - def __init__(self, sandbox_token: str, url: str = DEFAULT_URL): - self.sandbox_token = sandbox_token + def __init__(self, trial_token: str, url: str = DEFAULT_URL): + self.trial_token = trial_token self.url = url def _auth_headers(self, token: str) -> Dict[str, Any]: @@ -69,7 +69,7 @@ def create_user( """ print_intro("create_user", verbose=verbose) - headers = self._auth_headers(self.sandbox_token) + headers = self._auth_headers(self.trial_token) data = None if user_info: @@ -105,7 +105,7 @@ def delete_user(self, email: str, verbose: Optional[bool] = False) -> Response: """ print_intro("delete_user", verbose=verbose) - headers = self._auth_headers(self.sandbox_token) + headers = self._auth_headers(self.trial_token) response = request("DELETE", self.url + f"/user/{email}", headers=headers) print_response(response=response, verbose=verbose) @@ -132,7 +132,7 @@ def get_user(self, email: str, verbose: Optional[bool] = False) -> Response: """ print_intro("get_user", verbose=verbose) - headers = self._auth_headers(self.sandbox_token) + headers = self._auth_headers(self.trial_token) response = request("GET", self.url + f"/user/{email}", headers=headers) @@ -160,7 +160,7 @@ def get_user_token(self, email: str, verbose: Optional[bool] = False) -> Respons """ print_intro("get_user_token", verbose=verbose) - headers = self._auth_headers(self.sandbox_token) + headers = self._auth_headers(self.trial_token) response = request("GET", self.url + f"/user/token/{email}", headers=headers) diff --git a/examples/sandbox.py b/examples/sandbox.py index 70832f0..d64435e 100644 --- a/examples/sandbox.py +++ b/examples/sandbox.py @@ -10,8 +10,8 @@ @meiga -def sandbox_example(sandbox_token: str, email: str, verbose: Optional[bool] = False): - config = Config(sandbox_token=sandbox_token, verbose=verbose) +def sandbox_example(trial_token: str, email: str, verbose: Optional[bool] = False): + config = Config(trial_token=trial_token, verbose=verbose) sandbox = Sandbox.from_config(config) user_id = sandbox.create_user(user_info=UserInfo(email=email)).unwrap_or_raise() @@ -33,10 +33,10 @@ def random_mail(): if __name__ == "__main__": - sandbox_token = os.environ.get("ONBOARDING_SANDBOX_TOKEN") - if sandbox_token is None: + trial_token = os.environ.get("ONBOARDING_SANDBOX_TOKEN") + if trial_token is None: raise AssertionError( "Please configure your ONBOARDING_SANDBOX_TOKEN to run the example" ) print("Running sandbox example...") - sandbox_example(sandbox_token=sandbox_token, email=random_mail(), verbose=False) + sandbox_example(trial_token=trial_token, email=random_mail(), verbose=False) diff --git a/tests/conftest.py b/tests/conftest.py index 6bcb29c..7477037 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,13 +16,13 @@ def given_valid_api_key(): @pytest.fixture -def given_valid_sandbox_token(): - sandbox_token = os.environ.get("ONBOARDING_SANDBOX_TOKEN") - if sandbox_token is None: +def given_valid_trial_token(): + trial_token = os.environ.get("ONBOARDING_SANDBOX_TOKEN") + if trial_token is None: raise AssertionError( "Please configure your ONBOARDING_SANDBOX_TOKEN to run the tests" ) - return sandbox_token + return trial_token @pytest.fixture diff --git a/tests/test_integration_sandbox.py b/tests/test_integration_sandbox.py index 5bfbd6a..5c5953b 100644 --- a/tests/test_integration_sandbox.py +++ b/tests/test_integration_sandbox.py @@ -5,7 +5,7 @@ @pytest.mark.unit -def test_should_return_an_error_when_the_sandbox_token_is_not_configured(): +def test_should_return_an_error_when_the_trial_token_is_not_configured(): config = Config() sandbox = Sandbox.from_config(config) @@ -17,10 +17,10 @@ def test_should_return_an_error_when_the_sandbox_token_is_not_configured(): @pytest.mark.unit def test_should_create_a_user_and_get_user_token_and_delete_it( - given_valid_sandbox_token, given_any_valid_mail + given_valid_trial_token, given_any_valid_mail ): - config = Config(sandbox_token=given_valid_sandbox_token) + config = Config(trial_token=given_valid_trial_token) sandbox = Sandbox.from_config(config) result_create_user = sandbox.create_user( From 8090fea7636f5d021d592a5a9d544f3faf82e907 Mon Sep 17 00:00:00 2001 From: Miguel Lorenzo Date: Mon, 4 Sep 2023 09:30:49 +0200 Subject: [PATCH 2/5] feat: remove sandbox service --- alice/onboarding/enums/environment.py | 9 ++ alice/public_api.py | 7 +- alice/sandbox/__init__.py | 0 alice/sandbox/sandbox.py | 201 -------------------------- alice/sandbox/sandbox_client.py | 169 ---------------------- alice/sandbox/sandbox_errors.py | 31 ---- examples/sandbox.py | 42 ------ tests/conftest.py | 10 -- tests/test_integration_sandbox.py | 36 ----- 9 files changed, 10 insertions(+), 495 deletions(-) create mode 100644 alice/onboarding/enums/environment.py delete mode 100644 alice/sandbox/__init__.py delete mode 100644 alice/sandbox/sandbox.py delete mode 100644 alice/sandbox/sandbox_client.py delete mode 100644 alice/sandbox/sandbox_errors.py delete mode 100644 examples/sandbox.py delete mode 100644 tests/test_integration_sandbox.py diff --git a/alice/onboarding/enums/environment.py b/alice/onboarding/enums/environment.py new file mode 100644 index 0000000..a32c68b --- /dev/null +++ b/alice/onboarding/enums/environment.py @@ -0,0 +1,9 @@ +from enum import Enum + + +class DocumentType(Enum): + ID_CARD = "idcard" + DRIVER_LICENSE = "driverlicense" + RESIDENCE_PERMIT = "residencepermit" + PASSPORT = "passport" + HEALTH_INSURANCE_CARD = "healthinsurancecard" diff --git a/alice/public_api.py b/alice/public_api.py index 00e7166..bba56ec 100644 --- a/alice/public_api.py +++ b/alice/public_api.py @@ -34,8 +34,6 @@ # Classes from alice.onboarding.onboarding import Onboarding from alice.onboarding.onboarding_client import OnboardingClient -from alice.sandbox.sandbox import Sandbox -from alice.sandbox.sandbox_client import SandboxClient classes = [ "Onboarding", @@ -44,8 +42,6 @@ "DeviceInfo", "Auth", "AuthClient", - "Sandbox", - "SandboxClient", "Config", "Webhooks", "WebhooksClient", @@ -66,8 +62,7 @@ # Errors from alice.onboarding.onboarding_errors import OnboardingError -from alice.sandbox.sandbox_errors import SandboxError -errors = ["OnboardingError", "SandboxError"] +errors = ["OnboardingError"] __all__ = modules + classes + errors diff --git a/alice/sandbox/__init__.py b/alice/sandbox/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/alice/sandbox/sandbox.py b/alice/sandbox/sandbox.py deleted file mode 100644 index 37f2055..0000000 --- a/alice/sandbox/sandbox.py +++ /dev/null @@ -1,201 +0,0 @@ -import time -from typing import Any, Dict, Optional, Union - -import jwt -from meiga import Failure, Result, Success, isSuccess - -from alice.config import Config -from alice.onboarding.models.device_info import DeviceInfo -from alice.onboarding.models.user_info import UserInfo -from alice.sandbox.sandbox_client import SandboxClient -from alice.sandbox.sandbox_errors import SandboxError - -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding/sandbox" - - -class Sandbox: - @staticmethod - def from_config(config: Config) -> "Sandbox": - return Sandbox( - trial_token=config.trial_token, # type: ignore - url=config.sandbox_url, - verbose=config.verbose, - ) - - def __init__( - self, - trial_token: str, - url: str = DEFAULT_URL, - verbose: Optional[bool] = False, - ) -> None: - self.sandbox_client = SandboxClient(trial_token=trial_token, url=url) - self.url = url - self.verbose = verbose - - @staticmethod - def _is_token_valid(token: Union[str, None], margin_seconds: int = 60) -> bool: - if not token: - return False - - decoded_token = jwt.decode(token, verify=False) - return bool(decoded_token["exp"] > time.time() - margin_seconds) - - def healthcheck( - self, verbose: Optional[bool] = False - ) -> Result[bool, SandboxError]: - """ - Runs a healthcheck on the service to see if there are any problems. - - Parameters - ---------- - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Result where if the operation is successful it returns True. - Otherwise, it returns an SandboxError. - """ - verbose = self.verbose or verbose - response = self.sandbox_client.healthcheck(verbose=verbose) - - if response.status_code == 200: - return isSuccess - else: - return Failure( - SandboxError.from_response(operation="healthcheck", response=response) - ) - - def create_user( - self, - user_info: Union[UserInfo, None] = None, - device_info: Union[DeviceInfo, None] = None, - verbose: Optional[bool] = False, - ) -> Result[str, SandboxError]: - """ - - It creates a new User in the Sandbox service. - - Note that in this service, email is a required property - Note this command will execute also Onboarding User Creation. - - Parameters - ---------- - user_info - Object with optional values with info about the User. - device_info - Object with optional values with info about the User's Device. - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Result where if the operation is successful it returns a user_id. - Otherwise, it returns an SandboxError. - """ - verbose = self.verbose or verbose - response = self.sandbox_client.create_user( - user_info=user_info, device_info=device_info, verbose=verbose - ) - - if response.status_code == 200: - return Success(response.json()) - else: - return Failure( - SandboxError.from_response(operation="create_user", response=response) - ) - - def delete_user( - self, email: str, verbose: Optional[bool] = False - ) -> Result[bool, SandboxError]: - """ - - Delete all the information of a Sandbox User. Including the link with Onboarding user. - Note this command will execute also Onboarding User Deletion. - - Parameters - ---------- - email - User's email - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Result where if the operation is successful it returns True. - Otherwise, it returns an SandboxError. - """ - verbose = self.verbose or verbose - response = self.sandbox_client.delete_user(email=email, verbose=verbose) - - if response.status_code == 200: - return isSuccess - else: - return Failure( - SandboxError.from_response(operation="delete_user", response=response) - ) - - def get_user( - self, email: str, verbose: Optional[bool] = False - ) -> Result[Dict[str, Any], SandboxError]: - """ - - Returns User Status of a Sandbox user - - Parameters - ---------- - email - User's email - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Result where if the operation is successful it returns a Dict with the status info. - Otherwise, it returns an SandboxError. - """ - verbose = self.verbose or verbose - response = self.sandbox_client.get_user(email=email, verbose=verbose) - - if response.status_code == 200: - return Success(response.json()) - else: - return Failure( - SandboxError.from_response(operation="get_user", response=response) - ) - - def get_user_token( - self, email: str, verbose: Optional[bool] = False - ) -> Result[str, SandboxError]: - """ - - Returns user token linked to Onboarding User managed by the Sandbox Service - - Parameters - ---------- - email - User's email - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Result where if the operation is successful it returns list of string with already created user_ids. - Otherwise, it returns an SandboxError. - """ - verbose = self.verbose or verbose - response = self.sandbox_client.get_user_token(email=email, verbose=verbose) - - if response.status_code == 200: - return Success(response.json()["user_token"]) - else: - return Failure( - SandboxError.from_response( - operation="get_user_token", response=response - ) - ) diff --git a/alice/sandbox/sandbox_client.py b/alice/sandbox/sandbox_client.py deleted file mode 100644 index de41198..0000000 --- a/alice/sandbox/sandbox_client.py +++ /dev/null @@ -1,169 +0,0 @@ -from typing import Any, Dict, Optional, Union - -from requests import Response, request - -from alice.onboarding.models.device_info import DeviceInfo -from alice.onboarding.models.user_info import UserInfo -from alice.onboarding.tools import print_intro, print_response, timeit - -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding/sandbox" - - -class SandboxClient: - def __init__(self, trial_token: str, url: str = DEFAULT_URL): - self.trial_token = trial_token - self.url = url - - def _auth_headers(self, token: str) -> Dict[str, Any]: - auth_headers = {"Authorization": f"Bearer {token}"} - return auth_headers - - @timeit - def healthcheck(self, verbose: Optional[bool] = False) -> Response: - """ - - Runs a healthcheck on the service to see if there are any problems. - - Parameters - ---------- - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Response object [requests library] - """ - print_intro("healthcheck", verbose=verbose) - - response = request("GET", self.url + "/healthcheck") - - print_response(response=response, verbose=verbose) - - return response - - @timeit - def create_user( - self, - user_info: Union[UserInfo, None] = None, - device_info: Union[DeviceInfo, None] = None, - verbose: Optional[bool] = False, - ) -> Response: - """ - - It creates a new User in the sandbox service. - - Parameters - ---------- - user_info - Object with optional values with info about the User. - device_info - Object with optional values with info about the User's Device. - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Response object [requests library] - """ - print_intro("create_user", verbose=verbose) - - headers = self._auth_headers(self.trial_token) - - data = None - if user_info: - data = data if data is not None else {} - data.update(user_info.dict()) - if device_info: - data = data if data is not None else {} - data.update(device_info.dict()) - - response = request("POST", self.url + "/user", headers=headers, data=data) - - print_response(response=response, verbose=verbose) - - return response - - @timeit - def delete_user(self, email: str, verbose: Optional[bool] = False) -> Response: - """ - - Delete all the information of a user - - Parameters - ---------- - email - User's email - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Response object [requests library] - """ - print_intro("delete_user", verbose=verbose) - - headers = self._auth_headers(self.trial_token) - response = request("DELETE", self.url + f"/user/{email}", headers=headers) - - print_response(response=response, verbose=verbose) - - return response - - @timeit - def get_user(self, email: str, verbose: Optional[bool] = False) -> Response: - """ - - Returns User status to be used as feedback from the onboarding process - - Parameters - ---------- - email - User's email - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Response object [requests library] - """ - print_intro("get_user", verbose=verbose) - - headers = self._auth_headers(self.trial_token) - - response = request("GET", self.url + f"/user/{email}", headers=headers) - - print_response(response=response, verbose=verbose) - - return response - - @timeit - def get_user_token(self, email: str, verbose: Optional[bool] = False) -> Response: - """ - - Returns user token linked to Onboarding User managed by the Sandbox Service - - Parameters - ---------- - email - User's email - verbose - Used for print service response as well as the time elapsed - - - Returns - ------- - A Response object [requests library] - """ - print_intro("get_user_token", verbose=verbose) - - headers = self._auth_headers(self.trial_token) - - response = request("GET", self.url + f"/user/token/{email}", headers=headers) - - print_response(response=response, verbose=verbose) - - return response diff --git a/alice/sandbox/sandbox_errors.py b/alice/sandbox/sandbox_errors.py deleted file mode 100644 index ae9ee69..0000000 --- a/alice/sandbox/sandbox_errors.py +++ /dev/null @@ -1,31 +0,0 @@ -from typing import Any, Dict, Optional - -from meiga import Error -from pydantic.dataclasses import dataclass -from requests import Response - - -@dataclass -class SandboxError(Error): - operation: str - code: int - message: Optional[Dict[str, Any]] = None # type: ignore - - def __init__( - self, operation: str, code: int, message: Optional[Dict[str, Any]] = None - ): - self.operation = operation - self.code = code - self.message = message - - def __repr__(self) -> str: - return f"[SandboxError: [operation: {self.operation} | code: {self.code} | message: {self.message}]]" - - @staticmethod - def from_response(operation: str, response: Response) -> "SandboxError": - code = response.status_code - try: - message = response.json() - except: # noqa E722 - message = {"message": "no content"} - return SandboxError(operation=operation, code=code, message=message) diff --git a/examples/sandbox.py b/examples/sandbox.py deleted file mode 100644 index d64435e..0000000 --- a/examples/sandbox.py +++ /dev/null @@ -1,42 +0,0 @@ -import os -import random -import string -from typing import Optional - -from meiga import isSuccess -from meiga.decorators import meiga - -from alice import Config, Sandbox, UserInfo - - -@meiga -def sandbox_example(trial_token: str, email: str, verbose: Optional[bool] = False): - config = Config(trial_token=trial_token, verbose=verbose) - sandbox = Sandbox.from_config(config) - - user_id = sandbox.create_user(user_info=UserInfo(email=email)).unwrap_or_raise() - user_token = sandbox.get_user_token(email=email).unwrap_or_raise() - user = sandbox.get_user(email=email).unwrap_or_raise() - sandbox.delete_user(email=email).unwrap_or_raise() - - if verbose: - print(f"user_id: {user_id}") - print(f"user_token: {user_token}") - print(f"user: {user}") - - return isSuccess - - -def random_mail(): - user = "".join(random.choice(string.ascii_lowercase) for i in range(10)) - return f"example_{user}@alicebiometrics.com" - - -if __name__ == "__main__": - trial_token = os.environ.get("ONBOARDING_SANDBOX_TOKEN") - if trial_token is None: - raise AssertionError( - "Please configure your ONBOARDING_SANDBOX_TOKEN to run the example" - ) - print("Running sandbox example...") - sandbox_example(trial_token=trial_token, email=random_mail(), verbose=False) diff --git a/tests/conftest.py b/tests/conftest.py index 7477037..0f57507 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,16 +15,6 @@ def given_valid_api_key(): return api_key -@pytest.fixture -def given_valid_trial_token(): - trial_token = os.environ.get("ONBOARDING_SANDBOX_TOKEN") - if trial_token is None: - raise AssertionError( - "Please configure your ONBOARDING_SANDBOX_TOKEN to run the tests" - ) - return trial_token - - @pytest.fixture def given_any_valid_mail(): domains = [ diff --git a/tests/test_integration_sandbox.py b/tests/test_integration_sandbox.py deleted file mode 100644 index 5c5953b..0000000 --- a/tests/test_integration_sandbox.py +++ /dev/null @@ -1,36 +0,0 @@ -import pytest -from meiga.assertions import assert_failure, assert_success - -from alice import Config, DeviceInfo, Sandbox, UserInfo - - -@pytest.mark.unit -def test_should_return_an_error_when_the_trial_token_is_not_configured(): - - config = Config() - sandbox = Sandbox.from_config(config) - - result = sandbox.create_user() - - assert_failure(result) - - -@pytest.mark.unit -def test_should_create_a_user_and_get_user_token_and_delete_it( - given_valid_trial_token, given_any_valid_mail -): - - config = Config(trial_token=given_valid_trial_token) - sandbox = Sandbox.from_config(config) - - result_create_user = sandbox.create_user( - user_info=UserInfo(email=given_any_valid_mail), - device_info=DeviceInfo(device_platform="Android"), - ) - assert_success(result_create_user) - - result_user_token = sandbox.get_user_token(email=given_any_valid_mail) - assert_success(result_user_token) - - result_delete_user = sandbox.delete_user(email=given_any_valid_mail) - assert_success(result_delete_user) From 18b36a6a2673296b0207fd94c256f3db586e91bd Mon Sep 17 00:00:00 2001 From: Miguel Lorenzo Date: Mon, 4 Sep 2023 09:32:00 +0200 Subject: [PATCH 3/5] feat: get rid off URLs in config and use Environment --- alice/auth/auth.py | 11 +++++------ alice/config.py | 7 +++---- alice/onboarding/enums/environment.py | 18 ++++++++++++------ alice/onboarding/onboarding.py | 15 +++++++++------ alice/onboarding/onboarding_client.py | 4 +--- alice/webhooks/webhooks.py | 11 +++++------ alice/webhooks/webhooks_client.py | 4 +--- 7 files changed, 36 insertions(+), 34 deletions(-) diff --git a/alice/auth/auth.py b/alice/auth/auth.py index db7f849..caacc52 100644 --- a/alice/auth/auth.py +++ b/alice/auth/auth.py @@ -6,12 +6,11 @@ from alice.config import Config +from ..onboarding.enums.environment import Environment from .auth_client import AuthClient from .auth_errors import AuthError from .token_tools import get_token_from_response -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding" - class Auth: @staticmethod @@ -23,7 +22,7 @@ def from_config(config: Config) -> "Auth": return Auth( api_key=config.api_key, # type: ignore session=session, - url=config.onboarding_url, + environment=config.environment, timeout=config.timeout, verbose=config.verbose, ) @@ -32,14 +31,14 @@ def __init__( self, api_key: str, session: Session, - url: str = DEFAULT_URL, + environment: Environment, timeout: Union[float, None] = None, verbose: Optional[bool] = False, ): self._auth_client = AuthClient( - url=url, api_key=api_key, session=session, timeout=timeout + url=environment.get_url(), api_key=api_key, session=session, timeout=timeout ) - self.url = url + self.url = environment.get_url() self.verbose = verbose def create_user_token( diff --git a/alice/config.py b/alice/config.py index a7490bc..c63d70f 100644 --- a/alice/config.py +++ b/alice/config.py @@ -3,15 +3,14 @@ from pydantic import BaseModel, Field from requests import Session +from alice.onboarding.enums.environment import Environment + class Config(BaseModel): class Config: arbitrary_types_allowed = True - onboarding_url: str = Field(default="https://apis.alicebiometrics.com/onboarding") - sandbox_url: str = Field( - default="https://apis.alicebiometrics.com/onboarding/sandbox" - ) + environment: Environment = Field(default=Environment.SANDBOX) api_key: Union[str, None] = Field(default=None) trial_token: Union[str, None] = Field(default=None) timeout: Union[float, None] = Field( diff --git a/alice/onboarding/enums/environment.py b/alice/onboarding/enums/environment.py index a32c68b..f6db7f2 100644 --- a/alice/onboarding/enums/environment.py +++ b/alice/onboarding/enums/environment.py @@ -1,9 +1,15 @@ from enum import Enum -class DocumentType(Enum): - ID_CARD = "idcard" - DRIVER_LICENSE = "driverlicense" - RESIDENCE_PERMIT = "residencepermit" - PASSPORT = "passport" - HEALTH_INSURANCE_CARD = "healthinsurancecard" +class Environment(Enum): + SANDBOX = "sandbox" + PRODUCTION = "production" + STAGING = "staging" + + def get_url(self) -> str: + if self is Environment.SANDBOX: + return "https://apis.sandbox.alicebiometrics.com/onboarding" + elif self is Environment.PRODUCTION: + return "https://apis.alicebiometrics.com/onboarding" + else: + return "https://apis.staging.alicebiometrics.com/onboarding" diff --git a/alice/onboarding/onboarding.py b/alice/onboarding/onboarding.py index 3778d69..ac39155 100644 --- a/alice/onboarding/onboarding.py +++ b/alice/onboarding/onboarding.py @@ -13,6 +13,7 @@ from alice.onboarding.enums.document_source import DocumentSource from alice.onboarding.enums.document_type import DocumentType from alice.onboarding.enums.duplicates_resource_type import DuplicatesResourceType +from alice.onboarding.enums.environment import Environment from alice.onboarding.enums.match_case import MatchCase from alice.onboarding.enums.onboarding_steps import OnboardingSteps from alice.onboarding.enums.user_state import UserState @@ -24,8 +25,6 @@ from alice.onboarding.onboarding_client import OnboardingClient from alice.onboarding.onboarding_errors import OnboardingError -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding" - class Onboarding: @staticmethod @@ -36,7 +35,7 @@ def from_config(config: Config) -> "Onboarding": session = Session() return Onboarding( auth=Auth.from_config(config), - url=config.onboarding_url, + environment=config.environment, timeout=config.timeout, send_agent=config.send_agent, verbose=config.verbose, @@ -47,15 +46,19 @@ def __init__( self, auth: Auth, session: Session, - url: str = DEFAULT_URL, + environment: Environment = Environment.SANDBOX, timeout: Union[float, None] = None, send_agent: bool = True, verbose: bool = False, ): self.onboarding_client = OnboardingClient( - auth=auth, url=url, timeout=timeout, send_agent=send_agent, session=session + auth=auth, + url=environment.get_url(), + timeout=timeout, + send_agent=send_agent, + session=session, ) - self.url = url + self.url = environment.get_url() self.verbose = verbose @early_return diff --git a/alice/onboarding/onboarding_client.py b/alice/onboarding/onboarding_client.py index 52e5d20..b257393 100644 --- a/alice/onboarding/onboarding_client.py +++ b/alice/onboarding/onboarding_client.py @@ -25,15 +25,13 @@ from alice.onboarding.onboarding_errors import OnboardingError from alice.onboarding.tools import print_intro, print_response, print_token, timeit -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding" - class OnboardingClient: def __init__( self, auth: Auth, session: Session, - url: str = DEFAULT_URL, + url: str, timeout: Union[float, None] = None, send_agent: bool = True, ): diff --git a/alice/webhooks/webhooks.py b/alice/webhooks/webhooks.py index 76058e5..b1c90e8 100644 --- a/alice/webhooks/webhooks.py +++ b/alice/webhooks/webhooks.py @@ -6,12 +6,11 @@ from alice.auth.auth import Auth from alice.auth.auth_errors import AuthError from alice.config import Config +from alice.onboarding.enums.environment import Environment from alice.onboarding.onboarding_errors import OnboardingError from alice.webhooks.webhook import Webhook from alice.webhooks.webhooks_client import WebhooksClient -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding" - class Webhooks: @staticmethod @@ -22,7 +21,7 @@ def from_config(config: Config) -> "Webhooks": session = Session() return Webhooks( auth=Auth.from_config(config), - url=config.onboarding_url, + environment=config.environment, send_agent=config.send_agent, verbose=config.verbose, session=session, @@ -32,14 +31,14 @@ def __init__( self, auth: Auth, session: Session, - url: str = DEFAULT_URL, + environment: Environment, send_agent: bool = True, verbose: Optional[bool] = False, ): self.webhooks_client = WebhooksClient( - auth=auth, url=url, send_agent=send_agent, session=session + auth=auth, url=environment.get_url(), send_agent=send_agent, session=session ) - self.url = url + self.url = environment.get_url() self.verbose = verbose @early_return diff --git a/alice/webhooks/webhooks_client.py b/alice/webhooks/webhooks_client.py index 7d81262..3dfcb66 100644 --- a/alice/webhooks/webhooks_client.py +++ b/alice/webhooks/webhooks_client.py @@ -10,15 +10,13 @@ from alice.onboarding.tools import print_intro, print_response, print_token, timeit from alice.webhooks.webhook import Webhook -DEFAULT_URL = "https://apis.alicebiometrics.com/onboarding" - class WebhooksClient: def __init__( self, auth: Auth, session: Session, - url: str = DEFAULT_URL, + url: str, send_agent: bool = True, ): self.auth = auth From b1d70c8b1bc2b604ab7d22a4b82ef72e94102c12 Mon Sep 17 00:00:00 2001 From: Miguel Lorenzo Date: Mon, 4 Sep 2023 09:45:00 +0200 Subject: [PATCH 4/5] fix: set PRODUCTION as default env in Config --- alice/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alice/config.py b/alice/config.py index c63d70f..6a0daa3 100644 --- a/alice/config.py +++ b/alice/config.py @@ -10,7 +10,7 @@ class Config(BaseModel): class Config: arbitrary_types_allowed = True - environment: Environment = Field(default=Environment.SANDBOX) + environment: Environment = Field(default=Environment.PRODUCTION) api_key: Union[str, None] = Field(default=None) trial_token: Union[str, None] = Field(default=None) timeout: Union[float, None] = Field( From 0b8798120a888f95c2bdf28ba455cacf6568de89 Mon Sep 17 00:00:00 2001 From: Miguel Lorenzo Date: Mon, 4 Sep 2023 09:47:00 +0200 Subject: [PATCH 5/5] feat: remove report v0 example --- examples/onboarding_report_v0.py | 98 -------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 examples/onboarding_report_v0.py diff --git a/examples/onboarding_report_v0.py b/examples/onboarding_report_v0.py deleted file mode 100644 index 91928bd..0000000 --- a/examples/onboarding_report_v0.py +++ /dev/null @@ -1,98 +0,0 @@ -import os -from typing import Optional - -from alice import Config, Onboarding -from alice.onboarding.enums.document_side import DocumentSide -from alice.onboarding.enums.document_type import DocumentType -from alice.onboarding.enums.version import Version - -RESOURCES_PATH = f"{os.path.dirname(os.path.abspath(__file__))}/../resources" - - -def onboarding_example(api_key: str, verbose: Optional[bool] = False) -> None: - config = Config(api_key=api_key, verbose=verbose) - onboarding = Onboarding.from_config(config) - - selfie_media_data = given_any_selfie_image_media_data() - document_front_media_data = given_any_document_front_media_data() - document_back_media_data = given_any_document_back_media_data() - - user_id = onboarding.create_user().unwrap_or_raise() - - # Upload a selfie (Recommended 1-second video) - onboarding.add_selfie( - user_id=user_id, media_data=selfie_media_data - ).unwrap_or_raise() - - # Create and upload front and back side from a document - document_id = onboarding.create_document( - user_id=user_id, type=DocumentType.ID_CARD, issuing_country="ESP" - ).unwrap_or_raise() - onboarding.add_document( - user_id=user_id, - document_id=document_id, - media_data=document_front_media_data, - side=DocumentSide.FRONT, - manual=True, - ).unwrap_or_raise() - onboarding.add_document( - user_id=user_id, - document_id=document_id, - media_data=document_back_media_data, - side=DocumentSide.BACK, - manual=True, - ).unwrap_or_raise() - - onboarding.add_other_trusted_document( - user_id=user_id, - pdf=document_front_media_data, - category="MyCategory", - ).unwrap_or_raise() - - # Generate the report - report = onboarding.create_report( - user_id=user_id, version=Version.V0 - ).unwrap_or_raise() - - if verbose: - print(f"report: {report}") - - media_id = list(report.get("selfie_reports").values())[0].get("media_avatar_id") - media = onboarding.retrieve_media( - user_id=user_id, media_id=media_id - ).unwrap_or_raise() - assert isinstance(media, bytes) - - # Enable authentication for a user - # Based on report results and your business logic, you can enable the authentication for a user - onboarding.enable_authentication(user_id=user_id).unwrap_or_raise() - - # Authenticate a user - onboarding.authenticate_user( - user_id=user_id, media_data=selfie_media_data - ).unwrap_or_raise() - - -def given_any_selfie_image_media_data() -> bytes: - with open(f"{RESOURCES_PATH}/selfie.png", "rb") as f: - return f.read() - - -def given_any_document_front_media_data() -> bytes: - with open(f"{RESOURCES_PATH}/idcard_esp_front_example.png", "rb") as f: - return f.read() - - -def given_any_document_back_media_data() -> bytes: - with open(f"{RESOURCES_PATH}/idcard_esp_back_example.png", "rb") as f: - return f.read() - - -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 onboarding example...") - onboarding_example(api_key=api_key, verbose=True)