Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions lib/seam/http_single_workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,45 @@ class SingleWorkspace

attr_reader :client, :defaults

def initialize(client: nil, api_key: nil, personal_access_token: nil, workspace_id: nil, endpoint: nil,
wait_for_action_attempt: true, timeout: nil, faraday_options: {}, faraday_retry_options: {})
OPTION_NOT_PROVIDED = Object.new.freeze
private_constant :OPTION_NOT_PROVIDED

def initialize(client: nil, api_key: OPTION_NOT_PROVIDED, personal_access_token: OPTION_NOT_PROVIDED,
workspace_id: OPTION_NOT_PROVIDED, endpoint: OPTION_NOT_PROVIDED,
wait_for_action_attempt: OPTION_NOT_PROVIDED, timeout: OPTION_NOT_PROVIDED,
faraday_options: OPTION_NOT_PROVIDED, faraday_retry_options: OPTION_NOT_PROVIDED)
supplied_options = {
api_key: api_key,
personal_access_token: personal_access_token,
workspace_id: workspace_id,
endpoint: endpoint,
wait_for_action_attempt: wait_for_action_attempt,
timeout: timeout,
faraday_options: faraday_options,
faraday_retry_options: faraday_retry_options
}.reject { |_, value| value.equal?(OPTION_NOT_PROVIDED) }
invalid_options = supplied_options.keys - [:wait_for_action_attempt]

if client && invalid_options.any?
raise Http::Options::SeamInvalidOptionsError.new(
"The client option cannot be used with any other option, but received: #{supplied_options.keys.join(", ")}"
)
end

wait_for_action_attempt = true if wait_for_action_attempt.equal?(OPTION_NOT_PROVIDED)
@defaults = Seam::DeepHashAccessor.new({"wait_for_action_attempt" => wait_for_action_attempt})

# A client carries its own endpoint and authorization, so the auth
# options are only parsed when one has to be built.
@client = client || begin
api_key = nil if api_key.equal?(OPTION_NOT_PROVIDED)
personal_access_token = nil if personal_access_token.equal?(OPTION_NOT_PROVIDED)
workspace_id = nil if workspace_id.equal?(OPTION_NOT_PROVIDED)
endpoint = nil if endpoint.equal?(OPTION_NOT_PROVIDED)
timeout = nil if timeout.equal?(OPTION_NOT_PROVIDED)
faraday_options = {} if faraday_options.equal?(OPTION_NOT_PROVIDED)
faraday_retry_options = {} if faraday_retry_options.equal?(OPTION_NOT_PROVIDED)

options = Http::Options.parse_options(api_key: api_key, personal_access_token: personal_access_token,
workspace_id: workspace_id, endpoint: endpoint)
@endpoint = options[:endpoint]
Expand Down
45 changes: 37 additions & 8 deletions lib/seam/http_without_workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,47 @@ module Http
class WithoutWorkspace
attr_reader :client, :defaults

def initialize(personal_access_token: nil, endpoint: nil, wait_for_action_attempt: true, timeout: nil,
faraday_options: {}, faraday_retry_options: {})
OPTION_NOT_PROVIDED = Object.new.freeze
private_constant :OPTION_NOT_PROVIDED

def initialize(client: nil, personal_access_token: OPTION_NOT_PROVIDED, endpoint: OPTION_NOT_PROVIDED,
wait_for_action_attempt: OPTION_NOT_PROVIDED, timeout: OPTION_NOT_PROVIDED,
faraday_options: OPTION_NOT_PROVIDED, faraday_retry_options: OPTION_NOT_PROVIDED)
supplied_options = {
personal_access_token: personal_access_token,
endpoint: endpoint,
wait_for_action_attempt: wait_for_action_attempt,
timeout: timeout,
faraday_options: faraday_options,
faraday_retry_options: faraday_retry_options
}.reject { |_, value| value.equal?(OPTION_NOT_PROVIDED) }
invalid_options = supplied_options.keys - [:wait_for_action_attempt]

if client && invalid_options.any?
raise Http::Options::SeamInvalidOptionsError.new(
"The client option cannot be used with any other option, but received: #{supplied_options.keys.join(", ")}"
)
end

wait_for_action_attempt = true if wait_for_action_attempt.equal?(OPTION_NOT_PROVIDED)
@wait_for_action_attempt = wait_for_action_attempt
@defaults = {"wait_for_action_attempt" => wait_for_action_attempt}

options = Http::Options.parse_without_workspace_options(personal_access_token: personal_access_token,
endpoint: endpoint)
@endpoint = options[:endpoint]
@auth_headers = options[:auth_headers]
@client = client || begin
personal_access_token = nil if personal_access_token.equal?(OPTION_NOT_PROVIDED)
endpoint = nil if endpoint.equal?(OPTION_NOT_PROVIDED)
timeout = nil if timeout.equal?(OPTION_NOT_PROVIDED)
faraday_options = {} if faraday_options.equal?(OPTION_NOT_PROVIDED)
faraday_retry_options = {} if faraday_retry_options.equal?(OPTION_NOT_PROVIDED)

options = Http::Options.parse_without_workspace_options(personal_access_token: personal_access_token,
endpoint: endpoint)
@endpoint = options[:endpoint]
@auth_headers = options[:auth_headers]

@client = Http::Request.create_faraday_client(@endpoint, @auth_headers, faraday_options,
faraday_retry_options, timeout: timeout)
Http::Request.create_faraday_client(@endpoint, @auth_headers, faraday_options,
faraday_retry_options, timeout: timeout)
end
end

def workspaces
Expand Down
28 changes: 28 additions & 0 deletions spec/seam_client/client_without_workspace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@

expect(seam.workspaces.list.length).to be > 0
end

it "accepts an injected client with request defaults" do
client = Faraday.new

seam = described_class.new(client: client, wait_for_action_attempt: false)

expect(seam.client).to equal(client)
expect(seam.defaults["wait_for_action_attempt"]).to be(false)
end

construction_options = {
personal_access_token: "seam_at1_token",
endpoint: "https://example.com",
timeout: 10,
faraday_options: {headers: {"Custom-Header" => "value"}},
faraday_retry_options: {max: 1}
}

construction_options.each do |option, value|
it "rejects #{option} when a client is injected" do
expect do
described_class.new(client: Faraday.new, **{option => value})
end.to raise_error(
Seam::Http::Options::SeamInvalidOptionsError,
/The client option cannot be used with any other option, but received: #{option}/
)
end
end
end

describe "#workspaces" do
Expand Down
30 changes: 30 additions & 0 deletions spec/seam_client/faraday_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,35 @@
expect(response.status).to eq(200)
expect(response.body["device"]["device_id"]).to eq(seed["august_device_1"])
end

it "allows request defaults" do
client = Faraday.new

seam = described_class.new(client: client, wait_for_action_attempt: false)

expect(seam.client).to equal(client)
expect(seam.defaults["wait_for_action_attempt"]).to be(false)
end

construction_options = {
api_key: "seam_some_api_key",
personal_access_token: "seam_at1_token",
workspace_id: "workspace-id",
endpoint: "https://example.com",
timeout: 10,
faraday_options: {headers: {"Custom-Header" => "value"}},
faraday_retry_options: {max: 1}
}

construction_options.each do |option, value|
it "rejects #{option} because an injected client already carries its construction options" do
expect do
described_class.new(client: Faraday.new, **{option => value})
end.to raise_error(
Seam::Http::Options::SeamInvalidOptionsError,
/The client option cannot be used with any other option, but received: #{option}/
)
end
end
end
end
Loading