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

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

Constant Summary collapse

READ_TIMEOUT =
90
WRITE_TIMEOUT =
90

Constants included from AuthService

AuthService::DEFAULT_SERVER, AuthService::TOKEN_AUTORENEW

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AuthService::InstanceMethods

#session_token, #session_token_renewed

Constructor Details

#initialize(email: nil, pass: nil, org_id: nil, api_key: nil, version: 'graphql', host: 'live.ecoportal.com', logger: ::Logger.new(IO::NULL), deep_logging: false, no_schema: true) ⇒ HttpClient

Returns a new instance of HttpClient.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 23

def initialize(
  email:        nil,
  pass:         nil,
  org_id:       nil,
  api_key:      nil,
  version:      'graphql',
  host:         'live.ecoportal.com',
  logger:       ::Logger.new(IO::NULL),
  deep_logging: false,
  no_schema:    true
)
  @org_id      = org_id
  @user_email  = email
  @user_pass   = pass
  @no_schema   = no_schema
  @version     = version

  super(
    api_key:      api_key,
    host:         host,
    logger:       logger,
    deep_logging: deep_logging
  )
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#versionObject (readonly)

Returns the value of attribute version.



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

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 51

def base_request
  @base_request ||=
    case @version
    when NilClass, 'http'
      HTTP
    when 'v1', 'v0'
      HTTP.headers('X-ApiKey' => key_token)
    when 'v2', 'v3'
      HTTP.headers('X-ECOPORTAL-API-KEY' => key_token)
    when 'graphql'
      HTTP.headers('Authorization' => "Bearer #{key_token}")
    end.then do |request|
      request.accept(:json).timeout(
        read:  READ_TIMEOUT,
        write: WRITE_TIMEOUT
      )
    end
end

#org_idObject



70
71
72
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 70

def org_id
  @org_id || fetch_env_required('ORGANIZATION_ID')
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.



77
78
79
# File 'lib/ecoportal/api/common/graphql/http_client.rb', line 77

def url_for(path)
  "#{api_url}#{path}"
end