Class: Braintree::GraphQLClient
  
  
  
  
  
    - Inherits:
- 
      Http
      
        
          - Object
- Http
- Braintree::GraphQLClient
 show all
    - Defined in:
- lib/braintree/graphql_client.rb
 
  Constant Summary
  
  Constants inherited
     from Http
  Http::LINE_FEED
  
    
      Class Method Summary
      collapse
    
    
  
    
      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
  
    
  
  
    
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 | 
 
  
 
  
    Class Method Details
    
      
  
  
    .get_validation_error_code(error)  ⇒ Object 
  
  
  
  
    | 
46
47
48 | # File 'lib/braintree/graphql_client.rb', line 46
def self.get_validation_error_code(error)
  error[:extensions] && error[:extensions][:legacyCode] rescue nil
end | 
 
    
      
  
  
    .get_validation_errors(response)  ⇒ Object 
  
  
  
  
    | 
34
35
36
37
38
39
40
41
42
43
44 | # File 'lib/braintree/graphql_client.rb', line 34
def self.get_validation_errors(response)
  return nil unless response.key?(:errors) && response[:errors].is_a?(Array)
  validation_errors = response[:errors].map do |error|
    {
      :attribute => "",
      :code => get_validation_error_code(error),
      :message => error[:message]
    }
  end
  {errors: validation_errors}
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.["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 |