Skip to content

Commit 2c3c338

Browse files
Fixed support of multiple sites
The new version of the hack is somehow different. Though, it attempts to do the same trick altering the underlying connection, it does a reconnect instead of just patching the URL. As a result, this allows different resources to work with distinct `site`s at the same time. See [related upstream issue]( JsonApiClient/json_api_client#215 ) for details.
2 parents b263b64 + 5d46aa5 commit 2c3c338

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

lib/cirro_io/client/base.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ class Base < JsonApiClient::Resource
44
self.route_format = :dasherized_route
55
self.json_key_format = :dasherized_key
66

7-
# https://github.com/JsonApiClient/json_api_client/issues/215
7+
# HACK: https://github.com/JsonApiClient/json_api_client/issues/215
8+
# Used for initialization as well
89
def self.site=(url)
9-
super(url)
10-
connection.faraday.url_prefix = url
10+
super.tap do
11+
connection true do |connection|
12+
connection.use JwtAuthentication
13+
connection.use Faraday::Response::Logger
14+
# connection.use ResponseDebuggingMiddleware # for debugging or while adding new specs
15+
end
16+
end
1117
end
1218

1319
def self.custom_post(endpoint, payload)
@@ -25,9 +31,3 @@ def self.custom_connection
2531
end
2632
end
2733
end
28-
29-
CirroIO::Client::Base.connection do |connection|
30-
connection.use CirroIO::Client::JwtAuthentication
31-
connection.use Faraday::Response::Logger
32-
# connection.use CirroIO::Client::ResponseDebuggingMiddleware # This middleware can be injected during debugging or while adding new specs
33-
end

spec/cirro_io/client/base_spec.rb

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
RSpec.describe CirroIO::Client::Base do
2-
describe 'jwt_authentication' do
3-
before do
4-
configure_api_client
5-
end
2+
let :request_headers do
3+
{
4+
'Accept' => 'application/vnd.api+json',
5+
'Accept-Encoding' => 'gzip,deflate',
6+
'Content-Type' => 'application/vnd.api+json',
7+
'User-Agent' => 'Faraday v1.1.0',
8+
'Authorization' => 'Bearer jwt-token',
9+
}
10+
end
611

7-
it 'sends correct token' do
8-
allow(JWT).to receive(:encode).and_return('jwt-token')
12+
let :response_headers do
13+
{
14+
'Content-Type' => 'application/json',
15+
}
16+
end
917

18+
before do
19+
configure_api_client
20+
allow(JWT).to receive(:encode).and_return('jwt-token')
21+
end
22+
23+
describe 'jwt_authentication' do
24+
it 'sends correct token' do
1025
stub_request(:get, "#{test_site}/v1/app-workers/1")
11-
.with(headers: {
12-
'Accept' => 'application/vnd.api+json',
13-
'Accept-Encoding' => 'gzip,deflate',
14-
'Content-Type' => 'application/vnd.api+json',
15-
'User-Agent' => 'Faraday v1.1.0',
16-
'Authorization' => 'Bearer jwt-token',
17-
})
18-
.to_return(body: File.read('./spec/fixtures/app_worker.json'), headers: { 'Content-Type' => 'application/json' })
26+
.with(headers: request_headers)
27+
.to_return(body: File.read('./spec/fixtures/app_worker.json'), headers: response_headers)
1928

2029
app_worker = CirroIO::Client::AppWorker.find(1).first
2130

2231
expect(app_worker.id).to eq('1')
2332
end
2433

2534
it 'sends token correctly for custom requests as well' do
26-
allow(JWT).to receive(:encode).and_return('jwt-token')
27-
2835
stub_request(:post, "#{test_site}/v1/bulk/custom-endpoint")
2936
.with(headers: {
3037
'Accept' => '*/*',
@@ -38,4 +45,27 @@
3845
described_class.custom_post('bulk/custom-endpoint', { a: :b })
3946
end
4047
end
48+
49+
describe 'configuration' do
50+
let(:other_site) { 'https://api.other.cirro.io' }
51+
let(:other_version) { 'vXXX' }
52+
53+
before do
54+
CirroIO::Client::AppWorker.site = "#{other_site}/#{other_version}"
55+
end
56+
57+
it 'supports multiple backends' do
58+
stub_request(:get, "#{other_site}/#{other_version}/app-workers/1")
59+
.with(headers: request_headers)
60+
.to_return(body: File.read('spec/fixtures/app_worker.json'), headers: response_headers)
61+
62+
expect(CirroIO::Client::AppWorker.find(1).first.id).to eq('1')
63+
64+
stub_request(:get, "#{test_site}/v1/app-users/3")
65+
.with(headers: request_headers)
66+
.to_return(body: File.read('spec/fixtures/app_user.json'), headers: response_headers)
67+
68+
expect(CirroIO::Client::AppUser.find(3).first.id).to eq('3')
69+
end
70+
end
4171
end

0 commit comments

Comments
 (0)