Skip to content

Commit

Permalink
Added support for redirect responses for http client.
Browse files Browse the repository at this point in the history
Added a new dependency `faraday-follow_redirects` to support redirect responses for Faraday 2.0, given that `Faraday Middleware` is deprecated
  • Loading branch information
a-ibarra committed Nov 29, 2022
1 parent 354f5e3 commit dd55b36
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ also need to specify the custom `http client` for `multipart requests`:
http_client = Faraday.new do |conn|
conn.options.timeout = 10
conn.proxy "http://localhost:8080"
conn.response :raise_error
end

multipart_client = Faraday.new do |f|
Expand All @@ -184,6 +185,25 @@ api = CheckoutSdk.builder
.build
```

If you want to use your own `http_client` and wants to use `Reports` module, the `get_report_file` function follows a redirect URL from Checkout,
Ruby SDK uses `faraday-follow-redirects` which is an [open source](https://github.com/tisba/faraday-follow-redirects) solution that came after Faraday 2.0 deprecation,
you must add it otherwise Ruby SDK will not be able to download the contents file, or provide your custom redirect adapter.

```ruby
http_client = Faraday.new do |f|
f.response :follow_redirects
f.response :raise_error
end

api = CheckoutSdk.builder
.static_keys
.with_secret_key('secret_key')
.with_public_key('public_key') # optional, only required for operations related with tokens
.with_environment(CheckoutSdk::Environment.sandbox)
.with_http_client(http_client)
.build
```

## Building from source

Once you check out the code from GitHub, the project can be built using gem:
Expand Down
1 change: 1 addition & 0 deletions checkout_sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ sellers and service providers."
spec.add_development_dependency 'rubocop', '~> 1.36.0'
spec.add_dependency 'faraday', '>= 2.0.0'
spec.add_dependency 'faraday-multipart', '~> 1.0.4'
spec.add_dependency 'faraday-follow_redirects', '~> 0.3.0'
spec.add_dependency 'mime-types', '~> 3.0'
end
1 change: 1 addition & 0 deletions lib/checkout_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'faraday'
require 'faraday/multipart'
require 'faraday/net_http'
require 'faraday/follow_redirects'
require 'mime/types'
require 'logger'

Expand Down
1 change: 1 addition & 0 deletions lib/checkout_sdk/checkout_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def self.build_multipart_client
# @return [Faraday::Connection]
def self.build_default_client
Faraday.new do |f|
f.response :follow_redirects
f.response :raise_error
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/checkout_sdk/reports/reports_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def get_all_reports(query_filter)
def get_report_details(report_id)
api_client.invoke_get(build_path(REPORTS, report_id), sdk_authorization)
end

# @param [String] report_id
# @param [String] file_id
def get_report_file(report_id, file_id)
api_client.invoke_get(build_path(REPORTS, report_id, FILES, file_id), sdk_authorization)
end
end
end
end
17 changes: 17 additions & 0 deletions spec/checkout_sdk/reports/reports_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@
end
end
end

describe '.get_report_file' do
context 'when requesting report file' do
subject(:report) {
response = default_sdk.reports.get_all_reports @query
assert_response response, %w[data]
response.data[0]
}
it 'should retrieve report file contents' do
response = default_sdk.reports.get_report_file report.id, report.files[0].id
expect(response).not_to be_nil
expect(response.contents).not_to be_nil
expect(response.metadata.status_code).to eq 200
end

end
end
end

0 comments on commit dd55b36

Please sign in to comment.