-
Notifications
You must be signed in to change notification settings - Fork 7
Replace excon with Net::HTTP to support Unzer AWS proxy #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ jobs: | |
| strategy: | ||
| matrix: | ||
| ruby: | ||
| - "2.6" | ||
| - "2.7" | ||
| - "3.0" | ||
| - "3.1" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| source "https://rubygems.org" | ||
|
|
||
| gem "bundler" | ||
| gem "minitest" | ||
| gem "pry" | ||
| gem "rake" | ||
| gem "rubocop" | ||
| gem "simplecov" | ||
| gem "simplecov-console" | ||
| gem "webmock" | ||
|
|
||
| # Specify your gem's dependencies in quickpay-ruby-client.gemspec | ||
| gemspec | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| require "excon" | ||
| require "json" | ||
| require "net/http" | ||
| require "quickpay/api/error" | ||
| require "quickpay/api/version" | ||
|
|
||
|
|
@@ -16,20 +16,28 @@ class Client | |
| Request = Struct.new(:method, :path, :body, :headers, :query) # rubocop:disable Lint/StructNewOverride | ||
|
|
||
| def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net", options: {}) | ||
| opts = { | ||
| read_timeout: options.fetch(:read_timeout, 60), | ||
| write_timeout: options.fetch(:write_timeout, 60), | ||
| connect_timeout: options.fetch(:connect_timeout, 60), | ||
| json_opts: options.fetch(:json_opts, nil) | ||
| } | ||
|
|
||
| opts[:username] = Excon::Utils.escape_uri(username) if username | ||
| opts[:password] = Excon::Utils.escape_uri(password) if password | ||
|
|
||
| @connection = Excon.new(base_uri, opts) | ||
| @read_timeout = options.fetch(:read_timeout, 60) | ||
| @write_timeout = options.fetch(:write_timeout, 60) | ||
| @connect_timeout = options.fetch(:connect_timeout, 60) | ||
| @json_opts = options.fetch(:json_opts, nil) | ||
|
|
||
| uri_parser = URI::Parser.new | ||
| @username = uri_parser.escape(username) if username | ||
| @password = uri_parser.escape(password) if password | ||
| @base_uri = base_uri | ||
| end | ||
|
|
||
| %i[get post patch put delete head].each do |method| | ||
| HTTPS = "https".freeze | ||
|
|
||
| [ | ||
| Net::HTTP::Get, | ||
| Net::HTTP::Post, | ||
| Net::HTTP::Patch, | ||
| Net::HTTP::Put, | ||
| Net::HTTP::Delete, | ||
| Net::HTTP::Head | ||
| ].each do |method_class| | ||
| method = method_class.to_s.split("::").last.downcase | ||
| define_method(method) do |path, **options, &block| | ||
| headers = DEFAULT_HEADERS.merge(options.fetch(:headers, {})) | ||
| body = begin | ||
|
|
@@ -42,29 +50,48 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net | |
| end | ||
|
|
||
| req = Request.new( | ||
| method, | ||
| method.to_sym, | ||
| path, | ||
| scrub_body(body.dup, headers["Content-Type"]), | ||
| headers, | ||
| options.fetch(:query, {}) | ||
| options[:query] | ||
| ).freeze | ||
|
|
||
| res = @connection.request(**req.to_h) | ||
| error = QuickPay::API::Error.by_status_code(res.status, res.body, res.headers, req) | ||
| uri = URI(@base_uri) | ||
| uri.path << req.path | ||
| if (query = req.query) && query.any? | ||
| uri.query = URI.encode_www_form(req.query) | ||
| end | ||
| net_req = method_class.new(uri, req.headers) | ||
| net_req.basic_auth(@username, @password) if @username || @password | ||
| net_req.body = req.body | ||
| res = Net::HTTP.start( | ||
| uri.hostname, | ||
| use_ssl: uri.scheme == HTTPS, | ||
| open_timeout: @connect_timeout, | ||
| read_timeout: @read_timeout, | ||
| write_timeout: @write_timeout | ||
| ) do |http| | ||
| http.request(net_req) | ||
| end | ||
| status_code = res.code.to_i | ||
| body = res.body | ||
| headers = res.each_header.to_h | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a pretty big breaking change here, if we can't guarantee the headers "upper/lower-caseness". Perhaps we could consider returning a Hash-like object that behaves like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which is the reason for the major version change.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Besides changes in underlying client errors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't you be sleeping |
||
| error = QuickPay::API::Error.by_status_code(status_code, body, headers, req) | ||
|
|
||
| if !options.fetch(:raw, false) && res.headers["Content-Type"] =~ CONTENT_TYPE_JSON_REGEX | ||
| res.body = JSON.parse(res.body, options[:json_opts] || @connection.data[:json_opts]) | ||
| if !options.fetch(:raw, false) && res["content-type"] =~ CONTENT_TYPE_JSON_REGEX | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we be 100% sure the headers are always lowercase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems so https://github.com/ruby/net-http/blob/master/lib/net/http/header.rb#L40-L47 or at least they're treated as case-insensitive |
||
| body = JSON.parse(body, options[:json_opts] || @json_opts) | ||
| end | ||
|
|
||
| if block | ||
| # Raise error if not specified as fourth block parameter | ||
| raise error if error && block.parameters.size < 4 | ||
|
|
||
| block.call(res.body, res.status, res.headers, error) | ||
| block.call(body, status_code, headers, error) | ||
| else | ||
| raise error if error | ||
|
|
||
| [res.body, res.status, res.headers] | ||
| [body, status_code, headers] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| module QuickPay | ||
| module API | ||
| VERSION = "3.0.2".freeze | ||
| VERSION = "4.0.0".freeze | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.