Class: AuthingRuby::Common::GraphqlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/authing_ruby/common/GraphqlClient.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ GraphqlClient

Returns a new instance of GraphqlClient.



12
13
14
15
# File 'lib/authing_ruby/common/GraphqlClient.rb', line 12

def initialize(endpoint, options = {})
  @endpoint = endpoint # API 端点
  @options = options
end

Instance Method Details

#request(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/authing_ruby/common/GraphqlClient.rb', line 17

def request(options)
  headers = {
    'content-type': 'application/json',
    'x-authing-sdk-version': "ruby:#{AuthingRuby::VERSION}",
    'x-authing-userpool-id': @options.fetch(:userPoolId, ''),
    'x-authing-request-from': @options.fetch(:requestFrom, 'sdk'),
    'x-authing-app-id': @options.fetch(:appId, ''),
    'x-authing-lang': @options.fetch(:lang, ''),
  };
  token = options.fetch(:token, nil)
  if token
    headers['Authorization'] = "Bearer #{token}"
  end

  json = options.fetch(:json, nil)
  # puts "@endpoint 是 #{@endpoint}"
  response = HTTP.headers(headers).post(@endpoint, json: json)
  return response.body.to_s
end