Skip to content

Commit

Permalink
Support per-request :environment configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
harrylewis committed Aug 29, 2024
1 parent ca35c25 commit 6d9510c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

- Remove `Payrix.test_mode` configuration.

### Additions

- Support per-request `:environment` configuration for all CRUD operations.

## 1.0.0 (2023-11-27)

- Interact with the Payrix API!
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,16 @@ merchant.members # => [...}

#### Per Request Configuration

Pass in `:api_key` to set the API key used per request.
Set the following configuration parameters per request.

- `:api_key` - Set the API key used.
- `:environment` - Set the environment used (see valid values in Configuration below).

```ruby
Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { api_key: 'b442...' })
Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { api_key: 'b442...', environment: :production })
```

This is useful if you need to set the API key in a thread-safe way, which is not possible through
`Payrix.api_key=`. If both are set, the per request configuration takes precedent.
This is useful if you need to configure the request in a thread-safe way. This is not possible using the static configuration mentioned in Configuration below. The per-request configuration always takes precedent.

### Errors

Expand Down
2 changes: 1 addition & 1 deletion lib/payrix/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Payrix
# The Payrix::Client is used both internally and externally to initiate API requests to the Payrix API.
class Client
def request(method:, resource:, data: {}, filters: {}, options: {})
url = Payrix.configuration.url
url = Payrix.configuration.url(options[:environment])

page_number = Payrix::RequestOptions::Page::Number.construct(options[:page])
page_limit = Payrix::RequestOptions::Page::Limit.construct(options[:limit])
Expand Down
12 changes: 8 additions & 4 deletions spec/lib/payrix/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
WebMock
.stub_request(
:get,
'https://test-api.payrix.com/txns?page[number]=1&page[limit]=10&expand[merchant][]=',
'https://api.payrix.com/txns?page[number]=1&page[limit]=10&expand[merchant][]=',
)
.with(
headers: {
Expand Down Expand Up @@ -68,6 +68,7 @@
limit: 10,
expand: ['merchant'],
api_key: 'custom-key',
environment: :production,
},
)

Expand Down Expand Up @@ -110,7 +111,7 @@
it 'supports advanced requests' do
stub =
WebMock
.stub_request(:post, 'https://test-api.payrix.com/txns?expand[merchant][]=')
.stub_request(:post, 'https://api.payrix.com/txns?expand[merchant][]=')
.with(
headers: {
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -142,6 +143,7 @@
options: {
expand: ['merchant'],
api_key: 'custom-key',
environment: :production,
},
)

Expand Down Expand Up @@ -192,7 +194,7 @@
it 'supports advanced requests' do
stub =
WebMock
.stub_request(:put, 'https://test-api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.stub_request(:put, 'https://api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.with(
headers: {
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -223,6 +225,7 @@
},
options: {
api_key: 'custom-key',
environment: :production,
},
)

Expand Down Expand Up @@ -261,7 +264,7 @@
it 'supports advanced requests' do
stub =
WebMock
.stub_request(:delete, 'https://test-api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.stub_request(:delete, 'https://api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.with(
headers: {
'Content-Type' => 'application/json',
Expand All @@ -286,6 +289,7 @@
resource: 'txns/t1_txn_64026b07cc6a79dd5cfd0da',
options: {
api_key: 'custom-key',
environment: :production,
},
)

Expand Down

0 comments on commit 6d9510c

Please sign in to comment.