Skip to content

Commit bee5fbf

Browse files
Support passing http client options
1 parent fde0e78 commit bee5fbf

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

lib/seam/http_multi_workspace.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ module Http
1010
class MultiWorkspace
1111
attr_reader :client, :wait_for_action_attempt, :defaults
1212

13-
def initialize(personal_access_token:, endpoint: nil, wait_for_action_attempt: true)
13+
def initialize(personal_access_token:, endpoint: nil, wait_for_action_attempt: true, client_options: {})
1414
@wait_for_action_attempt = wait_for_action_attempt
1515
@defaults = {"wait_for_action_attempt" => wait_for_action_attempt}
1616
@endpoint = Http::Options.get_endpoint(endpoint)
1717
@auth_headers = Http::Auth.get_auth_headers_for_multi_workspace_personal_access_token(personal_access_token)
18-
@client = Seam::Http::Request.create_faraday_client(@endpoint, @auth_headers)
18+
@client = Seam::Http::Request.create_faraday_client(@endpoint, @auth_headers, client_options)
1919
end
2020

2121
def self.lts_version
@@ -30,11 +30,12 @@ def workspaces
3030
@workspaces ||= WorkspacesProxy.new(Seam::Clients::Workspaces.new(self))
3131
end
3232

33-
def self.from_personal_access_token(personal_access_token, endpoint: nil, wait_for_action_attempt: true)
33+
def self.from_personal_access_token(personal_access_token, endpoint: nil, wait_for_action_attempt: true, client_options: {})
3434
new(
3535
personal_access_token: personal_access_token,
3636
endpoint: endpoint,
37-
wait_for_action_attempt: wait_for_action_attempt
37+
wait_for_action_attempt: wait_for_action_attempt,
38+
client_options: client_options
3839
)
3940
end
4041

lib/seam/http_single_workspace.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ class SingleWorkspace
1212
attr_reader :client, :defaults
1313

1414
def initialize(client: nil, api_key: nil, personal_access_token: nil, workspace_id: nil, endpoint: nil,
15-
wait_for_action_attempt: true)
15+
wait_for_action_attempt: true, client_options: {})
1616
options = Http::Options.parse_options(api_key: api_key, personal_access_token: personal_access_token, workspace_id: workspace_id, endpoint: endpoint)
1717
@endpoint = options[:endpoint]
1818
@auth_headers = options[:auth_headers]
1919
@defaults = Seam::DeepHashAccessor.new({"wait_for_action_attempt" => wait_for_action_attempt})
2020

21-
@client = client || Seam::Http::Request.create_faraday_client(@endpoint, @auth_headers)
21+
@client = client || Seam::Http::Request.create_faraday_client(@endpoint, @auth_headers, client_options)
2222
end
2323

2424
def lts_version
2525
Seam::LTS_VERSION
2626
end
2727

28-
def self.from_api_key(api_key, endpoint: nil, wait_for_action_attempt: false)
29-
new(api_key: api_key, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt)
28+
def self.from_api_key(api_key, endpoint: nil, wait_for_action_attempt: false, client_options: {})
29+
new(api_key: api_key, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt, client_options: client_options)
3030
end
3131

32-
def self.from_personal_access_token(personal_access_token, workspace_id, endpoint: nil, wait_for_action_attempt: false)
33-
new(personal_access_token: personal_access_token, workspace_id: workspace_id, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt)
32+
def self.from_personal_access_token(personal_access_token, workspace_id, endpoint: nil, wait_for_action_attempt: false, client_options: {})
33+
new(personal_access_token: personal_access_token, workspace_id: workspace_id, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt, client_options: client_options)
3434
end
3535

3636
def request_seam_object(method, path, klass, inner_object, config = {})

lib/seam/request.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
module Seam
77
module Http
88
module Request
9-
def self.create_faraday_client(endpoint, auth_headers)
10-
Faraday.new(endpoint) do |builder|
9+
def self.create_faraday_client(endpoint, auth_headers, client_options = {})
10+
default_options = {
11+
url: endpoint,
12+
headers: auth_headers.merge(default_headers)
13+
}
14+
15+
options = deep_merge(default_options, client_options)
16+
17+
Faraday.new(options) do |builder|
1118
builder.request :json
1219
builder.request :retry, backoff_factor: 2
1320
builder.response :json
14-
builder.headers = auth_headers.merge(default_headers)
1521
end
1622
end
1723

@@ -57,6 +63,20 @@ def self.default_headers
5763
:"seam-lts-version" => Seam::LTS_VERSION
5864
}
5965
end
66+
67+
def self.deep_merge(hash1, hash2)
68+
result = hash1.dup
69+
hash2.each do |key, value|
70+
result[key] = if value.is_a?(Hash) && result[key].is_a?(Hash)
71+
deep_merge(result[key], value)
72+
else
73+
value
74+
end
75+
end
76+
result
77+
end
78+
79+
private_class_method :deep_merge
6080
end
6181
end
6282
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Seam::Http::SingleWorkspace do
6+
let(:api_key) { "seam_test_api_key" }
7+
let(:endpoint) { "https://example.com/api" }
8+
let(:client_options) do
9+
{
10+
headers: { "Custom-Header" => "Test-Value" },
11+
request: { timeout: 30 }
12+
}
13+
end
14+
15+
describe "client options" do
16+
it "passes client_options to the Faraday client" do
17+
expect(Faraday).to receive(:new).with(
18+
hash_including(
19+
headers: hash_including("Custom-Header" => "Test-Value"),
20+
request: { timeout: 30 }
21+
)
22+
).and_call_original
23+
24+
client = described_class.new(
25+
api_key: api_key,
26+
endpoint: endpoint,
27+
client_options: client_options
28+
)
29+
30+
expect(client).to be_a(Seam::Http::SingleWorkspace)
31+
end
32+
33+
it "merges client_options with default options" do
34+
client = described_class.new(
35+
api_key: api_key,
36+
endpoint: endpoint,
37+
client_options: client_options
38+
)
39+
40+
faraday_client = client.instance_variable_get(:@client)
41+
expect(faraday_client.headers["Custom-Header"]).to eq("Test-Value")
42+
expect(faraday_client.options.timeout).to eq(30)
43+
expect(faraday_client.headers["Authorization"]).to eq("Bearer #{api_key}")
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)