Class: Braintree::GraphQLClient

Inherits:
Http
  • Object
show all
Defined in:
lib/braintree/graphql_client.rb

Constant Summary

Constants inherited from Http

Http::LINE_FEED

Instance Method Summary collapse

Methods inherited from Http

#_add_file_part, #_add_form_field, #_body, #_build_query_string, #_build_xml, #_compose_headers, #_current_time, #_format_and_sanitize_body_for_log, #_http_do, #_mime_type_for_file_name, #_setup_connection, #_verify_ssl_certificate, #delete, #get, #post, #put

Constructor Details

#initialize(config) ⇒ GraphQLClient

Returns a new instance of GraphQLClient.



4
5
6
7
8
9
10
11
# File 'lib/braintree/graphql_client.rb', line 4

def initialize(config)
  @config = config
  @graphql_headers = {
    "Accept" => "application/json",
    "Braintree-Version" => @config.graphql_api_version,
    "Content-Type" => "application/json"
  }
end

Instance Method Details

#_parse_response(response) ⇒ Object



28
29
30
31
32
# File 'lib/braintree/graphql_client.rb', line 28

def _parse_response(response)
  body = response.body
  body = Zlib::GzipReader.new(StringIO.new(body)).read if response.header["Content-Encoding"] == "gzip"
  JSON.parse(body, :symbolize_names => true)
end

#query(definition, variables = {}, operationName = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/braintree/graphql_client.rb', line 13

def query(definition, variables = {}, operationName = nil)
  graphql_connection = _setup_connection(@config.graphql_server, @config.graphql_port)

  request = {}
  request["query"] = definition
  request["operationName"] = operationName if operationName
  request["variables"] = variables

  response = _http_do Net::HTTP::Post, @config.graphql_base_url, request.to_json, nil, graphql_connection, @graphql_headers
  data = _parse_response(response)
  Util.raise_exception_for_graphql_error(data)

  data
end