def configure_session_response(self, session_id, response_config): """ :param session_id: the session ID :type session_id: str :param response_config: the response config :type response_config: ResponseConfig :raises SandboxException: if there was an error with the request """ payload = json.dumps(response_config, cls=YotiEncoder).encode("utf-8") path = self.SESSION_RESPONSE_CONFIG_PATH.format(session_id=session_id) request = ( SignedRequest.builder() .with_pem_file(self.__key_file) .with_base_url(self.__api_url) .with_endpoint(path) .with_param("sdkId", self.__sdk_id) .with_payload(payload) .with_http_method("PUT") .build() ) response = request.execute() if response.status_code < 200 or response.status_code >= 300: raise DocScanSandboxException( "Failed on status code: {}".format(str(response.status_code)), response )
def get_media_content(self, session_id, media_id): """ Retrieves media related to a Yoti Doc Scan session based on the supplied media ID :param session_id: the session ID :type session_id: str :param media_id: the media ID :type media_id: str :return: the media :rtype: MediaValue :raises DocScanException: if there was an error retrieving the media content """ request = (SignedRequest.builder().with_get().with_pem_file( self.__key).with_base_url(self.__api_url).with_endpoint( Endpoint.get_media_content_path(session_id, media_id)).with_param( "sdkId", self.__sdk_id).build()) response = request.execute() if response.status_code == 204: return None if response.status_code != 200: raise DocScanException("Failed to retrieve media content", response) media_mime_type = response.headers["Content-Type"] media_content = response.content return MediaValue(media_mime_type, media_content)
def __make_activity_details_request(self, encrypted_request_token): decrypted_token = self.__crypto.decrypt_token(encrypted_request_token).decode( "utf-8" ) path = self.__endpoint.get_activity_details_request_path( decrypted_token, no_params=True ) signed_request = ( SignedRequest.builder() .with_get() .with_pem_file(self.__crypto) .with_base_url(yoti_python_sdk.YOTI_API_ENDPOINT) .with_endpoint(path) .with_param("appId", self.sdk_id) .with_request_handler(self.__request_handler) .build() ) response = signed_request.execute() self.http_error_handler( response, {"default": "Unsuccessful Yoti API call: {} {}"} ) return response
def __make_aml_check_request(self, aml_profile): aml_profile_json = json.dumps(aml_profile.__dict__, sort_keys=True) aml_profile_bytes = aml_profile_json.encode() path = self.__endpoint.get_aml_request_url(no_params=True) signed_request = ( SignedRequest.builder() .with_pem_file(self.__crypto) .with_base_url(yoti_python_sdk.YOTI_API_ENDPOINT) .with_endpoint(path) .with_payload(aml_profile_bytes) .with_param("appId", self.sdk_id) .with_post() .with_request_handler(self.__request_handler) .build() ) response = signed_request.execute() if not isinstance(response, YotiResponse): raise TypeError("Response must be of type YotiResponse") self.http_error_handler( response, {"default": "Unsuccessful Yoti API call: {} {}"} ) return response
def test_create_signed_request_with_crypto_object(valid_base_url, valid_endpoint): http_method = "GET" crypto = Crypto.read_pem_file(conftest.PEM_FILE_PATH) (SignedRequest.builder().with_pem_file(crypto).with_base_url( valid_base_url).with_endpoint(valid_endpoint).with_http_method( http_method).build())
def test_create_signed_request_missing_http_method(valid_base_url, valid_endpoint): with pytest.raises(ValueError) as ex: (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url( valid_base_url).with_endpoint(valid_endpoint).build()) assert "HTTP method" in str(ex.value)
def test_execute_signed_request(valid_base_url, valid_endpoint): signed_request = (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url(valid_base_url).with_endpoint( valid_endpoint).with_post().build()) response = signed_request.execute() assert response.status_code == 200 assert response.text is not None
def test_create_signed_request_missing_pem_file(valid_base_url, valid_endpoint): http_method = "GET" with pytest.raises(ValueError) as ex: (SignedRequest.builder().with_base_url(valid_base_url).with_endpoint( valid_endpoint).with_http_method(http_method).build()) assert "PEM file" in str(ex.value)
def test_create_signed_request_with_payload(valid_base_url, valid_endpoint, json_payload): http_method = "POST" signed_request = (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url(valid_base_url).with_endpoint( valid_endpoint).with_http_method(http_method).with_payload( json_payload).build()) assert signed_request.data == json_payload
def test_signed_request_should_throw_error_when_request_handler_wrong_type( valid_base_url, valid_endpoint): with pytest.raises(TypeError) as ex: (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url(valid_base_url). with_endpoint(valid_endpoint).with_request_handler( "myRequestHandler").with_post().build()) assert "yoti_python_sdk.http.RequestHandler" in str(ex.value)
def test_create_signed_request_with_query_param(valid_base_url, valid_endpoint): http_method = "POST" signed_request = (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url(valid_base_url).with_endpoint( valid_endpoint).with_http_method(http_method).with_param( "sdkId", "mySdkId").build()) assert "sdkId=mySdkId" in signed_request.url
def test_create_signed_request_with_get_convenience_method( valid_base_url, valid_endpoint ): ( SignedRequest.builder() .with_pem_file(conftest.PEM_FILE_PATH) .with_base_url(valid_base_url) .with_endpoint(valid_endpoint) .with_get() .build() )
def test_create_signed_request_with_header(valid_base_url, valid_endpoint): http_method = "POST" signed_request = (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url(valid_base_url).with_endpoint( valid_endpoint).with_http_method(http_method).with_header( "My-Http-Header", "someValue").build()) headers = signed_request.headers assert "My-Http-Header" in headers assert headers["My-Http-Header"] == "someValue"
def make_request(self, http_method, endpoint, body): signed_request = (SignedRequest.builder().with_pem_file( self.__crypto).with_base_url( yoti_python_sdk.YOTI_API_ENDPOINT).with_endpoint( endpoint).with_http_method(http_method).with_payload( body).with_request_handler( self.__request_handler).build()) response = signed_request.execute() if not isinstance(response, YotiResponse): raise TypeError("Response must be of type YotiResponse") return response
def test_create_signed_request_get_required_properties( valid_base_url, valid_endpoint, expected_request_headers): http_method = "GET" signed_request = (SignedRequest.builder().with_pem_file( conftest.PEM_FILE_PATH).with_base_url(valid_base_url).with_endpoint( valid_endpoint).with_http_method(http_method).build()) assert (valid_base_url + valid_endpoint) in signed_request.url assert signed_request.method == http_method assert signed_request.data is None header_keys = signed_request.headers.keys() for header in expected_request_headers: assert header in header_keys
def generate_session(): with open(os.getenv('TEST_IMAGE_PATH'), "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) data = {"data": encoded_string.decode("utf-8")} payload_string = json.dumps(data).encode() signed_request = (SignedRequest.builder().with_pem_file( os.getenv('PEM_FILE_PATH')).with_base_url( os.getenv('HOST') + "/api/v1/age-verification").with_endpoint( "/checks").with_http_method("POST").with_header( "X-Yoti-Auth-Id", os.getenv('SDK_ID')).with_payload(payload_string).build()) # get Yoti response response = signed_request.execute() response_payload = json.loads(response.text) print(response_payload)
def delete_session(self, session_id): """ Deletes a previously created Yoti Doc Scan session and all of its related resources :param session_id: the session id to delete :type session_id: str :rtype: None :raises DocScanException: if there was an error deleting the session """ request = ( SignedRequest.builder().with_http_method("DELETE").with_pem_file( self.__key).with_base_url(self.__api_url).with_endpoint( Endpoint.delete_docs_session_path(session_id)).with_param( "sdkId", self.__sdk_id).build()) response = request.execute() if response.status_code < 200 or response.status_code >= 300: raise DocScanException("Failed to delete session", response)
def get_supported_documents(self): """ Retrieves a list of all of the currently supported documents :return: the supported documents response :rtype: SupportedDocumentsResponse """ request = ( SignedRequest.builder().with_http_method("GET").with_pem_file( self.__key).with_base_url(self.__api_url).with_endpoint( Endpoint.get_supported_documents_path()).build()) response = request.execute() if response.status_code < 200 or response.status_code >= 300: raise DocScanException("Failed to retrieve supported documents", response) parsed = json.loads(response.text) return SupportedDocumentsResponse(parsed)
def get_session(self, session_id): """ Retrieves the state of a previously created Yoti Doc Scan session :param session_id: the session ID :type session_id: str :return: the session state :rtype: GetSessionResult :raises DocScanException: if there was an error retrieving the session """ request = (SignedRequest.builder().with_get().with_pem_file( self.__key).with_base_url(self.__api_url).with_endpoint( Endpoint.retrieve_docs_session_path(session_id)).with_param( "sdkId", self.__sdk_id).build()) response = request.execute() if response.status_code != 200: raise DocScanException("Failed to retrieve session", response) data = json.loads(response.text) return GetSessionResult(data)
def delete_media_content(self, session_id, media_id): """ Deletes media related to a Yoti Doc Scan session based on the supplied media ID :param session_id: the session ID :type session_id: str :param media_id: the media ID :type media_id: str :rtype: None :raises DocScanException: if there was an error deleting the media content """ request = ( SignedRequest.builder().with_http_method("DELETE").with_pem_file( self.__key).with_base_url(self.__api_url).with_endpoint( Endpoint.delete_media_path(session_id, media_id)).with_param( "sdkId", self.__sdk_id).build()) response = request.execute() if response.status_code < 200 or response.status_code >= 300: raise DocScanException("Failed to delete media content", response)
def create_session(self, session_spec): """ Creates a Doc Scan session using the supplied session specification :param session_spec: the session specification :type session_spec: SessionSpec :return: the create session result :rtype: CreateSessionResult :raises DocScanException: if there was an error creating the session """ payload = json.dumps(session_spec, cls=YotiEncoder).encode("utf-8") request = (SignedRequest.builder().with_post().with_pem_file( self.__key).with_base_url(self.__api_url).with_endpoint( Endpoint.create_docs_session_path()).with_param( "sdkId", self.__sdk_id).with_payload(payload).with_header( "Content-Type", "application/json").build()) response = request.execute() if response.status_code != 201: raise DocScanException("Failed to create session", response) data = json.loads(response.text) return CreateSessionResult(data)
def test_create_signed_request_with_invalid_http_method(): with pytest.raises(ValueError) as ex: SignedRequest.builder().with_http_method("INVALID").build() assert str(ex.value) == "INVALID is an unsupported HTTP method"