Class: GraphQL::Hive::Client
- Inherits:
-
Object
- Object
- GraphQL::Hive::Client
- Defined in:
- lib/graphql-hive/client.rb
Overview
API client
Instance Method Summary collapse
- #build_request(uri, body) ⇒ Object
- #extract_error_details(response) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #log_error(message) ⇒ Object
- #log_request(request, scheme, endpoint, path) ⇒ Object
- #send(path, body, _log_type) ⇒ Object
- #setup_http(uri) ⇒ Object
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 = 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() if @options[:warn_on_hive_errors] @options[:logger].warn() else @options[:logger].error() 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) = "#{request.method} #{scheme}://#{endpoint}#{path} request id: #{request["X-Request-Id"]}" if @options[:log_request_details] @options[:logger].info() else @options[:logger].debug() 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 = "Unsuccessful response: #{response.code} - #{response.}" log_error("#{} #{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 |