Class: DPay::Internal::ApiRequestor

Inherits:
Object
  • Object
show all
Defined in:
lib/dpay/internal/api_requestor.rb,
sig/dpay/internal/api_requestor.rbs

Constant Summary collapse

USER_AGENT =

Returns:

  • (String)
"dpay-ruby-sdk/#{VERSION} ruby/#{RUBY_VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, http_client) ⇒ ApiRequestor

Returns a new instance of ApiRequestor.

Parameters:

  • config (Config)
  • http_client (Object)


10
11
12
13
14
# File 'lib/dpay/internal/api_requestor.rb', line 10

def initialize(config, http_client)
  @config = config
  @http_client = http_client
  @checksum = ChecksumCalculator.new(config.secret_hash)
end

Instance Attribute Details

#checksumChecksumCalculator (readonly)

Returns the value of attribute checksum.

Returns:



8
9
10
# File 'lib/dpay/internal/api_requestor.rb', line 8

def checksum
  @checksum
end

#configConfig (readonly)

Returns the value of attribute config.

Returns:



8
9
10
# File 'lib/dpay/internal/api_requestor.rb', line 8

def config
  @config
end

Instance Method Details

#decode(response) ⇒ Hash[String, untyped]

Parameters:

Returns:

  • (Hash[String, untyped])

Raises:



57
58
59
60
61
62
# File 'lib/dpay/internal/api_requestor.rb', line 57

def decode(response)
  decoded = response.decode_json
  return decoded unless decoded.nil?

  raise ApiServerError.new("Invalid JSON in API response", response.status, nil, {}, response.body)
end

#get_json(host, path) ⇒ Hash[String, untyped]

Parameters:

  • host (Symbol)
  • path (String)

Returns:

  • (Hash[String, untyped])


24
25
26
# File 'lib/dpay/internal/api_requestor.rb', line 24

def get_json(host, path)
  decode(send_request("GET", host, path, nil))
end

#get_text(host, path) ⇒ String

Parameters:

  • host (Symbol)
  • path (String)

Returns:

  • (String)


28
29
30
# File 'lib/dpay/internal/api_requestor.rb', line 28

def get_text(host, path)
  send_request("GET", host, path, nil).body
end

#map_error(response) ⇒ ApiError

Parameters:

Returns:



51
52
53
# File 'lib/dpay/internal/api_requestor.rb', line 51

def map_error(response)
  ErrorMapper.map(response)
end

#post_json(host, path, body) ⇒ Hash[String, untyped]

Parameters:

  • host (Symbol)
  • path (String)
  • body (Hash[String, untyped])

Returns:

  • (Hash[String, untyped])


20
21
22
# File 'lib/dpay/internal/api_requestor.rb', line 20

def post_json(host, path, body)
  decode(send_request("POST", host, path, body))
end

#send_raw(method, host, path, body) ⇒ HTTP::Response

Parameters:

  • method (String)
  • host (Symbol)
  • path (String)
  • body (Hash[String, untyped], nil)

Returns:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dpay/internal/api_requestor.rb', line 39

def send_raw(method, host, path, body)
  headers = { "Accept" => "application/json", "User-Agent" => USER_AGENT }
  encoded = nil

  unless body.nil?
    headers["Content-Type"] = "application/json"
    encoded = PHP.json_encode(body)
  end

  @http_client.request(HTTP::Request.new(method, @config.base_urls.resolve(host) + path, headers, encoded))
end

#send_request(method, host, path, body) ⇒ HTTP::Response

Parameters:

  • method (String)
  • host (Symbol)
  • path (String)
  • body (Hash[String, untyped], nil)

Returns:



32
33
34
35
36
37
# File 'lib/dpay/internal/api_requestor.rb', line 32

def send_request(method, host, path, body)
  response = send_raw(method, host, path, body)
  raise map_error(response) if response.status >= 400

  response
end

#serviceString

Returns:

  • (String)


16
17
18
# File 'lib/dpay/internal/api_requestor.rb', line 16

def service
  @config.service
end