lib/yoti/doc_scan/client.rb in yoti-1.7.1 vs lib/yoti/doc_scan/client.rb in yoti-1.8.0

- old
+ new

@@ -16,19 +16,22 @@ Yoti::DocScan::Session::Create::SessionSpecification, session_specification, 'session_specification' ) - response = create_request - .with_http_method('POST') - .with_endpoint('sessions') - .with_payload(session_specification) - .with_query_param('sdkId', Yoti.configuration.client_sdk_id) - .build - .execute + request = create_request + .with_http_method('POST') + .with_endpoint('sessions') + .with_payload(session_specification) + .with_query_param('sdkId', Yoti.configuration.client_sdk_id) + .build - Yoti::DocScan::Session::Create::CreateSessionResult.new(JSON.parse(response.body)) + begin + Yoti::DocScan::Session::Create::CreateSessionResult.new(JSON.parse(request.execute.body)) + rescue Yoti::RequestError => e + raise Yoti::DocScan::Error.wrap(e) + end end # # Retrieves the state of a previously created Yoti Doc Scan session # @@ -37,18 +40,21 @@ # @return [Yoti::DocScan::Session::Retrieve::GetSessionResult] # def get_session(session_id) Validation.assert_is_a(String, session_id, 'session_id') - response = create_request - .with_http_method('GET') - .with_endpoint(session_path(session_id)) - .with_query_param('sdkId', Yoti.configuration.client_sdk_id) - .build - .execute + request = create_request + .with_http_method('GET') + .with_endpoint(session_path(session_id)) + .with_query_param('sdkId', Yoti.configuration.client_sdk_id) + .build - Yoti::DocScan::Session::Retrieve::GetSessionResult.new(JSON.parse(response.body)) + begin + Yoti::DocScan::Session::Retrieve::GetSessionResult.new(JSON.parse(request.execute.body)) + rescue Yoti::RequestError => e + raise Yoti::DocScan::Error.wrap(e) + end end # # Deletes a previously created Yoti Doc Scan session and all # of its related resources @@ -56,16 +62,21 @@ # @param [String] session_id # def delete_session(session_id) Validation.assert_is_a(String, session_id, 'session_id') - create_request - .with_http_method('DELETE') - .with_endpoint(session_path(session_id)) - .with_query_param('sdkId', Yoti.configuration.client_sdk_id) - .build - .execute + request = create_request + .with_http_method('DELETE') + .with_endpoint(session_path(session_id)) + .with_query_param('sdkId', Yoti.configuration.client_sdk_id) + .build + + begin + request.execute + rescue Yoti::RequestError => e + raise Yoti::DocScan::Error.wrap(e) + end end # # Retrieves media related to a Yoti Doc Scan session based # on the supplied media ID @@ -77,21 +88,26 @@ # def get_media_content(session_id, media_id) Validation.assert_is_a(String, session_id, 'session_id') Validation.assert_is_a(String, media_id, 'media_id') - response = create_request - .with_http_method('GET') - .with_endpoint(media_path(session_id, media_id)) - .with_query_param('sdkId', Yoti.configuration.client_sdk_id) - .build - .execute + request = create_request + .with_http_method('GET') + .with_endpoint(media_path(session_id, media_id)) + .with_query_param('sdkId', Yoti.configuration.client_sdk_id) + .build - Yoti::Media.new( - response.body, - response.get_fields('content-type')[0] - ) + begin + response = request.execute + + Yoti::Media.new( + response.body, + response.get_fields('content-type')[0] + ) + rescue Yoti::RequestError => e + raise Yoti::DocScan::Error.wrap(e) + end end # # Deletes media related to a Yoti Doc Scan session based # on the supplied media ID @@ -101,30 +117,38 @@ # def delete_media_content(session_id, media_id) Validation.assert_is_a(String, session_id, 'session_id') Validation.assert_is_a(String, media_id, 'media_id') - create_request - .with_http_method('DELETE') - .with_endpoint(media_path(session_id, media_id)) - .with_query_param('sdkId', Yoti.configuration.client_sdk_id) - .build - .execute + request = create_request + .with_http_method('DELETE') + .with_endpoint(media_path(session_id, media_id)) + .with_query_param('sdkId', Yoti.configuration.client_sdk_id) + .build + + begin + request.execute + rescue Yoti::RequestError => e + raise Yoti::DocScan::Error.wrap(e) + end end # # Gets a list of supported documents. # # @return [Yoti::DocScan::Support::SupportedDocumentsResponse] # def supported_documents - response = create_request - .with_http_method('GET') - .with_endpoint('supported-documents') - .build - .execute + request = create_request + .with_http_method('GET') + .with_endpoint('supported-documents') + .build - Yoti::DocScan::Support::SupportedDocumentsResponse.new(JSON.parse(response.body)) + begin + Yoti::DocScan::Support::SupportedDocumentsResponse.new(JSON.parse(request.execute.body)) + rescue Yoti::RequestError => e + raise Yoti::DocScan::Error.wrap(e) + end end private #