Skip to content

Commit b8ee978

Browse files
authored
Merge pull request #48 from QuickPay/replace-excon
Replace excon with Net::HTTP to support Unzer AWS proxy
2 parents 3c7b12e + 3fce304 commit b8ee978

File tree

6 files changed

+107
-177
lines changed

6 files changed

+107
-177
lines changed

.github/workflows/test_and_lint.yml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ jobs:
88
strategy:
99
matrix:
1010
ruby:
11-
- "2.6"
1211
- "2.7"
1312
- "3.0"
1413
- "3.1"

Gemfile

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
source "https://rubygems.org"
22

3+
gem "bundler"
4+
gem "minitest"
5+
gem "pry"
6+
gem "rake"
7+
gem "rubocop"
8+
gem "simplecov"
9+
gem "simplecov-console"
10+
gem "webmock"
11+
312
# Specify your gem's dependencies in quickpay-ruby-client.gemspec
413
gemspec

lib/quickpay/api/client.rb

+48-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require "excon"
21
require "json"
2+
require "net/http"
33
require "quickpay/api/error"
44
require "quickpay/api/version"
55

@@ -16,20 +16,28 @@ class Client
1616
Request = Struct.new(:method, :path, :body, :headers, :query) # rubocop:disable Lint/StructNewOverride
1717

1818
def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net", options: {})
19-
opts = {
20-
read_timeout: options.fetch(:read_timeout, 60),
21-
write_timeout: options.fetch(:write_timeout, 60),
22-
connect_timeout: options.fetch(:connect_timeout, 60),
23-
json_opts: options.fetch(:json_opts, nil)
24-
}
25-
26-
opts[:username] = Excon::Utils.escape_uri(username) if username
27-
opts[:password] = Excon::Utils.escape_uri(password) if password
28-
29-
@connection = Excon.new(base_uri, opts)
19+
@read_timeout = options.fetch(:read_timeout, 60)
20+
@write_timeout = options.fetch(:write_timeout, 60)
21+
@connect_timeout = options.fetch(:connect_timeout, 60)
22+
@json_opts = options.fetch(:json_opts, nil)
23+
24+
uri_parser = URI::Parser.new
25+
@username = uri_parser.escape(username) if username
26+
@password = uri_parser.escape(password) if password
27+
@base_uri = base_uri
3028
end
3129

32-
%i[get post patch put delete head].each do |method|
30+
HTTPS = "https".freeze
31+
32+
[
33+
Net::HTTP::Get,
34+
Net::HTTP::Post,
35+
Net::HTTP::Patch,
36+
Net::HTTP::Put,
37+
Net::HTTP::Delete,
38+
Net::HTTP::Head
39+
].each do |method_class|
40+
method = method_class.to_s.split("::").last.downcase
3341
define_method(method) do |path, **options, &block|
3442
headers = DEFAULT_HEADERS.merge(options.fetch(:headers, {}))
3543
body = begin
@@ -42,29 +50,48 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net
4250
end
4351

4452
req = Request.new(
45-
method,
53+
method.to_sym,
4654
path,
4755
scrub_body(body.dup, headers["Content-Type"]),
4856
headers,
49-
options.fetch(:query, {})
57+
options[:query]
5058
).freeze
5159

52-
res = @connection.request(**req.to_h)
53-
error = QuickPay::API::Error.by_status_code(res.status, res.body, res.headers, req)
60+
uri = URI(@base_uri)
61+
uri.path << req.path
62+
if (query = req.query) && query.any?
63+
uri.query = URI.encode_www_form(req.query)
64+
end
65+
net_req = method_class.new(uri, req.headers)
66+
net_req.basic_auth(@username, @password) if @username || @password
67+
net_req.body = req.body
68+
res = Net::HTTP.start(
69+
uri.hostname,
70+
use_ssl: uri.scheme == HTTPS,
71+
open_timeout: @connect_timeout,
72+
read_timeout: @read_timeout,
73+
write_timeout: @write_timeout
74+
) do |http|
75+
http.request(net_req)
76+
end
77+
status_code = res.code.to_i
78+
body = res.body
79+
headers = res.each_header.to_h
80+
error = QuickPay::API::Error.by_status_code(status_code, body, headers, req)
5481

55-
if !options.fetch(:raw, false) && res.headers["Content-Type"] =~ CONTENT_TYPE_JSON_REGEX
56-
res.body = JSON.parse(res.body, options[:json_opts] || @connection.data[:json_opts])
82+
if !options.fetch(:raw, false) && res["content-type"] =~ CONTENT_TYPE_JSON_REGEX
83+
body = JSON.parse(body, options[:json_opts] || @json_opts)
5784
end
5885

5986
if block
6087
# Raise error if not specified as fourth block parameter
6188
raise error if error && block.parameters.size < 4
6289

63-
block.call(res.body, res.status, res.headers, error)
90+
block.call(body, status_code, headers, error)
6491
else
6592
raise error if error
6693

67-
[res.body, res.status, res.headers]
94+
[body, status_code, headers]
6895
end
6996
end
7097
end

lib/quickpay/api/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module QuickPay
22
module API
3-
VERSION = "3.0.2".freeze
3+
VERSION = "4.0.0".freeze
44
end
55
end

quickpay-ruby-client.gemspec

-9
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,5 @@ Gem::Specification.new do |spec|
1919
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2020
spec.require_paths = ["lib"]
2121

22-
spec.add_development_dependency "bundler"
23-
spec.add_development_dependency "minitest"
24-
spec.add_development_dependency "pry"
25-
spec.add_development_dependency "rake"
26-
spec.add_development_dependency "rubocop"
27-
spec.add_development_dependency "simplecov"
28-
spec.add_development_dependency "simplecov-console"
29-
30-
spec.add_dependency "excon", ">= 0.79"
3122
spec.add_dependency "json", "~> 2", ">= 2.5"
3223
end

0 commit comments

Comments
 (0)