Class: GraphQL::Hive::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-hive/client.rb

Overview

API client

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/graphql-hive/client.rb', line 10

def initialize(options)
  @options = options
end

Instance Method Details

#build_request(uri, body) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphql-hive/client.rb', line 40

def build_request(uri, body)
  request = Net::HTTP::Post.new(uri.request_uri)
  request['Authorization'] = @options[:token]
  request['X-Usage-API-Version'] = '2'
  request['content-type'] = 'application/json'
  request['User-Agent'] = "Hive@#{Graphql::Hive::VERSION}"
  request['graphql-client-name'] = 'Hive Ruby Client'
  request['graphql-client-version'] = Graphql::Hive::VERSION
  request.body = JSON.generate(body)
  request
end

#send(path, body, _log_type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphql-hive/client.rb', line 14

def send(path, body, _log_type)
  uri =
    URI::HTTP.build(
      scheme: @options[:port].to_s == '443' ? 'https' : 'http',
      host: @options[:endpoint] || 'app.graphql-hive.com',
      port: @options[:port] || '443',
      path: path
    )

  http = setup_http(uri)
  request = build_request(uri, body)
  response = http.request(request)

  @options[:logger].debug(response.inspect)
  @options[:logger].debug(response.body.inspect)
rescue StandardError => e
  @options[:logger].fatal("Failed to send data: #{e}")
end

#setup_http(uri) ⇒ Object



33
34
35
36
37
38
# File 'lib/graphql-hive/client.rb', line 33

def setup_http(uri)
  http = ::Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = @options[:port].to_s == '443'
  http.read_timeout = 2
  http
end