class GoCardlessPro::Request
A class that wraps an API request
Constants
- MAX_RETRIES
- RETRYABLE_EXCEPTIONS
- RETRY_DELAY
Public Class Methods
new(connection, method, path, options)
click to toggle source
Initialize a request class, which makes calls to the API @param connection @param method [Symbol] the method to make the request with @param path [String] the path to make the request to @param options [hash] options for the request @param headers [hash] headers to send with the request
# File lib/gocardless_pro/request.rb, line 19 def initialize(connection, method, path, options) @connection = connection @method = method @path = path @headers = (options.delete(:headers) || {}).each_with_object({}) do |(k, v), hsh| hsh[k.to_s] = v end @envelope_name = options.delete(:envelope_key) @retry_failures = options.delete(:retry_failures) { true } @given_options = options @request_body = request_body if @request_body.is_a?(Hash) @request_body = @request_body.to_json @headers['Content-Type'] ||= 'application/json' end @headers['Idempotency-Key'] ||= SecureRandom.uuid if @method == :post end
Public Instance Methods
make_request()
click to toggle source
Make the API request
# File lib/gocardless_pro/request.rb, line 68 def make_request @connection.send(@method) do |request| request.url @path request.body = @request_body request.params = request_query request.headers.merge!(@headers) end end
request()
click to toggle source
Make the request and wrap it in a Response
object
# File lib/gocardless_pro/request.rb, line 41 def request if @retry_failures with_retries { Response.new(make_request) } else Response.new(make_request) end end
request_body()
click to toggle source
Fetch the body to send with the request
# File lib/gocardless_pro/request.rb, line 78 def request_body if @method == :get nil elsif %i[post put delete].include?(@method) @given_options.fetch(:params, {}) else raise "Unknown request method #{@method}" end end
request_query()
click to toggle source
Get the query params to send with the request
# File lib/gocardless_pro/request.rb, line 89 def request_query if @method == :get @given_options.fetch(:params, {}) else {} end end
with_retries() { || ... }
click to toggle source
# File lib/gocardless_pro/request.rb, line 49 def with_retries requests_attempted = 0 total_requests_allowed = MAX_RETRIES begin yield rescue => exception requests_attempted += 1 if requests_attempted < total_requests_allowed && should_retry?(exception) sleep(RETRY_DELAY) retry else raise exception end end end
Private Instance Methods
should_retry?(exception)
click to toggle source
# File lib/gocardless_pro/request.rb, line 99 def should_retry?(exception) RETRYABLE_EXCEPTIONS.include?(exception.class) end