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: {}, logger: nil) ⇒ Object
Call the specified URL using the specified HTTP method, body and headers rubocop:disable Metrics/AbcSize.
Class Method Details
.call(url:, method: :get, body: '', basic_auth: {}, additional_headers: {}, logger: nil) ⇒ Object
Call the specified URL using the specified HTTP method, body and headers rubocop:disable Metrics/AbcSize
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/uc3-dmp-external-api/client.rb', line 21 def call(url:, method: :get, body: '', basic_auth: {}, additional_headers: {}, logger: nil) uri = URI(url) # Skip the body if we are doing a get body = nil if method.to_sym == :get opts = (body: body, basic_auth: basic_auth, additional_headers: additional_headers, logger: logger) resp = HTTParty.send(method.to_sym, uri, opts) if resp.code != 200 msg = "status: #{resp&.code}, body: #{resp&.body}" raise ExternalApiError, "#{format(MSG_ERROR_FROM_EXTERNAL_API, url: url)} - #{msg}" end resp.body.nil? || resp.body.empty? ? nil : _process_response(resp: resp) rescue JSON::ParserError raise ExternalApiError, format(MSG_UNABLE_TO_PARSE, url: url) rescue HTTParty::Error => e raise ExternalApiError, "#{format(MSG_HTTPARTY_ERR, url: url)} - #{e.}" rescue URI::InvalidURIError raise ExternalApiError, format(MSG_INVALID_URI, url: url) end |