Class: Ecoportal::API::Common::Client

Inherits:
Object
  • Object
show all
Includes:
ElasticApmIntegration
Defined in:
lib/ecoportal/api/common/client.rb

Overview

Note:
  • You can see the documentation of the HTTP module in the repository
    • it does extend the module Chainable (chainable.rb),
    • where all the http requests are dev by using HTTP::Client#request (client.rb)
    • which calls build_request (new HTTP::Request) and perform (new HTTP::Connection)
    • to return HTTP::Response (response.rb)

Constant Summary collapse

DELAY_REQUEST_RETRY =
5

Constants included from ElasticApmIntegration

ElasticApmIntegration::APM_SERVICE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ElasticApmIntegration

#log_unexpected_server_error

Constructor Details

#initialize(api_key:, version: "v1", host: "live.ecoportal.com", logger: nil, response_logging: false) ⇒ Client

Note:

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ecoportal/api/common/client.rb', line 27

def initialize(api_key:, version: "v1", host: "live.ecoportal.com", logger: nil, response_logging: false)
  @version  = version
  @api_key  = api_key
  @logger   = logger
  @host     = host
  @response_logging_enabled = response_logging
  if host.match(/^localhost|^127\.0\.0\.1/)
    @base_uri = "http://#{host}/api/"
  else
    @base_uri = "https://#{host}/api/"
  end
  log(:info) { "#{version} client initialized pointing at #{host}" }
  if @api_key.nil? || @api_key.match(/\A\W*\z/)
    log(:error) { "Api-key missing!" }
  end
end

Instance Attribute Details

#hostString (readonly)

the remote target server.



13
14
15
# File 'lib/ecoportal/api/common/client.rb', line 13

def host
  @host
end

#loggerLogger

the logger.



13
14
15
# File 'lib/ecoportal/api/common/client.rb', line 13

def logger
  @logger
end

Instance Method Details

#base_requestHTTP

Note:

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.



132
133
134
135
136
137
138
139
140
141
# File 'lib/ecoportal/api/common/client.rb', line 132

def base_request
  @base_request ||= begin
    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
end

#delete(path) ⇒ Common::Reponse

Sends an http DELETE request against the api version using path to complete the base url.



104
105
106
107
108
109
110
# File 'lib/ecoportal/api/common/client.rb', line 104

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.

Options Hash (params:):

  • :page (String)

    the current page we are requesting with given the :per_page offset.

  • :per_page (String)

    the offset or the number of entries you get per request.

  • :q (String)

    some text to search. Omit this parameter to target all the entries.



65
66
67
68
69
70
71
# File 'lib/ecoportal/api/common/client.rb', line 65

def get(path, params: {})
  instrument("GET", path, params) do
    request do |http|
      http.get(url_for(path), params: params)
    end
  end
end

#log(level) { ... } ⇒ Object

Logger interface. @example: log(:info) information on what's going on" log(:warn) is a warning that something is likely to have gone amiss" log(:error) went wrong" log(:fatal) unrecoverable error has happend"

Yields:

  • [] generates the message.

Yield Returns:

  • (String)

    the generated message.



53
54
55
# File 'lib/ecoportal/api/common/client.rb', line 53

def log(level, &block)
  logger.send(level, &block) if logger
end

#patch(path, data:) ⇒ Common::Reponse

Note:

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.



93
94
95
96
97
98
99
# File 'lib/ecoportal/api/common/client.rb', line 93

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:) ⇒ Common::Reponse

Note:

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.



79
80
81
82
83
84
85
# File 'lib/ecoportal/api/common/client.rb', line 79

def post(path, data:)
  instrument("POST", path, data) do
    request do |http|
      http.post(url_for(path), json: data)
    end
  end
end

#request {|http| ... } ⇒ Common::Reponse

Allows to launch a different operation via block, providing the basic HTTP connection to the block.

Yields:

  • (http)

    launch specific http request.

Yield Parameters:

  • http (HTTP)

    the http connection.

Yield Returns:



118
119
120
# File 'lib/ecoportal/api/common/client.rb', line 118

def request
  wrap_response yield(base_request)
end

#url_for(path) ⇒ String

Full URl builder of the request



146
147
148
# File 'lib/ecoportal/api/common/client.rb', line 146

def url_for(path)
  @base_uri+@version+path
end

#wrap_response(response) ⇒ Common::Reponse

Wrap with basic custom object of the gem for responses.



125
126
127
# File 'lib/ecoportal/api/common/client.rb', line 125

def wrap_response(response)
  Ecoportal::API::Common::Response.new(response)
end