Skip to content

Commit

Permalink
Merge pull request #45 from alice-biometrics/feature/version-param-back
Browse files Browse the repository at this point in the history
feat: version parameter for create_report back
  • Loading branch information
miguel-lorenzo authored Jul 19, 2022
2 parents c9d1efb + cb2ced2 commit 28989b3
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
run: lume -test
- name: Example [Onboarding]
run: python examples/onboarding.py
- name: Example [Onboarding Report V0]
run: python examples/onboarding_report_v0.py
- name: Example [Onboarding with Identification]
run: python examples/onboarding_with_identification.py
- name: Example [Onboarding with Certificate]
Expand Down
20 changes: 14 additions & 6 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Union

from meiga import Failure, Result, Success, isSuccess

Expand Down Expand Up @@ -840,8 +840,11 @@ def void_other_trusted_document(
)

def create_report(
self, user_id: str, verbose: Optional[bool] = False
) -> Result[Report, OnboardingError]:
self,
user_id: str,
version: Version = Version.V1,
verbose: Optional[bool] = False,
) -> Result[Union[Report, Dict], OnboardingError]:
"""
This call is used to get the report of the onboarding process for a specific user.
Expand All @@ -853,21 +856,26 @@ def create_report(
----------
user_id
User identifier
version
Set Report Version
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Result where if the operation is successful it returns a Report object.
A Result where if the operation is successful it returns a Report object if Version.V1 or Dict otherwise.
Otherwise, it returns an OnboardingError.
"""
verbose = self.verbose or verbose
response = self.onboarding_client.create_report(
user_id=user_id, verbose=verbose
user_id=user_id, version=version, verbose=verbose
)

if response.status_code == 200:
return Success(Report.parse_obj(response.json()["report"]))
if version is Version.V1:
return Success(Report.parse_obj(response.json()["report"]))
else:
return Success(response.json()["report"])
else:
return Failure(
OnboardingError.from_response(
Expand Down
5 changes: 4 additions & 1 deletion alice/onboarding/onboarding_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ def add_other_trusted_document(
def create_report(
self,
user_id: str,
version: Version = Version.V1,
verbose: Optional[bool] = False,
) -> Response:
"""
Expand All @@ -861,6 +862,8 @@ def create_report(
----------
user_id
User identifier
version
Set Report Version
verbose
Used for print service response as well as the time elapsed
Expand All @@ -874,7 +877,7 @@ def create_report(
print_token("backend_token_with_user", backend_user_token, verbose=verbose)

headers = self._auth_headers(backend_user_token)
headers["Alice-Report-Version"] = Version.V1.value
headers["Alice-Report-Version"] = version.value

response = requests.get(f"{self.url}/user/report", headers=headers)

Expand Down
99 changes: 99 additions & 0 deletions examples/onboarding_report_v0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import os
from typing import Optional

from meiga import isSuccess
from meiga.decorators import meiga

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"


@meiga
def onboarding_example(api_key: str, verbose: Optional[bool] = False):
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_throw()

# Upload a selfie (Recommended 1-second video)
onboarding.add_selfie(
user_id=user_id, media_data=selfie_media_data
).unwrap_or_throw()

# 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_throw()
onboarding.add_document(
user_id=user_id,
document_id=document_id,
media_data=document_front_media_data,
side=DocumentSide.FRONT,
manual=True,
).unwrap_or_throw()
onboarding.add_document(
user_id=user_id,
document_id=document_id,
media_data=document_back_media_data,
side=DocumentSide.BACK,
manual=True,
).unwrap_or_throw()

onboarding.add_other_trusted_document(
user_id=user_id,
pdf=document_front_media_data,
category="MyCategory",
).unwrap_or_throw()

# Generate the report
report = onboarding.create_report(
user_id=user_id, version=Version.V0
).unwrap_or_throw()

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_throw()
assert isinstance(media, bytes)

# Authorize an user
# Based on report results and your business logic, you can authorize an user
onboarding.authorize_user(user_id=user_id)

# Authenticate an user (only available if a user is already authorized)
onboarding.authenticate_user(user_id=user_id, media_data=selfie_media_data)

return isSuccess


def given_any_selfie_image_media_data():
return open(f"{RESOURCES_PATH}/selfie.png", "rb").read()


def given_any_document_front_media_data():
return open(f"{RESOURCES_PATH}/idcard_esp_front_example.png", "rb").read()


def given_any_document_back_media_data():
return open(f"{RESOURCES_PATH}/idcard_esp_back_example.png", "rb").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)

0 comments on commit 28989b3

Please sign in to comment.