Class: Ecoportal::API::Common::GraphQL::HttpClient

Inherits:
Common::Client
  • Object
show all
Defined in:
lib/ecoportal/api/common/graphql/http_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, version: "v1", host: "live.ecoportal.com", logger: ::Logger.new(IO::NULL), response_logging: false) ⇒ HttpClient

Returns a new instance of HttpClient.



18
19
20
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 18

def initialize(api_key: nil, version: "v1", host: "live.ecoportal.com", logger: ::Logger.new(IO::NULL), response_logging: false)
  super(api_key: api_key, version: version, host: host, logger: logger, response_logging: false)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



16
17
18
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 16

def host
  @host
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 16

def version
  @version
end

Class Method Details

.base_url(host) ⇒ Object



7
8
9
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 7

def base_url(host)
   "#{protocol(host)}://#{host}"
end

.protocol(host) ⇒ Object



11
12
13
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 11

def protocol(host)
  host.match(/^localhost|^127\.0\.0\.1/)? "http" : "https"
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.

Returns:

  • (HTTP)

    HTTP object.



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

def base_request
  @base_request ||= begin
    case @version
    when NilClass
      HTTP.accept(:json)
    when "v2"
      HTTP.headers("X-ECOPORTAL-API-KEY" => key_token).accept(:json)
    when "graphql"
      HTTP.headers("Authorization" => "Bearer #{key_token}").accept(:json)
    else
      HTTP.headers("X-ApiKey" => key_token).accept(:json)
    end
  end
end

#refresh_key(value) ⇒ Object



22
23
24
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 22

def refresh_key(value)
  @api_key = value
end

#url_for(path) ⇒ String

Full URl builder of the request

Parameters:

  • path (String)

    the tail that completes the url of the request.

Returns:

  • (String)

    the final url.



47
48
49
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 47

def url_for(path)
  version? ? "#{base_url_api}#{path}" : "#{base_url}#{path}"
end