Class: Artemis::Adapters::NetHttpAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/artemis/adapters/net_http_adapter.rb

Direct Known Subclasses

NetHttpPersistentAdapter

Constant Summary

Constants inherited from AbstractAdapter

AbstractAdapter::DEFAULT_HEADERS, AbstractAdapter::EMPTY_HEADERS

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#pool_size, #service_name, #timeout

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#headers, #initialize

Constructor Details

This class inherits a constructor from Artemis::Adapters::AbstractAdapter

Instance Method Details

#connectionObject

Returns a fresh Net::HTTP object that creates a new connection.



39
40
41
42
43
44
45
46
# File 'lib/artemis/adapters/net_http_adapter.rb', line 39

def connection
  Net::HTTP.new(uri.host, uri.port).tap do |client|
    client.use_ssl       = uri.scheme == "https"
    client.open_timeout  = timeout
    client.read_timeout  = timeout
    client.write_timeout = timeout if client.respond_to?(:write_timeout=)
  end
end

#execute(document:, operation_name: nil, variables: {}, context: {}) ⇒ Object

Makes an HTTP request for GraphQL query.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/artemis/adapters/net_http_adapter.rb', line 13

def execute(document:, operation_name: nil, variables: {}, context: {})
  request = Net::HTTP::Post.new(uri.request_uri)

  request.basic_auth(uri.user, uri.password) if uri.user || uri.password

  DEFAULT_HEADERS.merge(headers(context)).each { |name, value| request[name] = value }

  body = {}
  body["query"] = document.to_query_string
  body["variables"] = variables if variables.any?
  body["operationName"] = operation_name if operation_name
  request.body = JSON.generate(body)

  response = connection.request(request)

  case response.code.to_i
  when 200, 400
    JSON.parse(response.body)
  when 500..599
    raise Artemis::GraphQLServerError, "Received server error status #{response.code}: #{response.body}"
  else
    { "errors" => [{ "message" => "#{response.code} #{response.message}" }] }
  end
end