Class: GoCardlessPro::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/gocardless_pro/request.rb

Overview

A class that wraps an API request

Constant Summary collapse

MAX_RETRIES =
3
RETRY_DELAY =
0.5
RETRYABLE_EXCEPTIONS =
[Faraday::TimeoutError,
Faraday::ConnectionFailed,
GoCardlessPro::ApiError,
GoCardlessPro::GoCardlessError].freeze

Instance Method Summary collapse

Constructor Details

#initialize(connection, method, path, options) ⇒ Request

Initialize a request class, which makes calls to the API

Parameters:

  • connection
  • method (Symbol)

    the method to make the request with

  • path (String)

    the path to make the request to

  • options (hash)

    options for the request

  • headers (hash)

    headers to send with the request



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Method Details

#make_requestObject

Make the API request



66
67
68
69
70
71
72
73
# File 'lib/gocardless_pro/request.rb', line 66

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

#requestObject

Make the request and wrap it in a Response object



41
42
43
44
45
46
47
# 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_bodyObject

Fetch the body to send with the request



76
77
78
79
80
81
82
83
84
# File 'lib/gocardless_pro/request.rb', line 76

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_queryObject

Get the query params to send with the request



87
88
89
90
91
92
93
# File 'lib/gocardless_pro/request.rb', line 87

def request_query
  if @method == :get
    @given_options.fetch(:params, {})
  else
    {}
  end
end

#with_retriesObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gocardless_pro/request.rb', line 49

def with_retries
  requests_attempted = 0
  total_requests_allowed = MAX_RETRIES

  begin
    yield
  rescue StandardError => e
    requests_attempted += 1

    raise e unless requests_attempted < total_requests_allowed && should_retry?(e)

    sleep(RETRY_DELAY)
    retry
  end
end