1- require "excon"
21require "json"
2+ require "net/http"
33require "quickpay/api/error"
44require "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
0 commit comments