rb-net_http-client 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/client/client.rb +10 -14
- data/lib/client/ext.rb +39 -31
- data/lib/client/schema.rb +21 -8
- data/lib/core/core.rb +16 -5
- data/lib/core/utilities.rb +57 -37
- data/lib/request/request.rb +51 -24
- data/lib/request/schema.rb +7 -0
- data/lib/response/response.rb +48 -30
- data/lib/version.rb +1 -1
- data/spec/unit/client/client_spec.rb +47 -0
- data/spec/unit/client/ext_spec.rb +90 -0
- data/spec/unit/core/core_spec.rb +47 -0
- data/spec/unit/core/utilities_spec.rb +230 -0
- data/spec/unit/request/request_spec.rb +275 -0
- data/spec/unit/response/response_spec.rb +39 -0
- data/spec/{integration/net_http → unit}/version_spec.rb +1 -1
- metadata +9 -17
- data/lib/response/schema.rb +0 -11
- data/spec/integration/net_http/client/client_ext_spec.rb +0 -7
- data/spec/integration/net_http/client/client_spec.rb +0 -7
- data/spec/integration/net_http/net_http_core_spec.rb +0 -18
- data/spec/integration/net_http/request/request_ext_spec.rb +0 -342
- data/spec/integration/net_http/response/response_ext_spec.rb +0 -7
- data/spec/unit/net_http/client/client_ext_spec.rb +0 -7
- data/spec/unit/net_http/client/client_schema_spec.rb +0 -8
- data/spec/unit/net_http/client/client_spec.rb +0 -232
- data/spec/unit/net_http/net_http_core_spec.rb +0 -433
- data/spec/unit/net_http/request/request_ext_spec.rb +0 -603
- data/spec/unit/net_http/request/request_schema_spec.rb +0 -8
- data/spec/unit/net_http/response/response_ext_spec.rb +0 -97
- data/spec/unit/net_http/version_spec.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d6b584195ee54303f03e597cedf0863b804496d00fe7dc23e1c95a72685d27e
|
4
|
+
data.tar.gz: bd2031391105c97823c38fe5422b497617e50bcbe77d0fe82138b191a6a8dd6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a1d80d3519cdd9b8ba725906ae951c70f70ed67a5f992650e66b0b3d31707eecc1608a03a241cb6c15aaf683e2ca7be4dcf814bdc7284aec53d16d2097b3617
|
7
|
+
data.tar.gz: 1519c8ecfaa3075ffa1db0ede1467c0b465c851464c721c118d25516656ff2abc63c67f3448fbdc5a23f3d668efe04d1ef660cf078cb4ab6546fa33fcc8c7b50
|
data/lib/client/client.rb
CHANGED
@@ -12,19 +12,18 @@ module NetHTTP
|
|
12
12
|
attr_reader :ca_file,
|
13
13
|
:cert,
|
14
14
|
:client,
|
15
|
-
:host,
|
16
15
|
:key,
|
17
16
|
:logger,
|
18
|
-
:
|
17
|
+
:open_timeout,
|
19
18
|
:pkcs12,
|
20
|
-
:port,
|
21
19
|
:proxy_uri,
|
22
|
-
:
|
20
|
+
:proxy_url,
|
21
|
+
:query,
|
23
22
|
:read_timeout,
|
24
23
|
:response,
|
25
|
-
:scheme,
|
26
24
|
:ssl_path,
|
27
25
|
:uri,
|
26
|
+
:url,
|
28
27
|
:use_proxy,
|
29
28
|
:use_ssl,
|
30
29
|
:verify_mode
|
@@ -32,19 +31,14 @@ module NetHTTP
|
|
32
31
|
def initialize(opts = {})
|
33
32
|
send('logger=', opts[:logger])
|
34
33
|
Core.schema_validation(opts, NetHTTP::Client::Schema, logger) unless opts[:enforce_schema_validation] == false
|
35
|
-
send('uri=',
|
34
|
+
send('uri=', opts)
|
36
35
|
send('client=', opts)
|
37
36
|
end
|
38
37
|
|
39
38
|
def client=(opts = {})
|
40
39
|
@client = Net::HTTP.new(uri.host, uri.port)
|
41
40
|
|
42
|
-
send('
|
43
|
-
send('host=', uri.host)
|
44
|
-
send('port=', uri.port)
|
45
|
-
send('path=', uri.path)
|
46
|
-
send('proxy_uri=', (opts[:proxy_uri] ||= opts[:proxy_url]))
|
47
|
-
send('use_proxy=')
|
41
|
+
send('proxy_uri=', opts)
|
48
42
|
send('open_timeout=', (opts[:open_timeout] ||= 60))
|
49
43
|
send('read_timeout=', (opts[:read_timeout] ||= 60))
|
50
44
|
send('use_ssl=', opts[:use_ssl], uri.scheme)
|
@@ -68,12 +62,14 @@ module NetHTTP
|
|
68
62
|
end
|
69
63
|
end
|
70
64
|
|
71
|
-
private :
|
72
|
-
:response
|
65
|
+
private :response
|
73
66
|
|
74
67
|
def logger=(logger = nil)
|
75
68
|
@logger = Core.get_logger(logger)
|
76
69
|
end
|
70
|
+
|
71
|
+
alias proxy_url proxy_uri
|
72
|
+
alias url uri
|
77
73
|
end
|
78
74
|
|
79
75
|
module_function
|
data/lib/client/ext.rb
CHANGED
@@ -2,40 +2,48 @@
|
|
2
2
|
|
3
3
|
module NetHTTP
|
4
4
|
class Client
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@use_proxy = true
|
5
|
+
def proxy_uri=(opts = {})
|
6
|
+
proxy_uri = opts[:proxy_uri] ||= opts[:proxy_url] ||= ENV['http_proxy'] ||= ENV['HTTP_PROXY'] ||= ENV['https_proxy'] ||= ENV['HTTPS_PROXY']
|
7
|
+
|
8
|
+
begin
|
9
|
+
if proxy_uri.to_s.empty? == false
|
10
|
+
@proxy_uri = Core::Utilities.parse_uri(proxy_uri)
|
11
|
+
else
|
12
|
+
@proxy_uri = Core::Utilities.parse_uri(
|
13
|
+
Core::Utilities.construct_uri(
|
14
|
+
scheme: opts[:proxy_uri_scheme],
|
15
|
+
host: opts[:proxy_uri_host],
|
16
|
+
port: opts[:proxy_uri_port],
|
17
|
+
path: opts[:proxy_uri_path],
|
18
|
+
query: opts[:proxy_uri_query]
|
19
|
+
)
|
20
|
+
)
|
21
|
+
end
|
22
|
+
rescue RuntimeError => error
|
23
|
+
logger.debug(error)
|
24
|
+
@proxy_uri = nil
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
28
|
+
def uri=(opts = {})
|
29
|
+
uri = opts[:uri] ||= opts[:url]
|
30
|
+
begin
|
31
|
+
if uri.to_s.empty? == false
|
32
|
+
@uri = Core::Utilities.parse_uri(uri)
|
33
|
+
else
|
34
|
+
@uri = Core::Utilities.parse_uri(
|
35
|
+
Core::Utilities.construct_uri(
|
36
|
+
scheme: opts[:scheme],
|
37
|
+
host: opts[:host],
|
38
|
+
port: opts[:port],
|
39
|
+
path: opts[:path],
|
40
|
+
query: opts[:query]
|
41
|
+
)
|
42
|
+
)
|
43
|
+
end
|
44
|
+
rescue RuntimeError => error
|
45
|
+
logger.debug(error)
|
46
|
+
@uri = nil
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
data/lib/client/schema.rb
CHANGED
@@ -6,24 +6,37 @@ module NetHTTP
|
|
6
6
|
class Client
|
7
7
|
Schema = Dry::Validation.Schema do
|
8
8
|
configure do
|
9
|
+
def name
|
10
|
+
'NetHTTP::Client::Schema'
|
11
|
+
end
|
12
|
+
|
9
13
|
def true_or_false
|
10
14
|
[true, false]
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
14
|
-
optional(:
|
15
|
-
optional(:uri).maybe(type?: String)
|
16
|
-
optional(:url).maybe(type?: String)
|
17
|
-
optional(:scheme).maybe(type?: String)
|
18
|
+
optional(:ca_file).filled(type?: String)
|
18
19
|
optional(:host).maybe(type?: String)
|
19
|
-
optional(:
|
20
|
+
optional(:logger).filled
|
21
|
+
optional(:open_timeout).filled(type?: Integer)
|
20
22
|
optional(:path).maybe(type?: String)
|
23
|
+
optional(:port).maybe(type?: Integer)
|
24
|
+
optional(:pkcs12_file).filled(type?: String)
|
25
|
+
optional(:pkcs12_passphrase).filled(type?: String)
|
26
|
+
optional(:proxy_uri_host).maybe(type?: String)
|
27
|
+
optional(:proxy_uri_path).maybe(type?: String)
|
28
|
+
optional(:proxy_uri_port).maybe(type?: Integer)
|
29
|
+
optional(:proxy_uri_scheme).maybe(type?: String)
|
21
30
|
optional(:proxy_uri).filled(type?: String)
|
22
31
|
optional(:proxy_url).filled(type?: String)
|
23
|
-
optional(:
|
24
|
-
optional(:use_ssl).filled(included_in?: true_or_false)
|
25
|
-
optional(:open_timeout).filled(type?: Integer)
|
32
|
+
optional(:query).filled(type?: String)
|
26
33
|
optional(:read_timeout).filled(type?: Integer)
|
34
|
+
optional(:scheme).maybe(type?: String)
|
35
|
+
optional(:ssl_path).filled(type?: String)
|
36
|
+
optional(:uri).maybe(type?: String)
|
37
|
+
optional(:url).maybe(type?: String)
|
38
|
+
optional(:use_ssl).filled(included_in?: true_or_false)
|
39
|
+
optional(:verify_mode).filled
|
27
40
|
|
28
41
|
rule(if_url_and_uri_are_nil_must_provide_host: [:uri, :url, :host]) do |uri, url, host|
|
29
42
|
uri.empty?.then(url.empty?.then(host.filled?))
|
data/lib/core/core.rb
CHANGED
@@ -16,11 +16,22 @@ module NetHTTP
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.schema_validation(opts, schema, logger)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
begin
|
20
|
+
results = schema.call(opts)
|
21
|
+
return if results.success?
|
22
|
+
begin
|
23
|
+
schema_name = schema.name
|
24
|
+
logger.debug("NetHTTP::Core::SchemaValidationError - #{schema_name} input validation failed due to => #{results.messages}")
|
25
|
+
raise("NetHTTP::Core::SchemaValidationError - #{schema_name} input validation failed due to => #{results.messages}")
|
26
|
+
rescue RuntimeError => error
|
27
|
+
logger.debug("NetHTTP::Core::SchemaValidationError - Dry::Validation::Schema input validation failed due to => #{results.messages}")
|
28
|
+
raise("NetHTTP::Core::SchemaValidationError - Dry::Validation::Schema input validation failed due to => #{results.messages}")
|
29
|
+
end
|
30
|
+
rescue RuntimeError => error
|
31
|
+
logger.debug(error)
|
32
|
+
logger.debug("NetHTTP::Core::SchemaValidationError - Dry::Validation::Schema input validation failed.")
|
33
|
+
raise error
|
34
|
+
end
|
24
35
|
end
|
25
36
|
end
|
26
37
|
end
|
data/lib/core/utilities.rb
CHANGED
@@ -9,38 +9,55 @@ module NetHTTP
|
|
9
9
|
class Core
|
10
10
|
class Utilities
|
11
11
|
def self.construct_uri(uri = {})
|
12
|
-
return nil if uri
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
return nil if uri.empty?
|
13
|
+
return nil if uri[:host].to_s.empty?
|
14
|
+
return nil if uri[:scheme].to_s.empty? && uri[:port].to_s.empty?
|
15
|
+
|
16
|
+
scheme = uri[:scheme]
|
17
|
+
if scheme.to_s.empty?
|
18
|
+
case uri[:port]
|
19
|
+
when 80, '80'
|
20
|
+
scheme = 'http'
|
21
|
+
else
|
22
|
+
scheme = 'https'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
port = uri[:port]
|
27
|
+
if port.to_s.empty?
|
28
|
+
case uri[:scheme]
|
29
|
+
when 'https'
|
30
|
+
port = 443
|
31
|
+
when 'http'
|
32
|
+
port = 80
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
scheme = "#{scheme}://"
|
37
|
+
host = uri[:host]
|
38
|
+
port = ":#{port}"
|
39
|
+
path = uri[:path]
|
40
|
+
query = "?#{uri[:query]}"
|
41
|
+
|
42
|
+
scheme = nil if scheme.to_s.empty?
|
43
|
+
port = nil if port.to_s.empty?
|
44
|
+
path = nil if uri[:path].to_s.empty?
|
45
|
+
query = nil if uri[:query].to_s.empty?
|
46
|
+
|
47
|
+
URI("#{scheme}#{host}#{port}#{path}#{query}").to_s
|
24
48
|
end
|
25
49
|
|
26
50
|
def self.parse_uri(uri)
|
27
51
|
return if uri.nil?
|
28
52
|
return if uri.to_s.empty?
|
29
53
|
|
30
|
-
|
31
|
-
|
32
|
-
scheme
|
33
|
-
port = parsed_uri.to_s.scan(%r{:(\d+)\/}).flatten[0] ||= parsed_uri.port
|
54
|
+
scheme = uri.to_s.scan(%r{([a-z][a-z0-9+\-.]*)://}).flatten[0].to_s
|
55
|
+
return URI(uri) if scheme.downcase == 'http'
|
56
|
+
return URI(uri) if scheme.downcase == 'https'
|
34
57
|
|
35
|
-
|
36
|
-
return URI(
|
37
|
-
|
38
|
-
case port
|
39
|
-
when 80, '80'
|
40
|
-
URI("http://#{parsed_uri.to_s.gsub("#{scheme}://", '')}")
|
41
|
-
else
|
42
|
-
URI("https://#{parsed_uri.to_s.gsub("#{scheme}://", '')}")
|
43
|
-
end
|
58
|
+
port = uri.to_s.scan(%r{:([0-9]+)}).flatten[0].to_s
|
59
|
+
return URI("http://#{uri.to_s.gsub("#{scheme}://", '')}") if port == '80'
|
60
|
+
URI("https://#{uri.to_s.gsub("#{scheme}://", '')}")
|
44
61
|
end
|
45
62
|
|
46
63
|
# CamelCase to snake_case
|
@@ -128,8 +145,8 @@ module NetHTTP
|
|
128
145
|
format: 'snake',
|
129
146
|
type: type.downcase
|
130
147
|
)
|
131
|
-
rescue RuntimeError =>
|
132
|
-
raise
|
148
|
+
rescue RuntimeError => error
|
149
|
+
raise error
|
133
150
|
end
|
134
151
|
end
|
135
152
|
|
@@ -152,8 +169,8 @@ module NetHTTP
|
|
152
169
|
format: 'snake',
|
153
170
|
type: type.downcase
|
154
171
|
)
|
155
|
-
rescue RuntimeError =>
|
156
|
-
raise
|
172
|
+
rescue RuntimeError => error
|
173
|
+
raise error
|
157
174
|
end
|
158
175
|
end
|
159
176
|
|
@@ -196,8 +213,8 @@ module NetHTTP
|
|
196
213
|
format: 'snake',
|
197
214
|
type: type.downcase
|
198
215
|
)
|
199
|
-
rescue RuntimeError =>
|
200
|
-
raise
|
216
|
+
rescue RuntimeError => error
|
217
|
+
raise error
|
201
218
|
end
|
202
219
|
end
|
203
220
|
|
@@ -207,13 +224,13 @@ module NetHTTP
|
|
207
224
|
return false if json_doc.empty?
|
208
225
|
|
209
226
|
JSON.parse(json_doc)
|
210
|
-
rescue JSON::ParserError =>
|
227
|
+
rescue JSON::ParserError => error
|
211
228
|
if logger.nil? || logger.to_s.empty?
|
212
229
|
puts 'WARNING - JSON syntax / parsing errors detected:'
|
213
|
-
puts
|
230
|
+
puts error
|
214
231
|
else
|
215
232
|
logger.debug('WARNING - JSON syntax / parsing errors detected:')
|
216
|
-
logger.debug(
|
233
|
+
logger.debug(error)
|
217
234
|
end
|
218
235
|
return false
|
219
236
|
end
|
@@ -253,9 +270,12 @@ module NetHTTP
|
|
253
270
|
return false unless html_doc.include?('<body>')
|
254
271
|
return false unless html_doc.include?('</body>')
|
255
272
|
return false unless Nokogiri::HTML(html_doc).errors.empty?
|
273
|
+
return false unless Nokogiri::XML(html_doc).errors.empty?
|
256
274
|
|
257
275
|
begin
|
258
|
-
parse_errors = Nokogiri::HTML.parse(html_doc).validate
|
276
|
+
# parse_errors = Nokogiri::HTML.parse(html_doc).validate
|
277
|
+
parse_errors = Nokogiri::XML(html_doc).errors { |c| c.options = Nokogiri::XML::ParseOptions::STRICT }
|
278
|
+
Nokogiri::XML(html_doc) { |c| c.options = Nokogiri::XML::ParseOptions::STRICT }
|
259
279
|
rescue Nokogiri::XML::SyntaxError
|
260
280
|
if logger.nil? || logger.to_s.empty?
|
261
281
|
puts 'WARNING - HTML syntax / parsing errors detected:'
|
@@ -266,8 +286,8 @@ module NetHTTP
|
|
266
286
|
end
|
267
287
|
return true
|
268
288
|
end
|
269
|
-
rescue RuntimeError =>
|
270
|
-
raise
|
289
|
+
rescue RuntimeError => error
|
290
|
+
raise error
|
271
291
|
end
|
272
292
|
|
273
293
|
true
|
data/lib/request/request.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative 'schema'
|
4
4
|
require_relative '../core/core'
|
5
|
+
require_relative '../response/response'
|
5
6
|
|
6
7
|
module NetHTTP
|
7
8
|
class Client
|
@@ -30,11 +31,11 @@ module NetHTTP
|
|
30
31
|
|
31
32
|
opts[:method] = 'delete'
|
32
33
|
request_opts = request_opts(opts)
|
33
|
-
logger.debug('Request Method => ' + request_opts[:method])
|
34
|
-
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
|
34
|
+
logger.debug('Request Method => ' + request_opts[:method].to_s.upcase)
|
35
|
+
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s)
|
35
36
|
logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
|
36
37
|
logger.debug('Request Path => ' + request_opts[:path])
|
37
|
-
Response.new(
|
38
|
+
NetHTTP::Response.new(
|
38
39
|
response: client.delete(
|
39
40
|
request_opts[:path]
|
40
41
|
),
|
@@ -47,13 +48,13 @@ module NetHTTP
|
|
47
48
|
|
48
49
|
opts[:method] = 'get'
|
49
50
|
request_opts = request_opts(opts)
|
50
|
-
logger.debug('Request Method => ' + request_opts[:method])
|
51
|
-
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
|
51
|
+
logger.debug('Request Method => ' + request_opts[:method].to_s.upcase)
|
52
|
+
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s)
|
52
53
|
logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
|
53
54
|
logger.debug('Request Path => ' + request_opts[:path])
|
54
55
|
logger.debug('Request Headers =>')
|
55
56
|
logger.debug(request_opts[:headers])
|
56
|
-
Response.new(
|
57
|
+
NetHTTP::Response.new(
|
57
58
|
response: client.get(
|
58
59
|
request_opts[:path],
|
59
60
|
request_opts[:headers]
|
@@ -67,15 +68,15 @@ module NetHTTP
|
|
67
68
|
|
68
69
|
opts[:method] = 'post'
|
69
70
|
request_opts = request_opts(opts)
|
70
|
-
logger.debug('Request Method => ' + request_opts[:method])
|
71
|
-
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
|
71
|
+
logger.debug('Request Method => ' + request_opts[:method].to_s.upcase)
|
72
|
+
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s)
|
72
73
|
logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
|
73
74
|
logger.debug('Request Path => ' + request_opts[:path])
|
74
75
|
logger.debug('Request Headers =>')
|
75
76
|
logger.debug(request_opts[:headers])
|
76
77
|
logger.debug('Request Body =>')
|
77
78
|
logger.debug(request_opts[:body])
|
78
|
-
Response.new(
|
79
|
+
NetHTTP::Response.new(
|
79
80
|
response: client.post(
|
80
81
|
request_opts[:path],
|
81
82
|
request_opts[:body],
|
@@ -90,15 +91,15 @@ module NetHTTP
|
|
90
91
|
|
91
92
|
opts[:method] = 'post_form'
|
92
93
|
request_opts = request_opts(opts)
|
93
|
-
logger.debug('Request Method => ' + request_opts[:method])
|
94
|
-
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
|
94
|
+
logger.debug('Request Method => ' + request_opts[:method].to_s.upcase)
|
95
|
+
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s)
|
95
96
|
logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
|
96
97
|
logger.debug('Request Path => ' + request_opts[:path])
|
97
98
|
logger.debug('Request Headers =>')
|
98
99
|
logger.debug(request_opts[:headers])
|
99
100
|
logger.debug('Request Body =>')
|
100
101
|
logger.debug(URI.encode_www_form(request_opts[:body]))
|
101
|
-
Response.new(
|
102
|
+
NetHTTP::Response.new(
|
102
103
|
response: client.post(
|
103
104
|
uri,
|
104
105
|
URI.encode_www_form(request_opts[:body]),
|
@@ -115,15 +116,15 @@ module NetHTTP
|
|
115
116
|
|
116
117
|
opts[:method] = 'put'
|
117
118
|
request_opts = request_opts(opts)
|
118
|
-
logger.debug('Request Method => ' + request_opts[:method])
|
119
|
-
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
|
119
|
+
logger.debug('Request Method => ' + request_opts[:method].to_s.upcase)
|
120
|
+
logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s)
|
120
121
|
logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
|
121
122
|
logger.debug('Request Path => ' + request_opts[:path])
|
122
123
|
logger.debug('Request Headers =>')
|
123
124
|
logger.debug(request_opts[:headers])
|
124
125
|
logger.debug('Request Body =>')
|
125
126
|
logger.debug(request_opts[:body])
|
126
|
-
Response.new(
|
127
|
+
NetHTTP::Response.new(
|
127
128
|
response: client.put(
|
128
129
|
request_opts[:path],
|
129
130
|
request_opts[:body],
|
@@ -133,17 +134,43 @@ module NetHTTP
|
|
133
134
|
)
|
134
135
|
end
|
135
136
|
|
136
|
-
def request_opts(opts
|
137
|
+
def request_opts(opts)
|
137
138
|
Core.schema_validation(opts, NetHTTP::Request::Schema, logger) unless opts[:enforce_schema_validation] == false
|
138
139
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
140
|
+
request_method = opts[:method] ||= 'post'
|
141
|
+
request_uri = Core::Utilities.parse_uri(opts[:uri] || opts[:url] || uri)
|
142
|
+
request_path = request_path(
|
143
|
+
path: (opts[:path] || request_uri.path || path),
|
144
|
+
query: (opts[:query] || request_uri.query || query)
|
145
|
+
)
|
146
|
+
request_headers = opts[:headers] ||= {}
|
147
|
+
request_body = opts[:body] ||= nil
|
148
|
+
|
149
|
+
{
|
150
|
+
method: request_method,
|
151
|
+
uri: request_uri,
|
152
|
+
path: request_path,
|
153
|
+
headers: request_headers,
|
154
|
+
body: request_body
|
155
|
+
}
|
156
|
+
end
|
157
|
+
|
158
|
+
def request_path(opts)
|
159
|
+
# i.e. URI('https://www.google.com')
|
160
|
+
# uri.path defaults to "" if empty / not set
|
161
|
+
# uri.query defaults to nil if empty / not set
|
162
|
+
path = opts[:path].to_s.chomp('?')
|
163
|
+
if opts[:query].to_s.start_with?('?')
|
164
|
+
query = opts[:query][1..-1]
|
165
|
+
else
|
166
|
+
query = opts[:query].to_s
|
167
|
+
end
|
168
|
+
|
169
|
+
return '' if path.empty? && query.empty?
|
170
|
+
return path if query.empty?
|
171
|
+
return '/' + query if path.empty?
|
172
|
+
|
173
|
+
path + '?' + query
|
147
174
|
end
|
148
175
|
end
|
149
176
|
end
|
data/lib/request/schema.rb
CHANGED
@@ -5,9 +5,16 @@ require 'dry-validation'
|
|
5
5
|
module NetHTTP
|
6
6
|
class Request
|
7
7
|
Schema = Dry::Validation.Schema do
|
8
|
+
configure do
|
9
|
+
def name
|
10
|
+
'NetHTTP::Request::Schema'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
8
14
|
required(:method).filled(type?: String)
|
9
15
|
optional(:headers).maybe(type?: Hash)
|
10
16
|
optional(:body).maybe
|
17
|
+
optional(:query).filled(type?: String)
|
11
18
|
|
12
19
|
rule(if_url_and_uri_are_nil_must_provide_path: [:uri, :url, :path]) do |uri, url, path|
|
13
20
|
uri.empty?.then(url.empty?.then(path.filled?))
|