gds-api-adapters 1.7.1 → 1.8.0
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/README.md +14 -0
- data/lib/gds_api/base.rb +1 -5
- data/lib/gds_api/json_client.rb +35 -10
- data/lib/gds_api/test_helpers/publisher.rb +1 -1
- data/lib/gds_api/version.rb +1 -1
- data/test/json_client_test.rb +16 -1
- data/test/licence_application_api_test.rb +5 -5
- data/test/panopticon_api_test.rb +1 -1
- data/test/publisher_api_test.rb +7 -6
- metadata +5 -6
- data/lib/gds_api/oauth2_client.rb +0 -25
data/README.md
CHANGED
@@ -29,6 +29,20 @@ something that actually logs:
|
|
29
29
|
|
30
30
|
GdsApi::Base.logger = Logger.new("/path/to/file.log")
|
31
31
|
|
32
|
+
## Authorization
|
33
|
+
|
34
|
+
The API Adapters currently support either HTTP Basic authentication or OAuth2
|
35
|
+
(bearer token) authorization. This is only used for Panopticon registration at
|
36
|
+
present. The GdsApi::Panopticon::Registerer adapter expects a constant called
|
37
|
+
PANOPTICON_API_CREDENTIALS to be defined and will use that to pass the relevant
|
38
|
+
options to the HTTP client.
|
39
|
+
|
40
|
+
To use bearer token authorization the format that constant should be a hash of
|
41
|
+
the form:
|
42
|
+
|
43
|
+
PANOPTICON_API_CREDENTIALS = { bearer_token: 'MY_BEARER_TOKEN' }
|
44
|
+
|
45
|
+
|
32
46
|
## Test Helpers
|
33
47
|
|
34
48
|
There are also test helpers for stubbing various requests in other apps.
|
data/lib/gds_api/base.rb
CHANGED
@@ -10,11 +10,7 @@ class GdsApi::Base
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def create_client
|
13
|
-
|
14
|
-
GdsApi::OAuth2Client.new(options)
|
15
|
-
else
|
16
|
-
GdsApi::JsonClient.new(options)
|
17
|
-
end
|
13
|
+
GdsApi::JsonClient.new(options)
|
18
14
|
end
|
19
15
|
|
20
16
|
def_delegators :client, :get_json, :get_json!,
|
data/lib/gds_api/json_client.rb
CHANGED
@@ -26,7 +26,7 @@ module GdsApi
|
|
26
26
|
@options = options
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
DEFAULT_REQUEST_HEADERS = {
|
30
30
|
'Accept' => 'application/json',
|
31
31
|
'Content-Type' => 'application/json',
|
32
32
|
'User-Agent' => "GDS Api Client v. #{GdsApi::VERSION}"
|
@@ -96,21 +96,46 @@ module GdsApi
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
+
def extract_url_and_path(url)
|
100
|
+
url = URI.parse(url)
|
101
|
+
path = url.path
|
102
|
+
path = path + "?" + url.query if url.query
|
103
|
+
return url, path
|
104
|
+
end
|
105
|
+
|
106
|
+
def attach_auth_options(request)
|
107
|
+
if @options[:bearer_token]
|
108
|
+
request.add_field('Authorization', "Bearer #{@options[:bearer_token]}")
|
109
|
+
elsif @options[:basic_auth]
|
110
|
+
request.basic_auth(@options[:basic_auth][:user], @options[:basic_auth][:password])
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def set_timeout(http)
|
115
|
+
unless options[:disable_timeout]
|
116
|
+
http.read_timeout = options[:timeout] || DEFAULT_TIMEOUT_IN_SECONDS
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def ssl_options(port)
|
121
|
+
if port == 443
|
122
|
+
{use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE}
|
123
|
+
else
|
124
|
+
{}
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
99
128
|
def do_request(method_class, url, params = nil)
|
100
129
|
loggable = {request_uri: url, start_time: Time.now.to_f}
|
101
130
|
start_logging = loggable.merge(action: 'start')
|
102
131
|
logger.debug start_logging.to_json
|
103
132
|
|
104
|
-
url =
|
105
|
-
path = url.path
|
106
|
-
path = path + "?" + url.query if url.query
|
133
|
+
url, path = extract_url_and_path(url)
|
107
134
|
|
108
|
-
response = Net::HTTP.start(url.host, url.port, nil, nil, nil, nil,
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
request = method_class.new(path, REQUEST_HEADERS)
|
113
|
-
request.basic_auth(@options[:basic_auth][:user], @options[:basic_auth][:password]) if @options[:basic_auth]
|
135
|
+
response = Net::HTTP.start(url.host, url.port, nil, nil, nil, nil, ssl_options(url.port)) do |http|
|
136
|
+
set_timeout(http)
|
137
|
+
request = method_class.new(path, DEFAULT_REQUEST_HEADERS)
|
138
|
+
attach_auth_options(request)
|
114
139
|
request.body = params.to_json if params
|
115
140
|
http.request(request)
|
116
141
|
end
|
@@ -34,7 +34,7 @@ module GdsApi
|
|
34
34
|
slug = input_details.delete('slug')
|
35
35
|
uri = "#{PUBLISHER_ENDPOINT}/local_transactions/#{slug}/verify_snac.json"
|
36
36
|
stub_request(:post, uri).with(:body => JSON.dump(input_details),
|
37
|
-
:headers => GdsApi::JsonClient::
|
37
|
+
:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
38
38
|
to_return(:body => json, :status => 200)
|
39
39
|
end
|
40
40
|
|
data/lib/gds_api/version.rb
CHANGED
data/test/json_client_test.rb
CHANGED
@@ -125,8 +125,23 @@ class JsonClientTest < MiniTest::Spec
|
|
125
125
|
def test_client_can_use_basic_auth
|
126
126
|
client = GdsApi::JsonClient.new(basic_auth: {user: 'user', password: 'password'})
|
127
127
|
|
128
|
-
stub_request(:put, "http://user:password@some.endpoint/some.json").
|
128
|
+
stub_request(:put, "http://user:password@some.endpoint/some.json").
|
129
|
+
to_return(:body => '{"a":1}', :status => 200)
|
130
|
+
|
129
131
|
response = client.put_json("http://some.endpoint/some.json", {})
|
130
132
|
assert_equal 1, response.a
|
131
133
|
end
|
134
|
+
|
135
|
+
def test_client_can_use_bearer_token
|
136
|
+
client = GdsApi::JsonClient.new(bearer_token: 'SOME_BEARER_TOKEN')
|
137
|
+
expected_headers = GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS.
|
138
|
+
merge('Authorization' => 'Bearer SOME_BEARER_TOKEN')
|
139
|
+
|
140
|
+
stub_request(:put, "http://some.other.endpoint/some.json").
|
141
|
+
with(:headers => expected_headers).
|
142
|
+
to_return(:body => '{"a":2}', :status => 200)
|
143
|
+
|
144
|
+
response = client.put_json("http://some.other.endpoint/some.json", {})
|
145
|
+
assert_equal 2, response.a
|
146
|
+
end
|
132
147
|
end
|
@@ -17,7 +17,7 @@ class LicenceApplicationApiTest < MiniTest::Unit::TestCase
|
|
17
17
|
|
18
18
|
def test_should_return_an_error_message_if_licence_is_unrecognised
|
19
19
|
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/bloop").
|
20
|
-
with(headers: GdsApi::JsonClient::
|
20
|
+
with(headers: GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
21
21
|
to_return(status: 404, body: "{\"error\": [\"Unrecognised Licence Id: bloop\"]}")
|
22
22
|
|
23
23
|
assert_equal nil, api.details_for_licence("bloop")
|
@@ -25,7 +25,7 @@ class LicenceApplicationApiTest < MiniTest::Unit::TestCase
|
|
25
25
|
|
26
26
|
def test_should_provide_full_licence_details_for_canonical_id
|
27
27
|
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/590001").
|
28
|
-
with(headers: GdsApi::JsonClient::
|
28
|
+
with(headers: GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
29
29
|
to_return(status: 200, body: "{\"issuingAuthorityType\":\"non-geographical\",\"geographicalAvailability\":\"England\",\"issuingAuthorities\":[{\"authorityName\":\"Authority Name\",\"interactions\":{\"apply\":{\"url\":\"www.gov.uk\",\"usesLicensify\":true,\"description\":\"Custom description\",\"payment\":\"none\",\"paymentAmount\":0}}}]}")
|
30
30
|
|
31
31
|
expected = {
|
@@ -48,7 +48,7 @@ class LicenceApplicationApiTest < MiniTest::Unit::TestCase
|
|
48
48
|
|
49
49
|
def test_should_return_an_error_message_for_bad_snac_code_entry
|
50
50
|
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/590001/bleep").
|
51
|
-
with(headers: GdsApi::JsonClient::
|
51
|
+
with(headers: GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
52
52
|
to_return(status: 404, body: "{\"error\": [\"Unrecognised SNAC: bleep\"]}")
|
53
53
|
|
54
54
|
assert_equal nil, api.details_for_licence("590001", "bleep")
|
@@ -56,7 +56,7 @@ class LicenceApplicationApiTest < MiniTest::Unit::TestCase
|
|
56
56
|
|
57
57
|
def test_should_return_error_messages_for_bad_licence_id_and_snac_code
|
58
58
|
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/bloop/bleep").
|
59
|
-
with(headers: GdsApi::JsonClient::
|
59
|
+
with(headers: GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
60
60
|
to_return(status: 404, body: "{\"error\": [\"Unrecognised Licence Id: bloop\", \"Unrecognised SNAC: bleep\"]}")
|
61
61
|
|
62
62
|
assert_equal nil, api.details_for_licence("bloop", "bleep")
|
@@ -64,7 +64,7 @@ class LicenceApplicationApiTest < MiniTest::Unit::TestCase
|
|
64
64
|
|
65
65
|
def test_should_return_error_message_to_pick_a_relevant_snac_code_for_the_provided_licence_id
|
66
66
|
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/590001/sw10").
|
67
|
-
with(headers: GdsApi::JsonClient::
|
67
|
+
with(headers: GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
68
68
|
to_return(status: 200, body: "{\"error\": [\"Licence not available in the provided snac area\"], \"geographicalAvailability\": [\"Scotland\", \"NI\"]}")
|
69
69
|
|
70
70
|
expected = {
|
data/test/panopticon_api_test.rb
CHANGED
@@ -173,7 +173,7 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
|
|
173
173
|
|
174
174
|
def test_should_be_able_to_fetch_curated_lists
|
175
175
|
stub_request(:get, "#{PANOPTICON_ENDPOINT}/curated_lists.json").
|
176
|
-
with(:headers => GdsApi::JsonClient::
|
176
|
+
with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
177
177
|
to_return(:status => 200, :body => '{ "crume": ["pinishment", "prosin"]}', :headers => {})
|
178
178
|
assert_equal({ "crume" => ["pinishment", "prosin"]}, api.curated_lists)
|
179
179
|
end
|
data/test/publisher_api_test.rb
CHANGED
@@ -5,6 +5,7 @@ require 'gds_api/test_helpers/publisher'
|
|
5
5
|
|
6
6
|
describe GdsApi::Publisher do
|
7
7
|
include GdsApi::TestHelpers::Publisher
|
8
|
+
PUBLISHER_ENDPOINT = GdsApi::TestHelpers::Publisher::PUBLISHER_ENDPOINT
|
8
9
|
|
9
10
|
def basic_answer
|
10
11
|
{
|
@@ -101,7 +102,7 @@ describe GdsApi::Publisher do
|
|
101
102
|
|
102
103
|
it "should be able to retrieve local transaction details" do
|
103
104
|
stub_request(:post, "#{PUBLISHER_ENDPOINT}/local_transactions/fake-transaction/verify_snac.json").
|
104
|
-
with(:body => "{\"snac_codes\":[12345]}", :headers => GdsApi::JsonClient::
|
105
|
+
with(:body => "{\"snac_codes\":[12345]}", :headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
105
106
|
to_return(:status => 200, :body => '{"snac": "12345"}', :headers => {})
|
106
107
|
assert_equal '12345', api.council_for_slug('fake-transaction', [12345])
|
107
108
|
end
|
@@ -141,7 +142,7 @@ describe GdsApi::Publisher do
|
|
141
142
|
|
142
143
|
it "should return nil if a council snac code is not found" do
|
143
144
|
stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_snac?snac=bloop").
|
144
|
-
with(:headers => GdsApi::JsonClient::
|
145
|
+
with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
145
146
|
to_return(:status => 404, :body => " ", :headers => {})
|
146
147
|
|
147
148
|
assert_equal nil, api.council_for_snac_code("bloop")
|
@@ -149,7 +150,7 @@ describe GdsApi::Publisher do
|
|
149
150
|
|
150
151
|
it "should return a council hash for a snac code" do
|
151
152
|
stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_snac?snac=AA00").
|
152
|
-
with(:headers => GdsApi::JsonClient::
|
153
|
+
with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
153
154
|
to_return(:status => 200, :body => '{"name": "Some Council", "snac": "AA00"}', :headers => {})
|
154
155
|
|
155
156
|
expected = {"name" => "Some Council", "snac" => "AA00"}
|
@@ -158,7 +159,7 @@ describe GdsApi::Publisher do
|
|
158
159
|
|
159
160
|
it "should return nil if a council name is not found" do
|
160
161
|
stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_council_name?name=bloop").
|
161
|
-
with(:headers => GdsApi::JsonClient::
|
162
|
+
with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
162
163
|
to_return(:status => 404, :body => " ", :headers => {})
|
163
164
|
|
164
165
|
assert_equal nil, api.council_for_name("bloop")
|
@@ -166,7 +167,7 @@ describe GdsApi::Publisher do
|
|
166
167
|
|
167
168
|
it "should return a council hash for a mixed case council name" do
|
168
169
|
stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_council_name?name=Some%20Council").
|
169
|
-
with(:headers => GdsApi::JsonClient::
|
170
|
+
with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
170
171
|
to_return(:status => 200, :body => '{"name": "Some Council", "snac": "AA00"}', :headers => {})
|
171
172
|
|
172
173
|
expected = {"name" => "Some Council", "snac" => "AA00"}
|
@@ -175,7 +176,7 @@ describe GdsApi::Publisher do
|
|
175
176
|
|
176
177
|
it "should return a council hash for a lowercase council name" do
|
177
178
|
stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_council_name?name=some%20council").
|
178
|
-
with(:headers => GdsApi::JsonClient::
|
179
|
+
with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
|
179
180
|
to_return(:status => 200, :body => '{"name": "Some Council", "snac": "AA00"}', :headers => {})
|
180
181
|
|
181
182
|
expected = {"name" => "Some Council", "snac" => "AA00"}
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: gds-api-adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.8.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Stewart
|
@@ -85,7 +85,7 @@ dependencies:
|
|
85
85
|
requirements:
|
86
86
|
- - ~>
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: 0.
|
88
|
+
version: 0.12.4
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
91
|
version_requirements: *id007
|
@@ -96,7 +96,7 @@ dependencies:
|
|
96
96
|
requirements:
|
97
97
|
- - ~>
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
99
|
+
version: 3.4.0
|
100
100
|
type: :development
|
101
101
|
prerelease: false
|
102
102
|
version_requirements: *id008
|
@@ -167,7 +167,6 @@ files:
|
|
167
167
|
- lib/gds_api/typhoeus_client.rb
|
168
168
|
- lib/gds_api/json_client.rb
|
169
169
|
- lib/gds_api/contactotron.rb
|
170
|
-
- lib/gds_api/oauth2_client.rb
|
171
170
|
- lib/gds_api/response.rb
|
172
171
|
- lib/gds_api/panopticon/registerer.rb
|
173
172
|
- lib/gds_api/publisher.rb
|
@@ -203,7 +202,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
203
202
|
requirements:
|
204
203
|
- - ">="
|
205
204
|
- !ruby/object:Gem::Version
|
206
|
-
hash:
|
205
|
+
hash: 2196406087180137809
|
207
206
|
segments:
|
208
207
|
- 0
|
209
208
|
version: "0"
|
@@ -212,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
212
211
|
requirements:
|
213
212
|
- - ">="
|
214
213
|
- !ruby/object:Gem::Version
|
215
|
-
hash:
|
214
|
+
hash: 2196406087180137809
|
216
215
|
segments:
|
217
216
|
- 0
|
218
217
|
version: "0"
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require_relative 'response'
|
2
|
-
require_relative 'exceptions'
|
3
|
-
|
4
|
-
# TODO: This is a work in progress
|
5
|
-
module GdsApi
|
6
|
-
class OAuth2Client
|
7
|
-
def initialize(options)
|
8
|
-
raise "access_token required" unless options[:access_token]
|
9
|
-
@oauth_client = OAuth2::Client.new(nil, nil)
|
10
|
-
@access_token = OAuth2::AccessToken.new(@oauth_client, options[:access_token], options[:token_options])
|
11
|
-
end
|
12
|
-
|
13
|
-
def get_json(url)
|
14
|
-
@access_token.get(url, headers: REQUEST_HEADERS)
|
15
|
-
end
|
16
|
-
|
17
|
-
def post_json(url, params)
|
18
|
-
@access_token.post(url, body: params, headers: REQUEST_HEADERS)
|
19
|
-
end
|
20
|
-
|
21
|
-
def put_json(url, params)
|
22
|
-
@access_token.put(url, body: params, headers: REQUEST_HEADERS)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|