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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/graphql-hive/client.rb', line 54

def build_request(uri, body)
  request = Net::HTTP::Post.new(uri.request_uri)
  request["Authorization"] = "Bearer #{@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["X-Request-Id"] = SecureRandom.uuid
  request.body = JSON.generate(body)
  request
end

#extract_error_details(response) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/graphql-hive/client.rb', line 77

def extract_error_details(response)
  parsed_body = JSON.parse(response.body)
  return unless parsed_body.is_a?(Hash) && parsed_body["errors"].is_a?(Array)
  parsed_body["errors"].map { |error| "{ path: #{error["path"]}, message: #{error["message"]} }" }.join(", ")
rescue JSON::ParserError
  "Could not parse response from Hive"
end

#log_error(message) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/graphql-hive/client.rb', line 85

def log_error(message)
  if @options[:warn_on_hive_errors]
    @options[:logger].warn(message)
  else
    @options[:logger].error(message)
  end
end

#log_request(request, scheme, endpoint, path) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/graphql-hive/client.rb', line 67

def log_request(request, scheme, endpoint, path)
  log_message = "#{request.method} #{scheme}://#{endpoint}#{path} request id: #{request["X-Request-Id"]}"

  if @options[:log_request_details]
    @options[:logger].info(log_message)
  else
    @options[:logger].debug(log_message)
  end
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql-hive/client.rb', line 14

def send(path, body, _log_type)
  path = path.to_s
  if path == "/usage" && @options[:target] && @options[:target] != ""
    path = "/usage/#{@options[:target]}"
  end

  scheme = (@options[:port].to_s == "443") ? "https" : "http"
  endpoint = @options[:endpoint] || "app.graphql-hive.com"
  uri = URI::HTTP.build(
    scheme: scheme,
    host: endpoint,
    port: @options[:port] || "443",
    path: path
  )
  http = setup_http(uri)
  request = build_request(uri, body)

  log_request(request, scheme, endpoint, path)

  response = http.request(request)

  code = response.code.to_i
  if code >= 400 && code < 500
    error_message = "Unsuccessful response: #{response.code} - #{response.message}"
    log_error("#{error_message} #{extract_error_details(response)}")
  end

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

#setup_http(uri) ⇒ Object



47
48
49
50
51
52
# File 'lib/graphql-hive/client.rb', line 47

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