Skip to content

Commit

Permalink
Add /user/certificates wrapper. Refactor certificates naming
Browse files Browse the repository at this point in the history
  • Loading branch information
acostapazo committed Jun 29, 2020
1 parent 84764f3 commit 619ec43
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 39 deletions.
60 changes: 46 additions & 14 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,12 @@ def create_report(
)
)

def create_certificated_pdf_report(
def create_certificate(
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 call is used to create a Certificate (Signed PDF Report) of the onboarding process for a specific user.
It returns a identifier (certificate_id) as a reference of created resource.
This resource contains all evidence defined in the template.
Expand All @@ -665,21 +664,21 @@ def create_certificated_pdf_report(
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(
response = self.onboarding_client.create_certificate(
user_id=user_id, template_name=template_name, verbose=verbose
)

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

def retrieve_certificated_pdf_report(
self, user_id: str, pdf_report_id: str, verbose: bool = False
def retrieve_certificate(
self, user_id: str, certificate_id: str, verbose: bool = False
) -> Result[bytes, OnboardingError]:
"""
Expand All @@ -689,8 +688,8 @@ def retrieve_certificated_pdf_report(
----------
user_id
User identifier
pdf_report_id
PdfReport Identifier. You can obtain it using retrieve_pdf_report method.
certificate_id
Certificate Unique Identifier. You can obtain it using create_certificate method.
verbose
Used for print service response as well as the time elapsed
Expand All @@ -700,16 +699,49 @@ def retrieve_certificated_pdf_report(
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
response = self.onboarding_client.retrieve_certificate(
user_id=user_id, certificate_id=certificate_id, verbose=verbose
)

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

def retrieve_certificates(
self, user_id: str, verbose: bool = False
) -> Result[bytes, OnboardingError]:
"""
Returns summary info for created certificates
Parameters
----------
user_id
User identifier
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Result where if the operation is successful it returns a list of dictionaries.
Otherwise, it returns an OnboardingError.
"""
response = self.onboarding_client.retrieve_certificates(
user_id=user_id, verbose=verbose
)

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

Expand Down
58 changes: 40 additions & 18 deletions alice/onboarding/onboarding_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,13 +668,12 @@ def create_report(self, user_id: str, verbose: bool = False) -> Response:
return response

@timeit
def create_certificated_pdf_report(
def create_certificate(
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 call is used to create a Certificate (Signed PDF Report) of the onboarding process for a specific user.
It returns a identifier (certificate_id) as a reference of created resource.
This resource contains all evidence defined in the template.
Expand All @@ -692,7 +691,7 @@ def create_certificated_pdf_report(
-------
A Response object [requests library]
"""
print_intro("create_certificated_pdf_report", verbose=verbose)
print_intro("create_certificate", 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)
Expand All @@ -701,7 +700,7 @@ def create_certificated_pdf_report(
headers["Content-Type"] = "application/json"
response = request(
"POST",
self.url + "/user/certificate/pdfreport",
self.url + "/user/certificate",
data=json.dumps(options),
headers=headers,
)
Expand All @@ -711,24 +710,21 @@ def create_certificated_pdf_report(
return response

@timeit
def retrieve_certificated_pdf_report(
self, user_id: str, pdf_report_id: str, verbose: bool = False
def retrieve_certificate(
self, user_id: str, certificate_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 call is used to retrieve a Certificate (Signed PDF Report) of the onboarding process for a specific user.
This resource contains all evidence defined in the template.
Parameters
----------
user_id
User identifier
pdf_report_id
PdfReport Identifier
template_name
'default' (only available)
certificate_id
Certificate Unique Identifier
verbose
Used for print service response as well as the time elapsed
Expand All @@ -737,21 +733,47 @@ def retrieve_certificated_pdf_report(
-------
A Response object [requests library]
"""
print_intro("retrieve_certificated_pdf_report", verbose=verbose)
print_intro("retrieve_certificate", 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,
"GET", f"{self.url}/user/certificate/{certificate_id}", headers=headers
)

print_response(response=response, verbose=verbose)

return response

@timeit
def retrieve_certificates(self, user_id: str, verbose: bool = False) -> Response:
"""
Returns summary info for created certificates
Parameters
----------
user_id
User identifier
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Response object [requests library]
"""
print_intro("retrieve_certificates", 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/certificates", headers=headers)

print_response(response=response, verbose=verbose)

return response

@timeit
def authorize_user(self, user_id: str, verbose: bool = False) -> Response:
"""
Expand Down
20 changes: 13 additions & 7 deletions examples/certified_onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,25 @@ def certified_onboarding(api_key: str, verbose: bool = False):
verbose=verbose,
).unwrap_or_return()

# Create Certified PdfReport
pdf_report_id = onboarding.create_certificated_pdf_report(
# Create Certificate
certificate_id = onboarding.create_certificate(
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
# Retrieved Certificate from certificate_id
certificate = onboarding.retrieve_certificate(
user_id=user_id, certificate_id=certificate_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)
with open(f"certificate_{certificate_id}.pdf", "wb") as outfile:
outfile.write(certificate)

certificates = onboarding.retrieve_certificates(
user_id=user_id, verbose=verbose
).unwrap_or_return()

assert len(certificates) >= 1

return isSuccess

Expand Down

0 comments on commit 619ec43

Please sign in to comment.