Skip to content

Commit

Permalink
Add support for creation of certified pdf report (#6)
Browse files Browse the repository at this point in the history
* Add support for creation of certified pdf report.
  • Loading branch information
acostapazo authored Jun 26, 2020
1 parent 356cd4a commit 1574b39
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ python examples/onboarding.py

see onboarding example [here](examples/onboarding.py)

#### Certified Onboarding

If you need certified your user data, you can retrieve a certified Pdf Report.

```console
export ONBOARDING_API_KEY="<YOUR-API-KEY>"
python examples/certified_onboarding.py
```

see certified onboarding [here](examples/certified_onboarding.py)

## Authentication :closed_lock_with_key:

To manage authorization and token creations, use *Auth* class.
Expand Down
77 changes: 74 additions & 3 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ def void_selfie(
A Result where if the operation is successful it returns True.
Otherwise, it returns an OnboardingError.
"""
response = self.onboarding_client.void_selfie(
user_id=user_id, verbose=verbose
)
response = self.onboarding_client.void_selfie(user_id=user_id, verbose=verbose)

if response.status_code == 200:
return isSuccess
Expand Down Expand Up @@ -642,6 +640,79 @@ def create_report(
)
)

def create_certificated_pdf_report(
self, user_id: str, template_name: str = "default", verbose: bool = False
) -> Result[Dict, OnboardingError]:
"""
This call is used to create a signed pdf report of the onboarding process for a specific user.
It returns a identifier (pdf_report_id) as a reference of created resource.
This resource contains all evidence defined in the template.
Parameters
----------
user_id
User identifier
template_name
'default' (only available)
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Result where if the operation is successful it returns a str with a pdf_report_id.
Otherwise, it returns an OnboardingError.
"""
response = self.onboarding_client.create_certificated_pdf_report(
user_id=user_id, template_name=template_name, verbose=verbose
)

if response.status_code == 200:
return Success(response.json()["pdf_report_id"])
else:
return Failure(
OnboardingError.from_response(
operation="create_report", response=response
)
)

def retrieve_certificated_pdf_report(
self, user_id: str, pdf_report_id: str, verbose: bool = False
) -> Result[bytes, OnboardingError]:
"""
Returns the binary data of an existent pdf report
Parameters
----------
user_id
User identifier
pdf_report_id
PdfReport Identifier. You can obtain it using retrieve_pdf_report method.
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Result where if the operation is successful it returns a binary pdf (bytes).
Otherwise, it returns an OnboardingError.
"""
response = self.onboarding_client.retrieve_certificated_pdf_report(
user_id=user_id, pdf_report_id=pdf_report_id, verbose=verbose
)

if response.status_code == 200:
return Success(response.content)
else:
return Failure(
OnboardingError.from_response(
operation="retrieve_media", response=response
)
)

def authorize_user(
self, user_id: str, verbose: bool = False
) -> Result[bool, OnboardingError]:
Expand Down
85 changes: 85 additions & 0 deletions alice/onboarding/onboarding_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,91 @@ def create_report(self, user_id: str, verbose: bool = False) -> Response:

return response

@timeit
def create_certificated_pdf_report(
self, user_id: str, template_name: str = "default", verbose: bool = False
) -> Response:
"""
This call is used to create a signed pdf report of the onboarding process for a specific user.
It returns a identifier (pdf_report_id) as a reference of created resource.
This resource contains all evidence defined in the template.
Parameters
----------
user_id
User identifier
template_name
'default' (only available)
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Response object [requests library]
"""
print_intro("create_certificated_pdf_report", verbose=verbose)

backend_user_token = self.auth.create_backend_token(user_id=user_id).unwrap()
print_token("backend_token_with_user", backend_user_token, verbose=verbose)
options = {"template_name": template_name}
headers = self._auth_headers(backend_user_token)
headers["Content-Type"] = "application/json"
response = request(
"POST",
self.url + "/user/certificate/pdfreport",
data=json.dumps(options),
headers=headers,
)

print_response(response=response, verbose=verbose)

return response

@timeit
def retrieve_certificated_pdf_report(
self, user_id: str, pdf_report_id: str, verbose: bool = False
) -> Response:
"""
This call is used to create a signed pdf report of the onboarding process for a specific user.
It returns a identifier (pdf_report_id) as a reference of created resource.
This resource contains all evidence defined in the template.
Parameters
----------
user_id
User identifier
pdf_report_id
PdfReport Identifier
template_name
'default' (only available)
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Response object [requests library]
"""
print_intro("retrieve_certificated_pdf_report", verbose=verbose)

backend_user_token = self.auth.create_backend_token(user_id=user_id).unwrap()
print_token("backend_token_with_user", backend_user_token, verbose=verbose)
headers = self._auth_headers(backend_user_token)
response = request(
"GET",
f"{self.url}/user/certificate/pdfreport/{pdf_report_id}",
headers=headers,
)

print_response(response=response, verbose=verbose)

return response

@timeit
def authorize_user(self, user_id: str, verbose: bool = False) -> Response:
"""
Expand Down
85 changes: 85 additions & 0 deletions examples/certified_onboarding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os

from meiga import isSuccess
from meiga.decorators import meiga

from alice import Onboarding, Config

RESOURCES_PATH = f"{os.path.dirname(os.path.abspath(__file__))}/../resources"


@meiga
def certified_onboarding(api_key: str, verbose: bool = False):

config = Config(api_key=api_key)
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(verbose=verbose).unwrap_or_return()

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

# Create and upload front and back side from a document
document_id = onboarding.create_document(
user_id=user_id, type="idcard", issuing_country="ESP", verbose=verbose
).unwrap_or_return()
onboarding.add_document(
user_id=user_id,
document_id=document_id,
media_data=document_front_media_data,
side="front",
manual=True,
verbose=verbose,
).unwrap_or_return()
onboarding.add_document(
user_id=user_id,
document_id=document_id,
media_data=document_back_media_data,
side="back",
manual=True,
verbose=verbose,
).unwrap_or_return()

# Create Certified PdfReport
pdf_report_id = onboarding.create_certificated_pdf_report(
user_id=user_id, verbose=verbose
).unwrap_or_return()

# Retrieved Certified PdfReport from pdf_report_id
pdf_report = onboarding.retrieve_certificated_pdf_report(
user_id=user_id, pdf_report_id=pdf_report_id, verbose=verbose
).unwrap_or_return()

# Save PdfReport data to a file
with open(f"certified_pdf_report_{pdf_report_id}.pdf", "wb") as outfile:
outfile.write(pdf_report)

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...")
certified_onboarding(api_key=api_key, verbose=False)
13 changes: 11 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ def given_valid_sandbox_token():

@pytest.fixture
def given_any_valid_mail():
domains = ["hotmail.com", "gmail.com", "aol.com", "mail.com", "mail.kz", "yahoo.com"]
domains = [
"hotmail.com",
"gmail.com",
"aol.com",
"mail.com",
"mail.kz",
"yahoo.com",
]
letters = string.ascii_lowercase[:12]
mail = ''.join(random.choice(letters) for i in range(7)) + '@' + random.choice(domains)
mail = (
"".join(random.choice(letters) for i in range(7)) + "@" + random.choice(domains)
)
return mail


Expand Down
5 changes: 2 additions & 3 deletions tests/test_integration_onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def do_complete_onboarding() -> Result[dict, Error]:
manual=True,
).handle()
onboarding.document_properties(
user_id=user_id,
document_id=document_id
).unwrap_or_return()
user_id=user_id, document_id=document_id
).unwrap_or_return()

report = onboarding.create_report(user_id=user_id)

Expand Down
Empty file removed yoti-pad-test.py
Empty file.

0 comments on commit 1574b39

Please sign in to comment.