Class: Ecoportal::API::Common::Client
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::Client
- Includes:
- RateThrottling, WithRetry
- Defined in:
- lib/ecoportal/api/common/client.rb,
lib/ecoportal/api/common/client/error.rb,
lib/ecoportal/api/common/client/time_out.rb,
lib/ecoportal/api/common/client/throughput.rb,
lib/ecoportal/api/common/client/with_retry.rb,
lib/ecoportal/api/common/client/error/checks.rb,
lib/ecoportal/api/common/client/rate_throttling.rb,
lib/ecoportal/api/common/client/throughput/stats.rb,
lib/ecoportal/api/common/client/elastic_apm_integration.rb
Overview
- You can see the documentation of the
HTTPmodule in the repository- it does
extendthe moduleChainable(chainable.rb), - where all the http requests are dev by using
HTTP::Client#request(client.rb) - which calls
build_request(newHTTP::Request) andperform(newHTTP::Connection) - to return
HTTP::Response(response.rb)
- it does
Defined Under Namespace
Modules: ElasticApmIntegration, Error, RateThrottling, TimeOut, WithRetry Classes: Throughput
Constant Summary collapse
- DEFAULT_HOST =
'live.ecoportal.com'.freeze
- MAIN_END_POINT =
'api'.freeze
Constants included from WithRetry
WithRetry::DELAY_REQUEST_RETRY, WithRetry::HANDLED_CONNECTION_ERRORS, WithRetry::RETRY_ATTEMPTS
Constants included from ElasticApmIntegration
ElasticApmIntegration::APM_SERVICE_NAME
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
the remote target server.
-
#logger ⇒ Logger
the logger.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#base_request ⇒ HTTP
Creates a HTTP object adding the
X-ApiKeyorX-ECOPORTAL-API-KEYparam to the header, depending on the API version. -
#delete(path) ⇒ Common::Reponse
Sends an http
DELETErequest against the api version usingpathto complete the base url. -
#get(path, params: {}) ⇒ Common::Reponse
Sends an http
GETrequest against the api version usingpathto complete the base url, and adding the key_value pairs ofparamsin the http header. -
#initialize(api_key:, version: :unused, host: DEFAULT_HOST, logger: nil, deep_logging: false) ⇒ Client
constructor
An object that holds the configuration of the api connection.
-
#patch(path, data:) ⇒ Common::Reponse
Sends an http
PATCHrequest against the api version usingpathto complete the base url, and thedataas a body of the http request. -
#post(path, data:, params: {}) ⇒ Common::Reponse
Sends an http
POSTrequest against the api version usingpathto complete the base url, and thedataas a body of the http request. -
#request {|http| ... } ⇒ Common::Reponse
Allows to launch a different operation via
block, providing the basic HTTP connection to the block. -
#url_for(path) ⇒ String
Full URl builder of the request.
-
#wrap_response(response) ⇒ Common::Reponse
Wrap with basic custom object of the gem for responses.
Methods included from ElasticApmIntegration
Constructor Details
#initialize(api_key:, version: :unused, host: DEFAULT_HOST, logger: nil, deep_logging: false) ⇒ Client
the api_key will be automatically added as parameter X-ApiKey in the header of the http requests.
Returns an object that holds the configuration of the api connection.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ecoportal/api/common/client.rb', line 43 def initialize(api_key:, version: :unused, host: DEFAULT_HOST, logger: nil, deep_logging: false) version = nil if version == :unused @version = version unless version.nil? @api_key = api_key @logger = logger @host = host @deep_logging = deep_logging if host.match(/^localhost|^127\.0\.0\.1/) @base_uri = "http://#{host}/#{main_end_point}/" else @base_uri = "https://#{host}/#{main_end_point}/" end if deep_logging? log(:debug) { "#{self.version} client initialized pointing at #{host}" } end return unless api_key.nil? || api_key.match(/\A\W*\z/) return unless self.version log(:error) { 'Api-key missing!' } end |
Instance Attribute Details
#host ⇒ String (readonly)
the remote target server.
26 27 28 |
# File 'lib/ecoportal/api/common/client.rb', line 26 def host @host end |
#logger ⇒ Logger
the logger.
26 27 28 |
# File 'lib/ecoportal/api/common/client.rb', line 26 def logger @logger end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
34 35 36 |
# File 'lib/ecoportal/api/common/client.rb', line 34 def version @version end |
Instance Method Details
#base_request ⇒ HTTP
It configures HTTP so it only allows body data in json format.
Creates a HTTP object adding the X-ApiKey or X-ECOPORTAL-API-KEY param
to the header, depending on the API version.
151 152 153 154 155 156 157 158 159 |
# File 'lib/ecoportal/api/common/client.rb', line 151 def base_request @base_request ||= case @version when 'v2' HTTP.headers('X-ECOPORTAL-API-KEY' => api_key).accept(:json) else HTTP.headers('X-ApiKey' => api_key).accept(:json) end end |
#delete(path) ⇒ Common::Reponse
Sends an http DELETE request against the api version using path to complete the base url.
122 123 124 125 126 127 128 |
# File 'lib/ecoportal/api/common/client.rb', line 122 def delete(path) instrument('DELETE', path) do request do |http| http.delete(url_for(path)) end end end |
#get(path, params: {}) ⇒ Common::Reponse
Sends an http GET request against the api version using path to complete the base url,
and adding the key_value pairs of params in the http header.
77 78 79 80 81 82 83 |
# File 'lib/ecoportal/api/common/client.rb', line 77 def get(path, params: {}) instrument('GET', path, params) do request do |http| http.get(url_for(path), params: params) end end end |
#patch(path, data:) ⇒ Common::Reponse
it automatically adds the http header param Content-Type as application/json
Sends an http PATCH request against the api version using path to complete the base url,
and the data as a body of the http request.
111 112 113 114 115 116 117 |
# File 'lib/ecoportal/api/common/client.rb', line 111 def patch(path, data:) instrument('PATCH', path, data) do request do |http| http.patch(url_for(path), json: data) end end end |
#post(path, data:, params: {}) ⇒ Common::Reponse
it automatically adds the http header param Content-Type as application/json
Sends an http POST request against the api version using path to complete the base url,
and the data as a body of the http request.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ecoportal/api/common/client.rb', line 93 def post(path, data:, params: {}) instrument('POST', path, params) do request do |http| http.post( url_for(path), json: data, params: params ) end end end |
#request {|http| ... } ⇒ Common::Reponse
Allows to launch a different operation via block, providing the
basic HTTP connection to the block.
136 137 138 |
# File 'lib/ecoportal/api/common/client.rb', line 136 def request wrap_response yield(base_request) end |
#url_for(path) ⇒ String
Full URl builder of the request
164 165 166 |
# File 'lib/ecoportal/api/common/client.rb', line 164 def url_for(path) "#{@base_uri}#{@version}#{path}" end |