gds-api-adapters 0.0.24 → 0.0.26
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.
- data/lib/gds_api/exceptions.rb +3 -0
- data/lib/gds_api/json_client.rb +10 -11
- data/lib/gds_api/version.rb +1 -1
- data/test/json_client_test.rb +22 -31
- metadata +3 -3
data/lib/gds_api/exceptions.rb
CHANGED
data/lib/gds_api/json_client.rb
CHANGED
@@ -5,18 +5,18 @@ require_relative 'version'
|
|
5
5
|
module GdsApi
|
6
6
|
class JsonClient
|
7
7
|
attr_accessor :logger, :options
|
8
|
-
|
8
|
+
|
9
9
|
def initialize(options = {})
|
10
10
|
@logger = options[:logger] || GdsApi::Base.logger
|
11
11
|
@options = options
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
REQUEST_HEADERS = {
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
'Accept' => 'application/json',
|
16
|
+
'Content-Type' => 'application/json',
|
17
|
+
'User-Agent' => "GDS Api Client v. #{GdsApi::VERSION}"
|
18
18
|
}
|
19
|
-
DEFAULT_TIMEOUT_IN_SECONDS =
|
19
|
+
DEFAULT_TIMEOUT_IN_SECONDS = 2
|
20
20
|
|
21
21
|
def get_json(url)
|
22
22
|
do_request(Net::HTTP::Get, url)
|
@@ -29,9 +29,8 @@ module GdsApi
|
|
29
29
|
def put_json(url, params)
|
30
30
|
do_request(Net::HTTP::Put, url, params)
|
31
31
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
|
33
|
+
private
|
35
34
|
def do_request(method_class, url, params = nil)
|
36
35
|
loggable = {request_uri: url, start_time: Time.now.to_f}
|
37
36
|
start_logging = loggable.merge(action: 'start')
|
@@ -41,7 +40,7 @@ module GdsApi
|
|
41
40
|
path = url.path
|
42
41
|
path = path + "?" + url.query if url.query
|
43
42
|
|
44
|
-
response = Net::HTTP.start(url.host, url.port, nil, nil, nil, nil, {use_ssl: url.port == 443, verify_mode: (OpenSSL::SSL::VERIFY_NONE if url.port == 443)
|
43
|
+
response = Net::HTTP.start(url.host, url.port, nil, nil, nil, nil, {use_ssl: url.port == 443, verify_mode: (OpenSSL::SSL::VERIFY_NONE if url.port == 443)}) do |http|
|
45
44
|
http.read_timeout = options[:timeout] || DEFAULT_TIMEOUT_IN_SECONDS
|
46
45
|
request = method_class.new(path, REQUEST_HEADERS)
|
47
46
|
request.basic_auth(@options[:basic_auth][:user], @options[:basic_auth][:password]) if @options[:basic_auth]
|
@@ -67,7 +66,7 @@ module GdsApi
|
|
67
66
|
raise GdsApi::EndpointNotFound.new("Could not connect to #{url}")
|
68
67
|
rescue Timeout::Error, Errno::ECONNRESET => e
|
69
68
|
logger.error loggable.merge(status: 'failed', end_time: Time.now.to_f).to_json
|
70
|
-
|
69
|
+
raise GdsApi::TimedOutException.new
|
71
70
|
end
|
72
71
|
end
|
73
72
|
end
|
data/lib/gds_api/version.rb
CHANGED
data/test/json_client_test.rb
CHANGED
@@ -1,35 +1,25 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
require 'gds_api/base'
|
3
3
|
require 'gds_api/json_client'
|
4
|
-
require 'rack'
|
5
4
|
require 'base64'
|
6
5
|
|
7
|
-
StubRackApp = lambda do |env|
|
8
|
-
sleep(30)
|
9
|
-
body = '{"some":"value"}'
|
10
|
-
[200, {"Content-Type" => "text/plain", "Content-Length" => body.length.to_s}, [body]]
|
11
|
-
end
|
12
|
-
|
13
6
|
class JsonClientTest < MiniTest::Spec
|
14
7
|
def setup
|
15
8
|
@client = GdsApi::JsonClient.new
|
16
9
|
end
|
17
10
|
|
18
|
-
def options;
|
19
|
-
|
11
|
+
def options;
|
12
|
+
{}
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_long_get_requests_timeout
|
20
16
|
url = "http://www.example.com/timeout.json"
|
21
|
-
stub_request(:get, url).
|
22
|
-
assert_raises GdsApi::
|
17
|
+
stub_request(:get, url).to_timeout
|
18
|
+
assert_raises GdsApi::TimedOutException do
|
23
19
|
@client.get_json(url)
|
24
20
|
end
|
25
21
|
end
|
26
22
|
|
27
|
-
def test_get_returns_nil_on_timeout
|
28
|
-
url = "http://some.endpoint/some.json"
|
29
|
-
stub_request(:get, url).to_raise(Timeout::Error)
|
30
|
-
assert_nil @client.get_json(url)
|
31
|
-
end
|
32
|
-
|
33
23
|
def test_get_should_raise_endpoint_not_found_if_connection_refused
|
34
24
|
url = "http://some.endpoint/some.json"
|
35
25
|
stub_request(:get, url).to_raise(Errno::ECONNREFUSED)
|
@@ -46,15 +36,17 @@ class JsonClientTest < MiniTest::Spec
|
|
46
36
|
end
|
47
37
|
end
|
48
38
|
|
49
|
-
def
|
39
|
+
def test_post_requests_timeout
|
50
40
|
url = "http://some.endpoint/some.json"
|
51
|
-
stub_request(:post, url).
|
52
|
-
|
41
|
+
stub_request(:post, url).to_timeout
|
42
|
+
assert_raises GdsApi::TimedOutException do
|
43
|
+
@client.post_json(url, {})
|
44
|
+
end
|
53
45
|
end
|
54
46
|
|
55
47
|
def test_should_fetch_and_parse_json_into_response
|
56
48
|
url = "http://some.endpoint/some.json"
|
57
|
-
stub_request(:get, url).to_return(:body => "{}"
|
49
|
+
stub_request(:get, url).to_return(:body => "{}", :status => 200)
|
58
50
|
assert_equal GdsApi::Response, @client.get_json(url).class
|
59
51
|
end
|
60
52
|
|
@@ -63,30 +55,30 @@ class JsonClientTest < MiniTest::Spec
|
|
63
55
|
stub_request(:get, url).to_return(:body => "{}", :status => 404)
|
64
56
|
assert_nil @client.get_json(url)
|
65
57
|
end
|
66
|
-
|
58
|
+
|
67
59
|
def empty_response
|
68
60
|
net_http_response = stub(:body => '{}')
|
69
61
|
GdsApi::Response.new(net_http_response)
|
70
62
|
end
|
71
|
-
|
63
|
+
|
72
64
|
def test_put_json_does_put_with_json_encoded_packet
|
73
65
|
url = "http://some.endpoint/some.json"
|
74
|
-
payload = {a:1}
|
66
|
+
payload = {a: 1}
|
75
67
|
stub_request(:put, url).with(body: payload.to_json).to_return(:body => "{}", :status => 200)
|
76
68
|
assert_equal({}, @client.put_json(url, payload).to_hash)
|
77
69
|
end
|
78
|
-
|
70
|
+
|
79
71
|
def test_can_convert_response_to_ostruct
|
80
72
|
url = "http://some.endpoint/some.json"
|
81
|
-
payload = {a:1}
|
73
|
+
payload = {a: 1}
|
82
74
|
stub_request(:put, url).with(body: payload.to_json).to_return(:body => '{"a":1}', :status => 200)
|
83
75
|
response = @client.put_json(url, payload)
|
84
|
-
assert_equal(OpenStruct.new(a:1), response.to_ostruct)
|
76
|
+
assert_equal(OpenStruct.new(a: 1), response.to_ostruct)
|
85
77
|
end
|
86
|
-
|
78
|
+
|
87
79
|
def test_can_access_attributes_of_response_directly
|
88
80
|
url = "http://some.endpoint/some.json"
|
89
|
-
payload = {a:1}
|
81
|
+
payload = {a: 1}
|
90
82
|
stub_request(:put, url).with(body: payload.to_json).to_return(:body => '{"a":{"b":2}}', :status => 200)
|
91
83
|
response = @client.put_json(url, payload)
|
92
84
|
assert_equal 2, response.a.b
|
@@ -95,8 +87,7 @@ class JsonClientTest < MiniTest::Spec
|
|
95
87
|
def test_client_can_use_basic_auth
|
96
88
|
client = GdsApi::JsonClient.new(basic_auth: {user: 'user', password: 'password'})
|
97
89
|
|
98
|
-
stub_request(:put, "http://user:password@some.endpoint/some.json")
|
99
|
-
.to_return(:body => '{"a":1}', :status => 200)
|
90
|
+
stub_request(:put, "http://user:password@some.endpoint/some.json").to_return(:body => '{"a":1}', :status => 200)
|
100
91
|
response = client.put_json("http://some.endpoint/some.json", {})
|
101
92
|
assert_equal 1, response.a
|
102
93
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: gds-api-adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.26
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Stewart
|
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
160
|
requirements:
|
161
161
|
- - ">="
|
162
162
|
- !ruby/object:Gem::Version
|
163
|
-
hash: -
|
163
|
+
hash: -3364287350249021531
|
164
164
|
segments:
|
165
165
|
- 0
|
166
166
|
version: "0"
|
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
169
|
requirements:
|
170
170
|
- - ">="
|
171
171
|
- !ruby/object:Gem::Version
|
172
|
-
hash: -
|
172
|
+
hash: -3364287350249021531
|
173
173
|
segments:
|
174
174
|
- 0
|
175
175
|
version: "0"
|