Class: Uc3DmpExternalApi::Client
- Inherits:
-
Object
- Object
- Uc3DmpExternalApi::Client
- Defined in:
- lib/uc3-dmp-external-api/client.rb
Overview
Helper class for communicating with external APIs
Constant Summary collapse
- MSG_ERROR_FROM_EXTERNAL_API =
'Received an error from %<url>s'- MSG_HTTPARTY_ERR =
'HTTParty failure when trying to call %<url>s'- MSG_INVALID_URI =
'%<url>s is an invalid URI'- MSG_UNABLE_TO_PARSE =
'Unable to parse the response from %<url>s'
Class Method Summary collapse
-
.call(url:, method: :get, body: '', basic_auth: {}, additional_headers: {}, timeout: 10, logger: nil) ⇒ Object
Call the specified URL using the specified HTTP method, body and headers rubocop:disable Metrics/AbcSize, Metrics/ParameterLists.
Class Method Details
.call(url:, method: :get, body: '', basic_auth: {}, additional_headers: {}, timeout: 10, logger: nil) ⇒ Object
Call the specified URL using the specified HTTP method, body and headers rubocop:disable Metrics/AbcSize, Metrics/ParameterLists
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/uc3-dmp-external-api/client.rb', line 21 def call(url:, method: :get, body: '', basic_auth: {}, additional_headers: {}, timeout: 10, logger: nil) uri = URI(url) # Skip the body if we are doing a get body = nil if method.to_sym == :get opts = (body:, basic_auth:, additional_headers:, timeout:, logger:) resp = HTTParty.send(method.to_sym, uri, opts) unless [200, 201].include?(resp.code) msg = "status: #{resp&.code}, body: #{resp&.body}" raise ExternalApiError, "#{format(MSG_ERROR_FROM_EXTERNAL_API, url:)} - #{msg}" end resp.body.nil? || resp.body.empty? ? nil : _process_response(resp:) rescue JSON::ParserError raise ExternalApiError, format(MSG_UNABLE_TO_PARSE, url:) rescue HTTParty::Error => e raise ExternalApiError, "#{format(MSG_HTTPARTY_ERR, url:)} - #{e.}" rescue HTTParty::ResponseError => e raise ExternalApiError, "#{format(MSG_HTTPARTY_ERR, url:)} - #{e.}" rescue URI::InvalidURIError raise ExternalApiError, format(MSG_INVALID_URI, url:) end |