|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import json |
| 5 | + |
| 6 | +import yoti_python_sdk |
| 7 | +from yoti_python_sdk.doc_scan.endpoint import Endpoint |
| 8 | +from yoti_python_sdk.doc_scan.session.retrieve.create_session_result import ( |
| 9 | + CreateSessionResult, |
| 10 | +) |
| 11 | +from yoti_python_sdk.doc_scan.session.retrieve.get_session_result import ( |
| 12 | + GetSessionResult, |
| 13 | +) |
| 14 | +from yoti_python_sdk.doc_scan.session.retrieve.media_value import MediaValue |
| 15 | +from yoti_python_sdk.http import SignedRequest |
| 16 | +from yoti_python_sdk.utils import YotiEncoder |
| 17 | +from .exception import DocScanException |
| 18 | + |
| 19 | + |
| 20 | +class DocScanClient(object): |
| 21 | + """ |
| 22 | + Client used for communication with the Yoti Doc Scan service where any |
| 23 | + signed request is required |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, sdk_id, key, api_url=None): |
| 27 | + self.__sdk_id = sdk_id |
| 28 | + self.__key = key |
| 29 | + if api_url is not None: |
| 30 | + self.__api_url = api_url |
| 31 | + else: |
| 32 | + self.__api_url = yoti_python_sdk.YOTI_DOC_SCAN_ENDPOINT |
| 33 | + |
| 34 | + def create_session(self, session_spec): |
| 35 | + """ |
| 36 | + Creates a Doc Scan session using the supplied session specification |
| 37 | +
|
| 38 | + :param session_spec: the session specification |
| 39 | + :type session_spec: SessionSpec |
| 40 | + :return: the create session result |
| 41 | + :rtype: CreateSessionResult |
| 42 | + :raises DocScanException: if there was an error creating the session |
| 43 | + """ |
| 44 | + payload = json.dumps(session_spec, cls=YotiEncoder).encode("utf-8") |
| 45 | + |
| 46 | + request = ( |
| 47 | + SignedRequest.builder() |
| 48 | + .with_post() |
| 49 | + .with_pem_file(self.__key) |
| 50 | + .with_base_url(self.__api_url) |
| 51 | + .with_endpoint(Endpoint.create_docs_session_path()) |
| 52 | + .with_param("sdkId", self.__sdk_id) |
| 53 | + .with_payload(payload) |
| 54 | + .with_header("Content-Type", "application/json") |
| 55 | + .build() |
| 56 | + ) |
| 57 | + response = request.execute() |
| 58 | + |
| 59 | + if response.status_code != 201: |
| 60 | + raise DocScanException("Failed to create session", response) |
| 61 | + |
| 62 | + data = json.loads(response.text) |
| 63 | + return CreateSessionResult(data) |
| 64 | + |
| 65 | + def get_session(self, session_id): |
| 66 | + """ |
| 67 | + Retrieves the state of a previously created Yoti Doc Scan session |
| 68 | +
|
| 69 | + :param session_id: the session ID |
| 70 | + :type session_id: str |
| 71 | + :return: the session state |
| 72 | + :rtype: GetSessionResult |
| 73 | + :raises DocScanException: if there was an error retrieving the session |
| 74 | + """ |
| 75 | + request = ( |
| 76 | + SignedRequest.builder() |
| 77 | + .with_get() |
| 78 | + .with_pem_file(self.__key) |
| 79 | + .with_base_url(self.__api_url) |
| 80 | + .with_endpoint(Endpoint.retrieve_docs_session_path(session_id)) |
| 81 | + .with_param("sdkId", self.__sdk_id) |
| 82 | + .build() |
| 83 | + ) |
| 84 | + response = request.execute() |
| 85 | + |
| 86 | + if response.status_code != 200: |
| 87 | + raise DocScanException("Failed to retrieve session", response) |
| 88 | + |
| 89 | + data = json.loads(response.text) |
| 90 | + return GetSessionResult(data) |
| 91 | + |
| 92 | + def delete_session(self, session_id): |
| 93 | + """ |
| 94 | + Deletes a previously created Yoti Doc Scan session and |
| 95 | + all of its related resources |
| 96 | +
|
| 97 | + :param session_id: the session id to delete |
| 98 | + :type session_id: str |
| 99 | + :rtype: None |
| 100 | + :raises DocScanException: if there was an error deleting the session |
| 101 | + """ |
| 102 | + request = ( |
| 103 | + SignedRequest.builder() |
| 104 | + .with_http_method("DELETE") |
| 105 | + .with_pem_file(self.__key) |
| 106 | + .with_base_url(self.__api_url) |
| 107 | + .with_endpoint(Endpoint.delete_docs_session_path(session_id)) |
| 108 | + .with_param("sdkId", self.__sdk_id) |
| 109 | + .build() |
| 110 | + ) |
| 111 | + response = request.execute() |
| 112 | + |
| 113 | + if response.status_code < 200 or response.status_code >= 300: |
| 114 | + raise DocScanException("Failed to delete session", response) |
| 115 | + |
| 116 | + def get_media_content(self, session_id, media_id): |
| 117 | + """ |
| 118 | + Retrieves media related to a Yoti Doc Scan session |
| 119 | + based on the supplied media ID |
| 120 | +
|
| 121 | + :param session_id: the session ID |
| 122 | + :type session_id: str |
| 123 | + :param media_id: the media ID |
| 124 | + :type media_id: str |
| 125 | + :return: the media |
| 126 | + :rtype: MediaValue |
| 127 | + :raises DocScanException: if there was an error retrieving the media content |
| 128 | + """ |
| 129 | + request = ( |
| 130 | + SignedRequest.builder() |
| 131 | + .with_get() |
| 132 | + .with_pem_file(self.__key) |
| 133 | + .with_base_url(self.__api_url) |
| 134 | + .with_endpoint(Endpoint.get_media_content_path(session_id, media_id)) |
| 135 | + .with_param("sdkId", self.__sdk_id) |
| 136 | + .build() |
| 137 | + ) |
| 138 | + response = request.execute() |
| 139 | + |
| 140 | + if response.status_code != 200: |
| 141 | + raise DocScanException("Failed to retrieve media content", response) |
| 142 | + |
| 143 | + media_mime_type = response.headers["Content-Type"] |
| 144 | + media_content = response.content |
| 145 | + return MediaValue(media_mime_type, media_content) |
| 146 | + |
| 147 | + def delete_media_content(self, session_id, media_id): |
| 148 | + """ |
| 149 | + Deletes media related to a Yoti Doc Scan session |
| 150 | + based on the supplied media ID |
| 151 | +
|
| 152 | + :param session_id: the session ID |
| 153 | + :type session_id: str |
| 154 | + :param media_id: the media ID |
| 155 | + :type media_id: str |
| 156 | + :rtype: None |
| 157 | + :raises DocScanException: if there was an error deleting the media content |
| 158 | + """ |
| 159 | + request = ( |
| 160 | + SignedRequest.builder() |
| 161 | + .with_http_method("DELETE") |
| 162 | + .with_pem_file(self.__key) |
| 163 | + .with_base_url(self.__api_url) |
| 164 | + .with_endpoint(Endpoint.delete_media_path(session_id, media_id)) |
| 165 | + .with_param("sdkId", self.__sdk_id) |
| 166 | + .build() |
| 167 | + ) |
| 168 | + |
| 169 | + response = request.execute() |
| 170 | + if response.status_code < 200 or response.status_code >= 300: |
| 171 | + raise DocScanException("Failed to delete media content", response) |
0 commit comments