Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/test_and_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ jobs:
strategy:
matrix:
ruby:
- "2.6"
- "2.7"
- "3.0"
- "3.1"
Expand Down
9 changes: 9 additions & 0 deletions Gemfile
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
69 changes: 48 additions & 21 deletions lib/quickpay/api/client.rb
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"

Expand All @@ -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
Expand All @@ -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

Choose a reason for hiding this comment

The 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 Net::HTTPHeader, because otherwise stuff might break when we or externals upgrade to version 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is the reason for the major version change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides changes in underlying client errors

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be 100% sure the headers are always lowercase?

Choose a reason for hiding this comment

The 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
Expand Down
2 changes: 1 addition & 1 deletion lib/quickpay/api/version.rb
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
9 changes: 0 additions & 9 deletions quickpay-ruby-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,5 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler"
spec.add_development_dependency "minitest"
spec.add_development_dependency "pry"
spec.add_development_dependency "rake"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "simplecov-console"

spec.add_dependency "excon", ">= 0.79"
spec.add_dependency "json", "~> 2", ">= 2.5"
end
Loading
Loading