Class: Ecoportal::API::Common::Client
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::Client
- Defined in:
- lib/ecoportal/api/common/client.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
Instance Attribute Summary collapse
-
#logger ⇒ Logger
the logger.
Instance Method Summary collapse
-
#base_request ⇒ HTTP
Creates a HTTP object adding the
X-ApiKeyparam to the header. -
#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: "v1", host: "live.ecoportal.com", logger: nil) ⇒ Client
constructor
An object that holds the configuration of the api connection.
-
#log(level) { ... } ⇒ Object
Logger interface.
-
#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:) ⇒ 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.
- #without_response_logging(&block) ⇒ Object
-
#wrap_response(response) ⇒ Common::Reponse
Wrap with basic custom object of the gem for responses.
Constructor Details
#initialize(api_key:, version: "v1", host: "live.ecoportal.com", logger: nil) ⇒ 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.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ecoportal/api/common/client.rb', line 21 def initialize(api_key:, version: "v1", host: "live.ecoportal.com", logger: nil) @version = version @api_key = api_key @logger = logger 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 @response_logging_enabled = true end |
Instance Attribute Details
#logger ⇒ Logger
the logger.
12 13 14 |
# File 'lib/ecoportal/api/common/client.rb', line 12 def logger @logger 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 param to the header.
125 126 127 |
# File 'lib/ecoportal/api/common/client.rb', line 125 def base_request @base_request ||= HTTP.headers("X-ApiKey" => @api_key).accept(:json) end |
#delete(path) ⇒ Common::Reponse
Sends an http DELETE request against the api version using path to complete the base url.
97 98 99 100 101 102 103 |
# File 'lib/ecoportal/api/common/client.rb', line 97 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.
58 59 60 61 62 63 64 |
# File 'lib/ecoportal/api/common/client.rb', line 58 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"
46 47 48 |
# File 'lib/ecoportal/api/common/client.rb', line 46 def log(level, &block) logger.send(level, &block) if logger 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.
86 87 88 89 90 91 92 |
# File 'lib/ecoportal/api/common/client.rb', line 86 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
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.
72 73 74 75 76 77 78 |
# File 'lib/ecoportal/api/common/client.rb', line 72 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.
111 112 113 |
# File 'lib/ecoportal/api/common/client.rb', line 111 def request wrap_response yield(base_request) end |
#url_for(path) ⇒ String
Full URl builder of the request
132 133 134 |
# File 'lib/ecoportal/api/common/client.rb', line 132 def url_for(path) @base_uri+@version+path end |
#without_response_logging(&block) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/ecoportal/api/common/client.rb', line 136 def without_response_logging(&block) begin @response_logging_enabled = false yield self ensure @response_logging_enabled = true end end |