1
- require "excon"
2
1
require "json"
2
+ require "net/http"
3
3
require "quickpay/api/error"
4
4
require "quickpay/api/version"
5
5
@@ -16,20 +16,28 @@ class Client
16
16
Request = Struct . new ( :method , :path , :body , :headers , :query ) # rubocop:disable Lint/StructNewOverride
17
17
18
18
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
30
28
end
31
29
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
33
41
define_method ( method ) do |path , **options , &block |
34
42
headers = DEFAULT_HEADERS . merge ( options . fetch ( :headers , { } ) )
35
43
body = begin
@@ -42,29 +50,48 @@ def initialize(username: nil, password: nil, base_uri: "https://api.quickpay.net
42
50
end
43
51
44
52
req = Request . new (
45
- method ,
53
+ method . to_sym ,
46
54
path ,
47
55
scrub_body ( body . dup , headers [ "Content-Type" ] ) ,
48
56
headers ,
49
- options . fetch ( :query , { } )
57
+ options [ :query ]
50
58
) . freeze
51
59
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 )
54
81
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 )
57
84
end
58
85
59
86
if block
60
87
# Raise error if not specified as fourth block parameter
61
88
raise error if error && block . parameters . size < 4
62
89
63
- block . call ( res . body , res . status , res . headers , error )
90
+ block . call ( body , status_code , headers , error )
64
91
else
65
92
raise error if error
66
93
67
- [ res . body , res . status , res . headers ]
94
+ [ body , status_code , headers ]
68
95
end
69
96
end
70
97
end
0 commit comments