Skip to content

Commit

Permalink
Add support for idempotency keys
Browse files Browse the repository at this point in the history
  • Loading branch information
justincase committed Jan 29, 2023
1 parent 2f88990 commit f180b47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mollie/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def perform_http_call(http_method, api_method, id = nil, http_body = {}, query =

api_key = http_body.delete(:api_key) || query.delete(:api_key) || @api_key
api_endpoint = http_body.delete(:api_endpoint) || query.delete(:api_endpoint) || @api_endpoint
idempotency_key = http_body.delete(:idempotency_key) || query.delete(:idempotency_key)

unless query.empty?
camelized_query = Util.camelize_keys(query)
Expand Down Expand Up @@ -110,6 +111,10 @@ def perform_http_call(http_method, api_method, id = nil, http_body = {}, query =
request['Authorization'] = "Bearer #{api_key}"
request['User-Agent'] = @version_strings.join(' ')

if http_method == 'POST' && idempotency_key
request['Idempotency-Key'] = idempotency_key
end

begin
response = client.request(request)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Expand Down
12 changes: 12 additions & 0 deletions test/mollie/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def test_perform_http_call_with_api_key_block
assert_equal 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM', Mollie::Client.instance.api_key
end

def test_post_with_idempotency_key
stub_request(:post, 'https://api.mollie.com/v2/payments')
.with(headers: { "Idempotency-Key" => '91d42bd5-e47f-4f4a-b38e-99333b264e78' })
.to_return(status: 200, body: '{}', headers: {})

payment = Mollie::Payment.create(
amount: { value: '10.00', currency: 'EUR' },
redirect_url: 'https://webshop.example.org/order/12345/',
idempotency_key: '91d42bd5-e47f-4f4a-b38e-99333b264e78'
)
end

def test_get_request_convert_to_camel_case
stub_request(:get, 'https://api.mollie.com/v2/my-method?myParam=ok')
.to_return(status: 200, body: '{}', headers: {})
Expand Down

0 comments on commit f180b47

Please sign in to comment.