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), deep_logging: false) ⇒ HttpClient

Returns a new instance of HttpClient.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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),
  deep_logging:     false
)
  super(
    api_key:          api_key,
    version:          version,
    host:             host,
    logger:           logger,
    deep_logging:     deep_logging
  )
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.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 41

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

#refresh_key(value) ⇒ Object



34
35
36
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 34

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.



58
59
60
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 58

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