Class: DPay::Internal::ApiRequestor
- Inherits:
-
Object
- Object
- DPay::Internal::ApiRequestor
- Defined in:
- lib/dpay/internal/api_requestor.rb,
sig/dpay/internal/api_requestor.rbs
Constant Summary collapse
- USER_AGENT =
"dpay-ruby-sdk/#{VERSION} ruby/#{RUBY_VERSION}".freeze
Instance Attribute Summary collapse
-
#checksum ⇒ ChecksumCalculator
readonly
Returns the value of attribute checksum.
-
#config ⇒ Config
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #decode(response) ⇒ Hash[String, untyped]
- #get_json(host, path) ⇒ Hash[String, untyped]
- #get_text(host, path) ⇒ String
-
#initialize(config, http_client) ⇒ ApiRequestor
constructor
A new instance of ApiRequestor.
- #map_error(response) ⇒ ApiError
- #post_json(host, path, body) ⇒ Hash[String, untyped]
- #send_raw(method, host, path, body) ⇒ HTTP::Response
- #send_request(method, host, path, body) ⇒ HTTP::Response
- #service ⇒ String
Constructor Details
#initialize(config, http_client) ⇒ ApiRequestor
Returns a new instance of ApiRequestor.
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
#checksum ⇒ ChecksumCalculator (readonly)
Returns the value of attribute checksum.
8 9 10 |
# File 'lib/dpay/internal/api_requestor.rb', line 8 def checksum @checksum end |
#config ⇒ Config (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/dpay/internal/api_requestor.rb', line 8 def config @config end |
Instance Method Details
#decode(response) ⇒ Hash[String, untyped]
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]
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
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
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]
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
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
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 |
#service ⇒ String
16 17 18 |
# File 'lib/dpay/internal/api_requestor.rb', line 16 def service @config.service end |