From 7b66dd61242a5607a40e59188bd7b17660c67ce1 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 14:56:32 +0100 Subject: [PATCH 01/15] Add response middleware --- lib/seam/request.rb | 77 +++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/seam/request.rb b/lib/seam/request.rb index 039838d..d1e4089 100644 --- a/lib/seam/request.rb +++ b/lib/seam/request.rb @@ -25,40 +25,7 @@ def self.create_faraday_client(endpoint, auth_headers, faraday_options = {}, far builder.request :json builder.request :retry, faraday_retry_options builder.response :json - end - end - - def self.handle_error_response(response, _method, _path) - status_code = response.status - request_id = response.headers["seam-request-id"] - - raise Http::UnauthorizedError.new(request_id) if status_code == 401 - - error = response.body.is_a?(Hash) ? response.body["error"] || {} : {} - error_type = error["type"] || "unknown_error" - error_message = error["message"] || "Unknown error" - error_details = { - type: error_type, - message: error_message, - data: error["data"] - } - - if error_type == "invalid_input" - error_details["validation_errors"] = error["validation_errors"] - raise Http::InvalidInputError.new(error_details, status_code, request_id) - end - - raise Http::ApiError.new(error_details, status_code, request_id) - end - - def self.request_seam(client, endpoint, method, path, config = {}) - url = "#{endpoint}#{path}" - response = client.run_request(method, url, config[:body], config[:headers]) - - if response.success? - response - else - handle_error_response(response, method, path) + builder.use ResponseMiddleware, retry_options: faraday_retry_options end end @@ -72,6 +39,48 @@ def self.default_headers } end + class ResponseMiddleware < Faraday::Middleware + def initialize(app, options = {}) + super(app) + @retry_options = options[:retry_options] + end + + def on_complete(env) + return if env.success? + + status_code = env.status + request_id = env.response_headers["seam-request-id"] + + retry_statuses = (@retry_options || {}).fetch(:retry_statuses, []) + if retry_statuses.include?(status_code) + raise Faraday::RetriableResponse.new(nil, env.response) + end + + raise Http::UnauthorizedError.new(request_id) if status_code == 401 + + body = begin + JSON.parse(env.body) + rescue + {} + end + error = body["error"] || {} + error_type = error["type"] || "unknown_error" + error_message = error["message"] || "Unknown error" + error_details = { + type: error_type, + message: error_message, + data: error["data"] + } + + if error_type == "invalid_input" + error_details["validation_errors"] = error["validation_errors"] + raise Http::InvalidInputError.new(error_details, status_code, request_id) + end + + raise Http::ApiError.new(error_details, status_code, request_id) + end + end + def self.deep_merge(hash1, hash2) result = hash1.dup hash2.each do |key, value| From 514251998fdc540a0b4c6e87aabaee36684971c5 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 14:57:19 +0100 Subject: [PATCH 02/15] Remove base client --- lib/seam/base_client.rb | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 lib/seam/base_client.rb diff --git a/lib/seam/base_client.rb b/lib/seam/base_client.rb deleted file mode 100644 index f51733c..0000000 --- a/lib/seam/base_client.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module Seam - module Clients - class BaseClient - attr_accessor :client - - def initialize(client) - @client = client - end - - def request_seam_object(*attrs) - client.request_seam_object(*attrs) - end - - def request_seam(*attrs) - client.request_seam(*attrs) - end - end - end -end From 0d8bce6563b2d6833369a116fe8e8a5c95de4266 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 14:57:58 +0100 Subject: [PATCH 03/15] Remove request_seam methods in favour of using faraday client directly --- lib/seam/http_multi_workspace.rb | 12 +----------- lib/seam/http_single_workspace.rb | 13 ++----------- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/lib/seam/http_multi_workspace.rb b/lib/seam/http_multi_workspace.rb index 516c0d1..973eaf4 100644 --- a/lib/seam/http_multi_workspace.rb +++ b/lib/seam/http_multi_workspace.rb @@ -29,7 +29,7 @@ def lts_version end def workspaces - @workspaces ||= WorkspacesProxy.new(Seam::Clients::Workspaces.new(self)) + @workspaces ||= WorkspacesProxy.new(Seam::Clients::Workspaces.new(client: @client, defaults: @defaults)) end def self.from_personal_access_token(personal_access_token, endpoint: nil, wait_for_action_attempt: true, faraday_options: {}, faraday_retry_options: {}) @@ -41,16 +41,6 @@ def self.from_personal_access_token(personal_access_token, endpoint: nil, wait_f faraday_retry_options: faraday_retry_options ) end - - def request_seam_object(method, path, klass, inner_object, config = {}) - response = Seam::Http::Request.request_seam(@client, @endpoint, method, path, config) - data = response.body[inner_object] - klass.load_from_response(data, self) - end - - def request_seam(method, path, config = {}) - Seam::Http::Request.request_seam(@client, @endpoint, method, path, config) - end end class WorkspacesProxy diff --git a/lib/seam/http_single_workspace.rb b/lib/seam/http_single_workspace.rb index 98d7880..157adbb 100644 --- a/lib/seam/http_single_workspace.rb +++ b/lib/seam/http_single_workspace.rb @@ -18,9 +18,10 @@ def initialize(client: nil, api_key: nil, personal_access_token: nil, workspace_ @endpoint = options[:endpoint] @auth_headers = options[:auth_headers] @defaults = Seam::DeepHashAccessor.new({"wait_for_action_attempt" => wait_for_action_attempt}) - @client = client || Seam::Http::Request.create_faraday_client(@endpoint, @auth_headers, faraday_options, faraday_retry_options) + + initialize_routes(client: @client, defaults: @defaults) end def lts_version @@ -36,16 +37,6 @@ def self.from_personal_access_token(personal_access_token, workspace_id, endpoin new(personal_access_token: personal_access_token, workspace_id: workspace_id, endpoint: endpoint, wait_for_action_attempt: wait_for_action_attempt, faraday_options: faraday_options, faraday_retry_options: faraday_retry_options) end - - def request_seam_object(method, path, klass, inner_object, config = {}) - response = Seam::Http::Request.request_seam(@client, @endpoint, method, path, config) - data = response.body[inner_object] - klass.load_from_response(data, self) - end - - def request_seam(method, path, config = {}) - Seam::Http::Request.request_seam(@client, @endpoint, method, path, config) - end end end end From 4e71113083ea8ec487015f971d47b338bac66486 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 15:03:44 +0100 Subject: [PATCH 04/15] Support [] syntax on DeepHashAccessor instances --- lib/seam/deep_hash_accessor.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/seam/deep_hash_accessor.rb b/lib/seam/deep_hash_accessor.rb index 093a5a7..515e877 100644 --- a/lib/seam/deep_hash_accessor.rb +++ b/lib/seam/deep_hash_accessor.rb @@ -9,6 +9,10 @@ def initialize(data) create_accessor_methods end + def [](key) + instance_variable_get(:"@#{key}") + end + private def create_accessor_methods From 95e3604aecbfb45175e799ddd3aabf72f439225f Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 15:04:33 +0100 Subject: [PATCH 05/15] Adjust action attempt polling to the new logic --- lib/seam/helpers/action_attempt.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/seam/helpers/action_attempt.rb b/lib/seam/helpers/action_attempt.rb index 89dbc59..ece3de1 100644 --- a/lib/seam/helpers/action_attempt.rb +++ b/lib/seam/helpers/action_attempt.rb @@ -4,13 +4,11 @@ module Seam module Helpers module ActionAttempt def self.decide_and_wait(action_attempt, client, wait_for_action_attempt) - wait_decision = wait_for_action_attempt.nil? ? client.defaults.wait_for_action_attempt : wait_for_action_attempt - - if wait_decision == true + if wait_for_action_attempt == true return wait_until_finished(action_attempt, client) - elsif wait_decision.is_a?(Hash) - return wait_until_finished(action_attempt, client, timeout: wait_decision[:timeout], - polling_interval: wait_decision[:polling_interval]) + elsif wait_for_action_attempt.is_a?(Hash) + return wait_until_finished(action_attempt, client, timeout: wait_for_action_attempt[:timeout], + polling_interval: wait_for_action_attempt[:polling_interval]) end action_attempt @@ -37,13 +35,7 @@ def self.wait_until_finished(action_attempt, client, timeout: nil, polling_inter end def self.update_action_attempt(action_attempt, client) - response = client.request_seam( - :post, - "/action_attempts/get", - body: { - action_attempt_id: action_attempt.action_attempt_id - } - ) + response = client.post("/action_attempts/get", {action_attempt_id: action_attempt.action_attempt_id}) action_attempt.update_from_response(response.body["action_attempt"]) action_attempt From 68a4c94675cb45de3df4beeba8fad1f1f8da2371 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 15:05:54 +0100 Subject: [PATCH 06/15] Bump the generator to 1.14.10 to adjust routes --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 166b59f..2b8a236 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "": { "name": "@seamapi/ruby", "devDependencies": { - "@seamapi/nextlove-sdk-generator": "1.14.6", + "@seamapi/nextlove-sdk-generator": "1.14.10", "@seamapi/types": "1.283.0", "del": "^7.1.0", "prettier": "^3.2.5" @@ -416,9 +416,9 @@ } }, "node_modules/@seamapi/nextlove-sdk-generator": { - "version": "1.14.6", - "resolved": "https://registry.npmjs.org/@seamapi/nextlove-sdk-generator/-/nextlove-sdk-generator-1.14.6.tgz", - "integrity": "sha512-SGOGUUZ+bzdm2xPzkbBc/a+t9Zlhee/nLF61rHcqGpljugKUTzyyoyT6iGdtFqO0zJiUtljakS1od6AJtONOow==", + "version": "1.14.10", + "resolved": "https://registry.npmjs.org/@seamapi/nextlove-sdk-generator/-/nextlove-sdk-generator-1.14.10.tgz", + "integrity": "sha512-kqNRJZ/+rNQpdcIo8mI0T20kf730P42SSL9r5hh23IObjd2+Z+bKaWTxsHjVbjlwQrhR4cKmR//xfr/7wx2ZCQ==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 46c9407..19b4df7 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "format": "prettier --write --ignore-path .gitignore ." }, "devDependencies": { - "@seamapi/nextlove-sdk-generator": "1.14.6", + "@seamapi/nextlove-sdk-generator": "1.14.10", "@seamapi/types": "1.283.0", "del": "^7.1.0", "prettier": "^3.2.5" From df3d364225f805e9867b692760a3c215494dfc03 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 15:06:04 +0100 Subject: [PATCH 07/15] Fix tests --- spec/clients/access_codes_spec.rb | 23 ++++++++++------ spec/resources/action_attempt_spec.rb | 26 +++++++++---------- spec/resources/base_resource_spec.rb | 1 - .../client_multi_workspace_spec.rb | 26 ++++++++++++++----- .../seam_client/faraday_retry_options_spec.rb | 7 ++++- 5 files changed, 53 insertions(+), 30 deletions(-) diff --git a/spec/clients/access_codes_spec.rb b/spec/clients/access_codes_spec.rb index 969da42..4ba9564 100644 --- a/spec/clients/access_codes_spec.rb +++ b/spec/clients/access_codes_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Seam::Clients::AccessCodes do - let(:client) { Seam.new(api_key: "seam_some_api_key") } + let(:seam) { Seam.new(api_key: "seam_some_api_key") } describe "#list" do let(:access_code_id) { "123" } @@ -17,13 +17,20 @@ end end - let(:access_codes) { client.access_codes.list(device_id: device_id) } + let(:access_codes) { seam.access_codes.list(device_id: device_id) } it "returns a list of Access codes" do expect(access_codes).to be_a(Array) expect(access_codes.first).to be_a(Seam::Resources::AccessCode) expect(access_codes.first.access_code_id).to be_a(String) end + + it "has client set" do + expect(seam.client).not_to be_nil + end + it "initializes access_codes" do + expect(seam.access_codes).not_to be_nil + end end context "'access_code_ids' param" do @@ -34,7 +41,7 @@ end end - let(:access_codes) { client.access_codes.list(access_code_ids: [access_code_id]) } + let(:access_codes) { seam.access_codes.list(access_code_ids: [access_code_id]) } it "returns a list of Access codes" do expect(access_codes).to be_a(Array) @@ -61,7 +68,7 @@ ).with { |req| req.body == {access_code_id: access_code_id}.to_json } end - let(:result) { client.access_codes.get(access_code_id: access_code_id) } + let(:result) { seam.access_codes.get(access_code_id: access_code_id) } it "returns an Access Code" do expect(result).to be_a(Seam::Resources::AccessCode) @@ -85,7 +92,7 @@ ) end - let(:result) { client.access_codes.create(**access_code_hash) } + let(:result) { seam.access_codes.create(**access_code_hash) } it "returns an Access Code" do expect(result).to be_a(Seam::Resources::AccessCode) @@ -114,7 +121,7 @@ ).with { |req| req.body == {action_attempt_id: action_attempt_hash[:action_attempt_id]}.to_json } end - let(:result) { client.access_codes.delete(access_code_id: access_code_id) } + let(:result) { seam.access_codes.delete(access_code_id: access_code_id) } it "returns an Access Code" do expect(result).to be_a(NilClass) @@ -143,7 +150,7 @@ ).with { |req| req.body == {action_attempt_id: action_attempt_hash[:action_attempt_id]}.to_json } end - let(:result) { client.access_codes.update(access_code_id: access_code_id, type: "ongoing") } + let(:result) { seam.access_codes.update(access_code_id: access_code_id, type: "ongoing") } it "returns an Access Code" do expect(result).to be_a(NilClass) @@ -162,7 +169,7 @@ end end - let(:result) { client.access_codes.pull_backup_access_code(access_code_id: access_code_id) } + let(:result) { seam.access_codes.pull_backup_access_code(access_code_id: access_code_id) } it "returns an backup Access Code" do expect(result).to be_a(Seam::Resources::AccessCode) diff --git a/spec/resources/action_attempt_spec.rb b/spec/resources/action_attempt_spec.rb index f94efc2..7ce36af 100644 --- a/spec/resources/action_attempt_spec.rb +++ b/spec/resources/action_attempt_spec.rb @@ -3,7 +3,7 @@ require "seam/helpers/action_attempt" RSpec.describe Seam::Helpers::ActionAttempt do - let(:client) { Seam.new(api_key: "seam_some_api_key") } + let(:seam) { Seam.new(api_key: "seam_some_api_key") } let(:action_attempt_id) { "action_attempt_id_1234" } let(:finished_status) { "finished" } let(:action_attempt_hash) do @@ -14,13 +14,13 @@ result: {} } end - let(:action_attempt) { Seam::Resources::ActionAttempt.new(action_attempt_hash, client) } + let(:action_attempt) { Seam::Resources::ActionAttempt.new(action_attempt_hash, seam) } describe ".decide_and_wait" do context "when wait_for_action_attempt is true" do it "calls wait_until_finished" do - expect(described_class).to receive(:wait_until_finished).with(action_attempt, client) - described_class.decide_and_wait(action_attempt, client, true) + expect(described_class).to receive(:wait_until_finished).with(action_attempt, seam) + described_class.decide_and_wait(action_attempt, seam, true) end end @@ -28,23 +28,23 @@ let(:wait_options) { {timeout: 10, polling_interval: 1} } it "calls wait_until_finished with options" do - expect(described_class).to receive(:wait_until_finished).with(action_attempt, client, timeout: 10, + expect(described_class).to receive(:wait_until_finished).with(action_attempt, seam, timeout: 10, polling_interval: 1) - described_class.decide_and_wait(action_attempt, client, wait_options) + described_class.decide_and_wait(action_attempt, seam, wait_options) end end context "when wait_for_action_attempt is nil" do - it "uses the client's actual default wait_for_action_attempt value" do - client_default = client.defaults.wait_for_action_attempt + it "uses the seam's actual default wait_for_action_attempt value" do + seam_default = seam.defaults.wait_for_action_attempt - if client_default - expect(described_class).to receive(:wait_until_finished).with(action_attempt, client) + if seam_default + expect(described_class).to receive(:wait_until_finished).with(action_attempt, seam) else expect(described_class).not_to receive(:wait_until_finished) end - described_class.decide_and_wait(action_attempt, client, nil) + described_class.decide_and_wait(action_attempt, seam, nil) end end end @@ -69,7 +69,7 @@ ) end - let(:result) { described_class.wait_until_finished(action_attempt, client) } + let(:result) { described_class.wait_until_finished(action_attempt, seam.client) } it "returns an updated ActionAttempt" do expect(result.status).to eq(finished_status) @@ -88,7 +88,7 @@ end it "updates the ActionAttempt" do - updated_attempt = described_class.update_action_attempt(action_attempt, client) + updated_attempt = described_class.update_action_attempt(action_attempt, seam.client) expect(updated_attempt.status).to eq("finished") end end diff --git a/spec/resources/base_resource_spec.rb b/spec/resources/base_resource_spec.rb index 0285009..27e16a3 100644 --- a/spec/resources/base_resource_spec.rb +++ b/spec/resources/base_resource_spec.rb @@ -12,7 +12,6 @@ describe ".date_accessor" do it "parses a date string" do - puts device.created_at.class expect(device.created_at).to be_a(Time) end end diff --git a/spec/seam_client/client_multi_workspace_spec.rb b/spec/seam_client/client_multi_workspace_spec.rb index 3c6c2bd..f6b9497 100644 --- a/spec/seam_client/client_multi_workspace_spec.rb +++ b/spec/seam_client/client_multi_workspace_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Seam::Http::MultiWorkspace do let(:personal_access_token) { "seam_at_12345" } - let(:endpoint) { "https://example.com/api" } + let(:endpoint) { "https://example.com" } let(:client) { described_class.from_personal_access_token(personal_access_token, endpoint: endpoint) } describe ".from_personal_access_token" do @@ -20,16 +20,28 @@ expect(client.workspaces).to be_a(Seam::Http::WorkspacesProxy) end - before do + it "creates a new workspace" do + name = "Test Workspace" + connect_partner_name = "Example Partner" + is_sandbox = true + stub_request(:post, "#{endpoint}/workspaces/create") + .with( + body: { + name: name, + connect_partner_name: connect_partner_name, + is_sandbox: is_sandbox + }.to_json, + headers: { + "Content-Type" => "application/json" + } + ) .to_return(status: 200, body: {workspace: {workspace_id: "ws_123456"}}.to_json, headers: {"Content-Type" => "application/json"}) - end - it "creates a new workspace" do workspace = client.workspaces.create( - name: "Test Workspace", - connect_partner_name: "Example Partner", - is_sandbox: true + name: name, + connect_partner_name: connect_partner_name, + is_sandbox: is_sandbox ) expect(workspace).to be_a(Seam::Resources::Workspace) diff --git a/spec/seam_client/faraday_retry_options_spec.rb b/spec/seam_client/faraday_retry_options_spec.rb index 8f0424e..749dc29 100644 --- a/spec/seam_client/faraday_retry_options_spec.rb +++ b/spec/seam_client/faraday_retry_options_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Seam::Http::SingleWorkspace do let(:api_key) { "seam_test_api_key" } - let(:endpoint) { "https://example.com/api" } + let(:endpoint) { "https://example.com" } let(:faraday_retry_options) do { max: 3, @@ -23,6 +23,11 @@ ) stub_request(:post, "#{endpoint}/devices/list") + .with( + headers: { + "Content-Type" => "application/json" + } + ) .to_return(status: 500, body: {"error" => {"type" => "server_error", "message" => "Internal Server Error"}}.to_json, headers: {"Content-Type" => "application/json"}) .to_return(status: 500, body: {"error" => {"type" => "server_error", "message" => "Internal Server Error"}}.to_json, headers: {"Content-Type" => "application/json"}) .to_return(status: 200, body: {devices: []}.to_json, headers: {"Content-Type" => "application/json"}) From 9fdc02b6c829b6fe7090364ef05209c6e72cb3c4 Mon Sep 17 00:00:00 2001 From: Seam Bot Date: Thu, 31 Oct 2024 14:07:32 +0000 Subject: [PATCH 08/15] ci: Generate code --- lib/seam/routes/clients/access_codes.rb | 89 ++++------- .../routes/clients/access_codes_simulate.rb | 17 ++- .../routes/clients/access_codes_unmanaged.rb | 45 ++---- lib/seam/routes/clients/acs.rb | 23 +-- lib/seam/routes/clients/acs_access_groups.rb | 59 +++----- .../clients/acs_access_groups_unmanaged.rb | 19 +-- .../routes/clients/acs_credential_pools.rb | 17 ++- ...acs_credential_provisioning_automations.rb | 17 ++- lib/seam/routes/clients/acs_credentials.rb | 71 +++------ .../clients/acs_credentials_unmanaged.rb | 19 +-- lib/seam/routes/clients/acs_encoders.rb | 45 +++--- lib/seam/routes/clients/acs_entrances.rb | 43 ++---- lib/seam/routes/clients/acs_systems.rb | 37 ++--- lib/seam/routes/clients/acs_users.rb | 89 +++-------- .../routes/clients/acs_users_unmanaged.rb | 19 +-- lib/seam/routes/clients/action_attempts.rb | 29 ++-- lib/seam/routes/clients/client_sessions.rb | 65 +++----- lib/seam/routes/clients/connect_webviews.rb | 43 ++---- lib/seam/routes/clients/connected_accounts.rb | 39 ++--- lib/seam/routes/clients/devices.rb | 53 +++---- lib/seam/routes/clients/devices_simulate.rb | 25 ++- lib/seam/routes/clients/devices_unmanaged.rb | 33 ++-- lib/seam/routes/clients/events.rb | 27 ++-- lib/seam/routes/clients/index.rb | 1 - lib/seam/routes/clients/locks.rb | 55 +++---- lib/seam/routes/clients/networks.rb | 27 ++-- lib/seam/routes/clients/noise_sensors.rb | 21 +-- .../clients/noise_sensors_noise_thresholds.rb | 49 +++--- .../routes/clients/noise_sensors_simulate.rb | 13 +- lib/seam/routes/clients/phones.rb | 25 ++- lib/seam/routes/clients/phones_simulate.rb | 17 ++- lib/seam/routes/clients/thermostats.rb | 143 ++++++------------ .../routes/clients/thermostats_schedules.rb | 49 +++--- lib/seam/routes/clients/user_identities.rb | 105 ++++--------- .../user_identities_enrollment_automations.rb | 39 ++--- lib/seam/routes/clients/webhooks.rb | 49 +++--- lib/seam/routes/clients/workspaces.rb | 51 +++---- lib/seam/routes/routes.rb | 45 +++--- 38 files changed, 605 insertions(+), 1007 deletions(-) diff --git a/lib/seam/routes/clients/access_codes.rb b/lib/seam/routes/clients/access_codes.rb index c52d632..87413d4 100644 --- a/lib/seam/routes/clients/access_codes.rb +++ b/lib/seam/routes/clients/access_codes.rb @@ -2,101 +2,70 @@ module Seam module Clients - class AccessCodes < BaseClient + class AccessCodes + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def simulate - @simulate ||= Seam::Clients::AccessCodesSimulate.new(self) + @simulate ||= Seam::Clients::AccessCodesSimulate.new(client: @client, defaults: @defaults) end def unmanaged - @unmanaged ||= Seam::Clients::AccessCodesUnmanaged.new(self) + @unmanaged ||= Seam::Clients::AccessCodesUnmanaged.new(client: @client, defaults: @defaults) end def create(device_id:, allow_external_modification: nil, attempt_for_offline_device: nil, code: nil, common_code_key: nil, ends_at: nil, is_external_modification_allowed: nil, is_offline_access_code: nil, is_one_time_use: nil, max_time_rounding: nil, name: nil, prefer_native_scheduling: nil, preferred_code_length: nil, starts_at: nil, sync: nil, use_backup_access_code_pool: nil, use_offline_access_code: nil) - request_seam_object( - :post, - "/access_codes/create", - Seam::Resources::AccessCode, - "access_code", - body: {device_id: device_id, allow_external_modification: allow_external_modification, attempt_for_offline_device: attempt_for_offline_device, code: code, common_code_key: common_code_key, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, is_offline_access_code: is_offline_access_code, is_one_time_use: is_one_time_use, max_time_rounding: max_time_rounding, name: name, prefer_native_scheduling: prefer_native_scheduling, preferred_code_length: preferred_code_length, starts_at: starts_at, sync: sync, use_backup_access_code_pool: use_backup_access_code_pool, use_offline_access_code: use_offline_access_code}.compact - ) + res = @client.post("/access_codes/create", {device_id: device_id, allow_external_modification: allow_external_modification, attempt_for_offline_device: attempt_for_offline_device, code: code, common_code_key: common_code_key, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, is_offline_access_code: is_offline_access_code, is_one_time_use: is_one_time_use, max_time_rounding: max_time_rounding, name: name, prefer_native_scheduling: prefer_native_scheduling, preferred_code_length: preferred_code_length, starts_at: starts_at, sync: sync, use_backup_access_code_pool: use_backup_access_code_pool, use_offline_access_code: use_offline_access_code}.compact) + + Seam::Resources::AccessCode.load_from_response(res.body["access_code"]) end def create_multiple(device_ids:, allow_external_modification: nil, attempt_for_offline_device: nil, behavior_when_code_cannot_be_shared: nil, code: nil, ends_at: nil, is_external_modification_allowed: nil, is_offline_access_code: nil, is_one_time_use: nil, max_time_rounding: nil, name: nil, prefer_native_scheduling: nil, preferred_code_length: nil, starts_at: nil, use_backup_access_code_pool: nil, use_offline_access_code: nil) - request_seam_object( - :post, - "/access_codes/create_multiple", - Seam::Resources::AccessCode, - "access_codes", - body: {device_ids: device_ids, allow_external_modification: allow_external_modification, attempt_for_offline_device: attempt_for_offline_device, behavior_when_code_cannot_be_shared: behavior_when_code_cannot_be_shared, code: code, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, is_offline_access_code: is_offline_access_code, is_one_time_use: is_one_time_use, max_time_rounding: max_time_rounding, name: name, prefer_native_scheduling: prefer_native_scheduling, preferred_code_length: preferred_code_length, starts_at: starts_at, use_backup_access_code_pool: use_backup_access_code_pool, use_offline_access_code: use_offline_access_code}.compact - ) + res = @client.post("/access_codes/create_multiple", {device_ids: device_ids, allow_external_modification: allow_external_modification, attempt_for_offline_device: attempt_for_offline_device, behavior_when_code_cannot_be_shared: behavior_when_code_cannot_be_shared, code: code, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, is_offline_access_code: is_offline_access_code, is_one_time_use: is_one_time_use, max_time_rounding: max_time_rounding, name: name, prefer_native_scheduling: prefer_native_scheduling, preferred_code_length: preferred_code_length, starts_at: starts_at, use_backup_access_code_pool: use_backup_access_code_pool, use_offline_access_code: use_offline_access_code}.compact) + + Seam::Resources::AccessCode.load_from_response(res.body["access_codes"]) end def delete(access_code_id:, device_id: nil, sync: nil) - request_seam( - :post, - "/access_codes/delete", - body: {access_code_id: access_code_id, device_id: device_id, sync: sync}.compact - ) + @client.post("/access_codes/delete", {access_code_id: access_code_id, device_id: device_id, sync: sync}.compact) nil end def generate_code(device_id:) - request_seam_object( - :post, - "/access_codes/generate_code", - Seam::Resources::AccessCode, - "generated_code", - body: {device_id: device_id}.compact - ) + res = @client.post("/access_codes/generate_code", {device_id: device_id}.compact) + + Seam::Resources::AccessCode.load_from_response(res.body["generated_code"]) end def get(access_code_id: nil, code: nil, device_id: nil) - request_seam_object( - :post, - "/access_codes/get", - Seam::Resources::AccessCode, - "access_code", - body: {access_code_id: access_code_id, code: code, device_id: device_id}.compact - ) + res = @client.post("/access_codes/get", {access_code_id: access_code_id, code: code, device_id: device_id}.compact) + + Seam::Resources::AccessCode.load_from_response(res.body["access_code"]) end def list(access_code_ids: nil, device_id: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/access_codes/list", - Seam::Resources::AccessCode, - "access_codes", - body: {access_code_ids: access_code_ids, device_id: device_id, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/access_codes/list", {access_code_ids: access_code_ids, device_id: device_id, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::AccessCode.load_from_response(res.body["access_codes"]) end def pull_backup_access_code(access_code_id:) - request_seam_object( - :post, - "/access_codes/pull_backup_access_code", - Seam::Resources::AccessCode, - "backup_access_code", - body: {access_code_id: access_code_id}.compact - ) + res = @client.post("/access_codes/pull_backup_access_code", {access_code_id: access_code_id}.compact) + + Seam::Resources::AccessCode.load_from_response(res.body["backup_access_code"]) end def update(access_code_id:, allow_external_modification: nil, attempt_for_offline_device: nil, code: nil, device_id: nil, ends_at: nil, is_external_modification_allowed: nil, is_managed: nil, is_offline_access_code: nil, is_one_time_use: nil, max_time_rounding: nil, name: nil, prefer_native_scheduling: nil, preferred_code_length: nil, starts_at: nil, sync: nil, type: nil, use_backup_access_code_pool: nil, use_offline_access_code: nil) - request_seam( - :post, - "/access_codes/update", - body: {access_code_id: access_code_id, allow_external_modification: allow_external_modification, attempt_for_offline_device: attempt_for_offline_device, code: code, device_id: device_id, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, is_managed: is_managed, is_offline_access_code: is_offline_access_code, is_one_time_use: is_one_time_use, max_time_rounding: max_time_rounding, name: name, prefer_native_scheduling: prefer_native_scheduling, preferred_code_length: preferred_code_length, starts_at: starts_at, sync: sync, type: type, use_backup_access_code_pool: use_backup_access_code_pool, use_offline_access_code: use_offline_access_code}.compact - ) + @client.post("/access_codes/update", {access_code_id: access_code_id, allow_external_modification: allow_external_modification, attempt_for_offline_device: attempt_for_offline_device, code: code, device_id: device_id, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, is_managed: is_managed, is_offline_access_code: is_offline_access_code, is_one_time_use: is_one_time_use, max_time_rounding: max_time_rounding, name: name, prefer_native_scheduling: prefer_native_scheduling, preferred_code_length: preferred_code_length, starts_at: starts_at, sync: sync, type: type, use_backup_access_code_pool: use_backup_access_code_pool, use_offline_access_code: use_offline_access_code}.compact) nil end def update_multiple(common_code_key:, allow_external_modification: nil, code: nil, ends_at: nil, is_external_modification_allowed: nil, name: nil, prefer_native_scheduling: nil, starts_at: nil) - request_seam( - :post, - "/access_codes/update_multiple", - body: {common_code_key: common_code_key, allow_external_modification: allow_external_modification, code: code, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, name: name, prefer_native_scheduling: prefer_native_scheduling, starts_at: starts_at}.compact - ) + @client.post("/access_codes/update_multiple", {common_code_key: common_code_key, allow_external_modification: allow_external_modification, code: code, ends_at: ends_at, is_external_modification_allowed: is_external_modification_allowed, name: name, prefer_native_scheduling: prefer_native_scheduling, starts_at: starts_at}.compact) nil end diff --git a/lib/seam/routes/clients/access_codes_simulate.rb b/lib/seam/routes/clients/access_codes_simulate.rb index e6b84e0..3654f33 100644 --- a/lib/seam/routes/clients/access_codes_simulate.rb +++ b/lib/seam/routes/clients/access_codes_simulate.rb @@ -2,15 +2,16 @@ module Seam module Clients - class AccessCodesSimulate < BaseClient + class AccessCodesSimulate + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create_unmanaged_access_code(code:, device_id:, name:) - request_seam_object( - :post, - "/access_codes/simulate/create_unmanaged_access_code", - Seam::Resources::UnmanagedAccessCode, - "access_code", - body: {code: code, device_id: device_id, name: name}.compact - ) + res = @client.post("/access_codes/simulate/create_unmanaged_access_code", {code: code, device_id: device_id, name: name}.compact) + + Seam::Resources::UnmanagedAccessCode.load_from_response(res.body["access_code"]) end end end diff --git a/lib/seam/routes/clients/access_codes_unmanaged.rb b/lib/seam/routes/clients/access_codes_unmanaged.rb index 3d96d67..03983f1 100644 --- a/lib/seam/routes/clients/access_codes_unmanaged.rb +++ b/lib/seam/routes/clients/access_codes_unmanaged.rb @@ -2,53 +2,38 @@ module Seam module Clients - class AccessCodesUnmanaged < BaseClient + class AccessCodesUnmanaged + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def convert_to_managed(access_code_id:, allow_external_modification: nil, force: nil, is_external_modification_allowed: nil, sync: nil) - request_seam( - :post, - "/access_codes/unmanaged/convert_to_managed", - body: {access_code_id: access_code_id, allow_external_modification: allow_external_modification, force: force, is_external_modification_allowed: is_external_modification_allowed, sync: sync}.compact - ) + @client.post("/access_codes/unmanaged/convert_to_managed", {access_code_id: access_code_id, allow_external_modification: allow_external_modification, force: force, is_external_modification_allowed: is_external_modification_allowed, sync: sync}.compact) nil end def delete(access_code_id:, sync: nil) - request_seam( - :post, - "/access_codes/unmanaged/delete", - body: {access_code_id: access_code_id, sync: sync}.compact - ) + @client.post("/access_codes/unmanaged/delete", {access_code_id: access_code_id, sync: sync}.compact) nil end def get(access_code_id: nil, code: nil, device_id: nil) - request_seam_object( - :post, - "/access_codes/unmanaged/get", - Seam::Resources::UnmanagedAccessCode, - "access_code", - body: {access_code_id: access_code_id, code: code, device_id: device_id}.compact - ) + res = @client.post("/access_codes/unmanaged/get", {access_code_id: access_code_id, code: code, device_id: device_id}.compact) + + Seam::Resources::UnmanagedAccessCode.load_from_response(res.body["access_code"]) end def list(device_id:, user_identifier_key: nil) - request_seam_object( - :post, - "/access_codes/unmanaged/list", - Seam::Resources::UnmanagedAccessCode, - "access_codes", - body: {device_id: device_id, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/access_codes/unmanaged/list", {device_id: device_id, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::UnmanagedAccessCode.load_from_response(res.body["access_codes"]) end def update(access_code_id:, is_managed:, allow_external_modification: nil, force: nil, is_external_modification_allowed: nil) - request_seam( - :post, - "/access_codes/unmanaged/update", - body: {access_code_id: access_code_id, is_managed: is_managed, allow_external_modification: allow_external_modification, force: force, is_external_modification_allowed: is_external_modification_allowed}.compact - ) + @client.post("/access_codes/unmanaged/update", {access_code_id: access_code_id, is_managed: is_managed, allow_external_modification: allow_external_modification, force: force, is_external_modification_allowed: is_external_modification_allowed}.compact) nil end diff --git a/lib/seam/routes/clients/acs.rb b/lib/seam/routes/clients/acs.rb index 508beaf..bc3552a 100644 --- a/lib/seam/routes/clients/acs.rb +++ b/lib/seam/routes/clients/acs.rb @@ -2,37 +2,42 @@ module Seam module Clients - class Acs < BaseClient + class Acs + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def access_groups - @access_groups ||= Seam::Clients::AcsAccessGroups.new(self) + @access_groups ||= Seam::Clients::AcsAccessGroups.new(client: @client, defaults: @defaults) end def credential_pools - @credential_pools ||= Seam::Clients::AcsCredentialPools.new(self) + @credential_pools ||= Seam::Clients::AcsCredentialPools.new(client: @client, defaults: @defaults) end def credential_provisioning_automations - @credential_provisioning_automations ||= Seam::Clients::AcsCredentialProvisioningAutomations.new(self) + @credential_provisioning_automations ||= Seam::Clients::AcsCredentialProvisioningAutomations.new(client: @client, defaults: @defaults) end def credentials - @credentials ||= Seam::Clients::AcsCredentials.new(self) + @credentials ||= Seam::Clients::AcsCredentials.new(client: @client, defaults: @defaults) end def encoders - @encoders ||= Seam::Clients::AcsEncoders.new(self) + @encoders ||= Seam::Clients::AcsEncoders.new(client: @client, defaults: @defaults) end def entrances - @entrances ||= Seam::Clients::AcsEntrances.new(self) + @entrances ||= Seam::Clients::AcsEntrances.new(client: @client, defaults: @defaults) end def systems - @systems ||= Seam::Clients::AcsSystems.new(self) + @systems ||= Seam::Clients::AcsSystems.new(client: @client, defaults: @defaults) end def users - @users ||= Seam::Clients::AcsUsers.new(self) + @users ||= Seam::Clients::AcsUsers.new(client: @client, defaults: @defaults) end end end diff --git a/lib/seam/routes/clients/acs_access_groups.rb b/lib/seam/routes/clients/acs_access_groups.rb index b4f5b5a..4a5a997 100644 --- a/lib/seam/routes/clients/acs_access_groups.rb +++ b/lib/seam/routes/clients/acs_access_groups.rb @@ -2,63 +2,44 @@ module Seam module Clients - class AcsAccessGroups < BaseClient + class AcsAccessGroups + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def add_user(acs_access_group_id:, acs_user_id:) - request_seam( - :post, - "/acs/access_groups/add_user", - body: {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/access_groups/add_user", {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact) nil end def get(acs_access_group_id:) - request_seam_object( - :post, - "/acs/access_groups/get", - Seam::Resources::AcsAccessGroup, - "acs_access_group", - body: {acs_access_group_id: acs_access_group_id}.compact - ) + res = @client.post("/acs/access_groups/get", {acs_access_group_id: acs_access_group_id}.compact) + + Seam::Resources::AcsAccessGroup.load_from_response(res.body["acs_access_group"]) end def list(acs_system_id: nil, acs_user_id: nil) - request_seam_object( - :post, - "/acs/access_groups/list", - Seam::Resources::AcsAccessGroup, - "acs_access_groups", - body: {acs_system_id: acs_system_id, acs_user_id: acs_user_id}.compact - ) + res = @client.post("/acs/access_groups/list", {acs_system_id: acs_system_id, acs_user_id: acs_user_id}.compact) + + Seam::Resources::AcsAccessGroup.load_from_response(res.body["acs_access_groups"]) end def list_accessible_entrances(acs_access_group_id:) - request_seam_object( - :post, - "/acs/access_groups/list_accessible_entrances", - Seam::Resources::AcsEntrance, - "acs_entrances", - body: {acs_access_group_id: acs_access_group_id}.compact - ) + res = @client.post("/acs/access_groups/list_accessible_entrances", {acs_access_group_id: acs_access_group_id}.compact) + + Seam::Resources::AcsEntrance.load_from_response(res.body["acs_entrances"]) end def list_users(acs_access_group_id:) - request_seam_object( - :post, - "/acs/access_groups/list_users", - Seam::Resources::AcsUser, - "acs_users", - body: {acs_access_group_id: acs_access_group_id}.compact - ) + res = @client.post("/acs/access_groups/list_users", {acs_access_group_id: acs_access_group_id}.compact) + + Seam::Resources::AcsUser.load_from_response(res.body["acs_users"]) end def remove_user(acs_access_group_id:, acs_user_id:) - request_seam( - :post, - "/acs/access_groups/remove_user", - body: {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/access_groups/remove_user", {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact) nil end diff --git a/lib/seam/routes/clients/acs_access_groups_unmanaged.rb b/lib/seam/routes/clients/acs_access_groups_unmanaged.rb index 522e341..e563ca3 100644 --- a/lib/seam/routes/clients/acs_access_groups_unmanaged.rb +++ b/lib/seam/routes/clients/acs_access_groups_unmanaged.rb @@ -2,23 +2,20 @@ module Seam module Clients - class AcsAccessGroupsUnmanaged < BaseClient + class AcsAccessGroupsUnmanaged + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(acs_access_group_id:) - request_seam( - :post, - "/acs/access_groups/unmanaged/get", - body: {acs_access_group_id: acs_access_group_id}.compact - ) + @client.post("/acs/access_groups/unmanaged/get", {acs_access_group_id: acs_access_group_id}.compact) nil end def list(acs_system_id: nil, acs_user_id: nil) - request_seam( - :post, - "/acs/access_groups/unmanaged/list", - body: {acs_system_id: acs_system_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/access_groups/unmanaged/list", {acs_system_id: acs_system_id, acs_user_id: acs_user_id}.compact) nil end diff --git a/lib/seam/routes/clients/acs_credential_pools.rb b/lib/seam/routes/clients/acs_credential_pools.rb index 66996e5..de1e51d 100644 --- a/lib/seam/routes/clients/acs_credential_pools.rb +++ b/lib/seam/routes/clients/acs_credential_pools.rb @@ -2,15 +2,16 @@ module Seam module Clients - class AcsCredentialPools < BaseClient + class AcsCredentialPools + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def list(acs_system_id:) - request_seam_object( - :post, - "/acs/credential_pools/list", - Seam::Resources::AcsCredentialPool, - "acs_credential_pools", - body: {acs_system_id: acs_system_id}.compact - ) + res = @client.post("/acs/credential_pools/list", {acs_system_id: acs_system_id}.compact) + + Seam::Resources::AcsCredentialPool.load_from_response(res.body["acs_credential_pools"]) end end end diff --git a/lib/seam/routes/clients/acs_credential_provisioning_automations.rb b/lib/seam/routes/clients/acs_credential_provisioning_automations.rb index 6864f62..f2ade18 100644 --- a/lib/seam/routes/clients/acs_credential_provisioning_automations.rb +++ b/lib/seam/routes/clients/acs_credential_provisioning_automations.rb @@ -2,15 +2,16 @@ module Seam module Clients - class AcsCredentialProvisioningAutomations < BaseClient + class AcsCredentialProvisioningAutomations + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def launch(credential_manager_acs_system_id:, user_identity_id:, acs_credential_pool_id: nil, create_credential_manager_user: nil, credential_manager_acs_user_id: nil) - request_seam_object( - :post, - "/acs/credential_provisioning_automations/launch", - Seam::Resources::AcsCredentialProvisioningAutomation, - "acs_credential_provisioning_automation", - body: {credential_manager_acs_system_id: credential_manager_acs_system_id, user_identity_id: user_identity_id, acs_credential_pool_id: acs_credential_pool_id, create_credential_manager_user: create_credential_manager_user, credential_manager_acs_user_id: credential_manager_acs_user_id}.compact - ) + res = @client.post("/acs/credential_provisioning_automations/launch", {credential_manager_acs_system_id: credential_manager_acs_system_id, user_identity_id: user_identity_id, acs_credential_pool_id: acs_credential_pool_id, create_credential_manager_user: create_credential_manager_user, credential_manager_acs_user_id: credential_manager_acs_user_id}.compact) + + Seam::Resources::AcsCredentialProvisioningAutomation.load_from_response(res.body["acs_credential_provisioning_automation"]) end end end diff --git a/lib/seam/routes/clients/acs_credentials.rb b/lib/seam/routes/clients/acs_credentials.rb index ea83200..17895b7 100644 --- a/lib/seam/routes/clients/acs_credentials.rb +++ b/lib/seam/routes/clients/acs_credentials.rb @@ -2,83 +2,56 @@ module Seam module Clients - class AcsCredentials < BaseClient + class AcsCredentials + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def assign(acs_credential_id:, acs_user_id:) - request_seam( - :post, - "/acs/credentials/assign", - body: {acs_credential_id: acs_credential_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/credentials/assign", {acs_credential_id: acs_credential_id, acs_user_id: acs_user_id}.compact) nil end def create(access_method:, acs_user_id:, allowed_acs_entrance_ids: nil, code: nil, credential_manager_acs_system_id: nil, ends_at: nil, is_multi_phone_sync_credential: nil, starts_at: nil, visionline_metadata: nil) - request_seam_object( - :post, - "/acs/credentials/create", - Seam::Resources::AcsCredential, - "acs_credential", - body: {access_method: access_method, acs_user_id: acs_user_id, allowed_acs_entrance_ids: allowed_acs_entrance_ids, code: code, credential_manager_acs_system_id: credential_manager_acs_system_id, ends_at: ends_at, is_multi_phone_sync_credential: is_multi_phone_sync_credential, starts_at: starts_at, visionline_metadata: visionline_metadata}.compact - ) + res = @client.post("/acs/credentials/create", {access_method: access_method, acs_user_id: acs_user_id, allowed_acs_entrance_ids: allowed_acs_entrance_ids, code: code, credential_manager_acs_system_id: credential_manager_acs_system_id, ends_at: ends_at, is_multi_phone_sync_credential: is_multi_phone_sync_credential, starts_at: starts_at, visionline_metadata: visionline_metadata}.compact) + + Seam::Resources::AcsCredential.load_from_response(res.body["acs_credential"]) end def delete(acs_credential_id:) - request_seam( - :post, - "/acs/credentials/delete", - body: {acs_credential_id: acs_credential_id}.compact - ) + @client.post("/acs/credentials/delete", {acs_credential_id: acs_credential_id}.compact) nil end def get(acs_credential_id:) - request_seam_object( - :post, - "/acs/credentials/get", - Seam::Resources::AcsCredential, - "acs_credential", - body: {acs_credential_id: acs_credential_id}.compact - ) + res = @client.post("/acs/credentials/get", {acs_credential_id: acs_credential_id}.compact) + + Seam::Resources::AcsCredential.load_from_response(res.body["acs_credential"]) end def list(acs_user_id: nil, acs_system_id: nil, user_identity_id: nil, created_before: nil, is_multi_phone_sync_credential: nil, limit: nil) - request_seam_object( - :post, - "/acs/credentials/list", - Seam::Resources::AcsCredential, - "acs_credentials", - body: {acs_user_id: acs_user_id, acs_system_id: acs_system_id, user_identity_id: user_identity_id, created_before: created_before, is_multi_phone_sync_credential: is_multi_phone_sync_credential, limit: limit}.compact - ) + res = @client.post("/acs/credentials/list", {acs_user_id: acs_user_id, acs_system_id: acs_system_id, user_identity_id: user_identity_id, created_before: created_before, is_multi_phone_sync_credential: is_multi_phone_sync_credential, limit: limit}.compact) + + Seam::Resources::AcsCredential.load_from_response(res.body["acs_credentials"]) end def list_accessible_entrances(acs_credential_id:) - request_seam_object( - :post, - "/acs/credentials/list_accessible_entrances", - Seam::Resources::AcsEntrance, - "acs_entrances", - body: {acs_credential_id: acs_credential_id}.compact - ) + res = @client.post("/acs/credentials/list_accessible_entrances", {acs_credential_id: acs_credential_id}.compact) + + Seam::Resources::AcsEntrance.load_from_response(res.body["acs_entrances"]) end def unassign(acs_credential_id:, acs_user_id:) - request_seam( - :post, - "/acs/credentials/unassign", - body: {acs_credential_id: acs_credential_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/credentials/unassign", {acs_credential_id: acs_credential_id, acs_user_id: acs_user_id}.compact) nil end def update(acs_credential_id:, code: nil, ends_at: nil) - request_seam( - :post, - "/acs/credentials/update", - body: {acs_credential_id: acs_credential_id, code: code, ends_at: ends_at}.compact - ) + @client.post("/acs/credentials/update", {acs_credential_id: acs_credential_id, code: code, ends_at: ends_at}.compact) nil end diff --git a/lib/seam/routes/clients/acs_credentials_unmanaged.rb b/lib/seam/routes/clients/acs_credentials_unmanaged.rb index 526b0c9..7e9c2f0 100644 --- a/lib/seam/routes/clients/acs_credentials_unmanaged.rb +++ b/lib/seam/routes/clients/acs_credentials_unmanaged.rb @@ -2,23 +2,20 @@ module Seam module Clients - class AcsCredentialsUnmanaged < BaseClient + class AcsCredentialsUnmanaged + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(acs_credential_id:) - request_seam( - :post, - "/acs/credentials/unmanaged/get", - body: {acs_credential_id: acs_credential_id}.compact - ) + @client.post("/acs/credentials/unmanaged/get", {acs_credential_id: acs_credential_id}.compact) nil end def list(acs_user_id: nil, acs_system_id: nil, user_identity_id: nil) - request_seam( - :post, - "/acs/credentials/unmanaged/list", - body: {acs_user_id: acs_user_id, acs_system_id: acs_system_id, user_identity_id: user_identity_id}.compact - ) + @client.post("/acs/credentials/unmanaged/list", {acs_user_id: acs_user_id, acs_system_id: acs_system_id, user_identity_id: user_identity_id}.compact) nil end diff --git a/lib/seam/routes/clients/acs_encoders.rb b/lib/seam/routes/clients/acs_encoders.rb index 7c59084..802103a 100644 --- a/lib/seam/routes/clients/acs_encoders.rb +++ b/lib/seam/routes/clients/acs_encoders.rb @@ -4,39 +4,32 @@ module Seam module Clients - class AcsEncoders < BaseClient + class AcsEncoders + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def encode_card(acs_credential_id:, device_id:, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/acs/encoders/encode_card", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {acs_credential_id: acs_credential_id, device_id: device_id}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/acs/encoders/encode_card", {acs_credential_id: acs_credential_id, device_id: device_id}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def list(acs_system_ids: nil, device_ids: nil, limit: nil) - request_seam_object( - :post, - "/acs/encoders/list", - Seam::Resources::Device, - "devices", - body: {acs_system_ids: acs_system_ids, device_ids: device_ids, limit: limit}.compact - ) + res = @client.post("/acs/encoders/list", {acs_system_ids: acs_system_ids, device_ids: device_ids, limit: limit}.compact) + + Seam::Resources::Device.load_from_response(res.body["devices"]) end def scan_card(acs_system_id:, device_id:, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/acs/encoders/scan_card", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {acs_system_id: acs_system_id, device_id: device_id}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/acs/encoders/scan_card", {acs_system_id: acs_system_id, device_id: device_id}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end end end diff --git a/lib/seam/routes/clients/acs_entrances.rb b/lib/seam/routes/clients/acs_entrances.rb index 896c69a..30b3e48 100644 --- a/lib/seam/routes/clients/acs_entrances.rb +++ b/lib/seam/routes/clients/acs_entrances.rb @@ -2,45 +2,34 @@ module Seam module Clients - class AcsEntrances < BaseClient + class AcsEntrances + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(acs_entrance_id:) - request_seam_object( - :post, - "/acs/entrances/get", - Seam::Resources::AcsEntrance, - "acs_entrance", - body: {acs_entrance_id: acs_entrance_id}.compact - ) + res = @client.post("/acs/entrances/get", {acs_entrance_id: acs_entrance_id}.compact) + + Seam::Resources::AcsEntrance.load_from_response(res.body["acs_entrance"]) end def grant_access(acs_entrance_id:, acs_user_id:) - request_seam( - :post, - "/acs/entrances/grant_access", - body: {acs_entrance_id: acs_entrance_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/entrances/grant_access", {acs_entrance_id: acs_entrance_id, acs_user_id: acs_user_id}.compact) nil end def list(acs_credential_id: nil, acs_system_id: nil) - request_seam_object( - :post, - "/acs/entrances/list", - Seam::Resources::AcsEntrance, - "acs_entrances", - body: {acs_credential_id: acs_credential_id, acs_system_id: acs_system_id}.compact - ) + res = @client.post("/acs/entrances/list", {acs_credential_id: acs_credential_id, acs_system_id: acs_system_id}.compact) + + Seam::Resources::AcsEntrance.load_from_response(res.body["acs_entrances"]) end def list_credentials_with_access(acs_entrance_id:, include_if: nil) - request_seam_object( - :post, - "/acs/entrances/list_credentials_with_access", - Seam::Resources::AcsCredential, - "acs_credentials", - body: {acs_entrance_id: acs_entrance_id, include_if: include_if}.compact - ) + res = @client.post("/acs/entrances/list_credentials_with_access", {acs_entrance_id: acs_entrance_id, include_if: include_if}.compact) + + Seam::Resources::AcsCredential.load_from_response(res.body["acs_credentials"]) end end end diff --git a/lib/seam/routes/clients/acs_systems.rb b/lib/seam/routes/clients/acs_systems.rb index 31fa209..4b9672f 100644 --- a/lib/seam/routes/clients/acs_systems.rb +++ b/lib/seam/routes/clients/acs_systems.rb @@ -2,35 +2,28 @@ module Seam module Clients - class AcsSystems < BaseClient + class AcsSystems + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(acs_system_id:) - request_seam_object( - :post, - "/acs/systems/get", - Seam::Resources::AcsSystem, - "acs_system", - body: {acs_system_id: acs_system_id}.compact - ) + res = @client.post("/acs/systems/get", {acs_system_id: acs_system_id}.compact) + + Seam::Resources::AcsSystem.load_from_response(res.body["acs_system"]) end def list(connected_account_id: nil) - request_seam_object( - :post, - "/acs/systems/list", - Seam::Resources::AcsSystem, - "acs_systems", - body: {connected_account_id: connected_account_id}.compact - ) + res = @client.post("/acs/systems/list", {connected_account_id: connected_account_id}.compact) + + Seam::Resources::AcsSystem.load_from_response(res.body["acs_systems"]) end def list_compatible_credential_manager_acs_systems(acs_system_id:) - request_seam_object( - :post, - "/acs/systems/list_compatible_credential_manager_acs_systems", - Seam::Resources::AcsSystem, - "acs_systems", - body: {acs_system_id: acs_system_id}.compact - ) + res = @client.post("/acs/systems/list_compatible_credential_manager_acs_systems", {acs_system_id: acs_system_id}.compact) + + Seam::Resources::AcsSystem.load_from_response(res.body["acs_systems"]) end end end diff --git a/lib/seam/routes/clients/acs_users.rb b/lib/seam/routes/clients/acs_users.rb index 323cb95..7533b51 100644 --- a/lib/seam/routes/clients/acs_users.rb +++ b/lib/seam/routes/clients/acs_users.rb @@ -2,113 +2,74 @@ module Seam module Clients - class AcsUsers < BaseClient + class AcsUsers + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def add_to_access_group(acs_access_group_id:, acs_user_id:) - request_seam( - :post, - "/acs/users/add_to_access_group", - body: {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/add_to_access_group", {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact) nil end def create(acs_system_id:, access_schedule: nil, acs_access_group_ids: nil, email: nil, email_address: nil, full_name: nil, phone_number: nil, user_identity_id: nil) - request_seam_object( - :post, - "/acs/users/create", - Seam::Resources::AcsUser, - "acs_user", - body: {acs_system_id: acs_system_id, access_schedule: access_schedule, acs_access_group_ids: acs_access_group_ids, email: email, email_address: email_address, full_name: full_name, phone_number: phone_number, user_identity_id: user_identity_id}.compact - ) + res = @client.post("/acs/users/create", {acs_system_id: acs_system_id, access_schedule: access_schedule, acs_access_group_ids: acs_access_group_ids, email: email, email_address: email_address, full_name: full_name, phone_number: phone_number, user_identity_id: user_identity_id}.compact) + + Seam::Resources::AcsUser.load_from_response(res.body["acs_user"]) end def delete(acs_user_id:) - request_seam( - :post, - "/acs/users/delete", - body: {acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/delete", {acs_user_id: acs_user_id}.compact) nil end def get(acs_user_id:) - request_seam_object( - :post, - "/acs/users/get", - Seam::Resources::AcsUser, - "acs_user", - body: {acs_user_id: acs_user_id}.compact - ) + res = @client.post("/acs/users/get", {acs_user_id: acs_user_id}.compact) + + Seam::Resources::AcsUser.load_from_response(res.body["acs_user"]) end def list(acs_system_id: nil, created_before: nil, limit: nil, user_identity_email_address: nil, user_identity_id: nil, user_identity_phone_number: nil) - request_seam_object( - :post, - "/acs/users/list", - Seam::Resources::AcsUser, - "acs_users", - body: {acs_system_id: acs_system_id, created_before: created_before, limit: limit, user_identity_email_address: user_identity_email_address, user_identity_id: user_identity_id, user_identity_phone_number: user_identity_phone_number}.compact - ) + res = @client.post("/acs/users/list", {acs_system_id: acs_system_id, created_before: created_before, limit: limit, user_identity_email_address: user_identity_email_address, user_identity_id: user_identity_id, user_identity_phone_number: user_identity_phone_number}.compact) + + Seam::Resources::AcsUser.load_from_response(res.body["acs_users"]) end def list_accessible_entrances(acs_user_id:) - request_seam_object( - :post, - "/acs/users/list_accessible_entrances", - Seam::Resources::AcsEntrance, - "acs_entrances", - body: {acs_user_id: acs_user_id}.compact - ) + res = @client.post("/acs/users/list_accessible_entrances", {acs_user_id: acs_user_id}.compact) + + Seam::Resources::AcsEntrance.load_from_response(res.body["acs_entrances"]) end def remove_from_access_group(acs_access_group_id:, acs_user_id:) - request_seam( - :post, - "/acs/users/remove_from_access_group", - body: {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/remove_from_access_group", {acs_access_group_id: acs_access_group_id, acs_user_id: acs_user_id}.compact) nil end def revoke_access_to_all_entrances(acs_user_id:) - request_seam( - :post, - "/acs/users/revoke_access_to_all_entrances", - body: {acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/revoke_access_to_all_entrances", {acs_user_id: acs_user_id}.compact) nil end def suspend(acs_user_id:) - request_seam( - :post, - "/acs/users/suspend", - body: {acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/suspend", {acs_user_id: acs_user_id}.compact) nil end def unsuspend(acs_user_id:) - request_seam( - :post, - "/acs/users/unsuspend", - body: {acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/unsuspend", {acs_user_id: acs_user_id}.compact) nil end def update(acs_user_id:, access_schedule: nil, email: nil, email_address: nil, full_name: nil, hid_acs_system_id: nil, phone_number: nil) - request_seam( - :post, - "/acs/users/update", - body: {acs_user_id: acs_user_id, access_schedule: access_schedule, email: email, email_address: email_address, full_name: full_name, hid_acs_system_id: hid_acs_system_id, phone_number: phone_number}.compact - ) + @client.post("/acs/users/update", {acs_user_id: acs_user_id, access_schedule: access_schedule, email: email, email_address: email_address, full_name: full_name, hid_acs_system_id: hid_acs_system_id, phone_number: phone_number}.compact) nil end diff --git a/lib/seam/routes/clients/acs_users_unmanaged.rb b/lib/seam/routes/clients/acs_users_unmanaged.rb index da95c9c..da2f597 100644 --- a/lib/seam/routes/clients/acs_users_unmanaged.rb +++ b/lib/seam/routes/clients/acs_users_unmanaged.rb @@ -2,23 +2,20 @@ module Seam module Clients - class AcsUsersUnmanaged < BaseClient + class AcsUsersUnmanaged + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(acs_user_id:) - request_seam( - :post, - "/acs/users/unmanaged/get", - body: {acs_user_id: acs_user_id}.compact - ) + @client.post("/acs/users/unmanaged/get", {acs_user_id: acs_user_id}.compact) nil end def list(acs_system_id: nil, limit: nil, user_identity_email_address: nil, user_identity_id: nil, user_identity_phone_number: nil) - request_seam( - :post, - "/acs/users/unmanaged/list", - body: {acs_system_id: acs_system_id, limit: limit, user_identity_email_address: user_identity_email_address, user_identity_id: user_identity_id, user_identity_phone_number: user_identity_phone_number}.compact - ) + @client.post("/acs/users/unmanaged/list", {acs_system_id: acs_system_id, limit: limit, user_identity_email_address: user_identity_email_address, user_identity_id: user_identity_id, user_identity_phone_number: user_identity_phone_number}.compact) nil end diff --git a/lib/seam/routes/clients/action_attempts.rb b/lib/seam/routes/clients/action_attempts.rb index d9bc0ff..b9d44d1 100644 --- a/lib/seam/routes/clients/action_attempts.rb +++ b/lib/seam/routes/clients/action_attempts.rb @@ -4,27 +4,24 @@ module Seam module Clients - class ActionAttempts < BaseClient + class ActionAttempts + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(action_attempt_id:, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/action_attempts/get", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {action_attempt_id: action_attempt_id}.compact - ) + res = @client.post("/action_attempts/get", {action_attempt_id: action_attempt_id}.compact) - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def list(action_attempt_ids:) - request_seam_object( - :post, - "/action_attempts/list", - Seam::Resources::ActionAttempt, - "action_attempts", - body: {action_attempt_ids: action_attempt_ids}.compact - ) + res = @client.post("/action_attempts/list", {action_attempt_ids: action_attempt_ids}.compact) + + Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempts"]) end end end diff --git a/lib/seam/routes/clients/client_sessions.rb b/lib/seam/routes/clients/client_sessions.rb index 16ca468..1dea99d 100644 --- a/lib/seam/routes/clients/client_sessions.rb +++ b/lib/seam/routes/clients/client_sessions.rb @@ -2,73 +2,50 @@ module Seam module Clients - class ClientSessions < BaseClient + class ClientSessions + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create(connect_webview_ids: nil, connected_account_ids: nil, expires_at: nil, user_identifier_key: nil, user_identity_ids: nil) - request_seam_object( - :post, - "/client_sessions/create", - Seam::Resources::ClientSession, - "client_session", - body: {connect_webview_ids: connect_webview_ids, connected_account_ids: connected_account_ids, expires_at: expires_at, user_identifier_key: user_identifier_key, user_identity_ids: user_identity_ids}.compact - ) + res = @client.post("/client_sessions/create", {connect_webview_ids: connect_webview_ids, connected_account_ids: connected_account_ids, expires_at: expires_at, user_identifier_key: user_identifier_key, user_identity_ids: user_identity_ids}.compact) + + Seam::Resources::ClientSession.load_from_response(res.body["client_session"]) end def delete(client_session_id:) - request_seam( - :post, - "/client_sessions/delete", - body: {client_session_id: client_session_id}.compact - ) + @client.post("/client_sessions/delete", {client_session_id: client_session_id}.compact) nil end def get(client_session_id: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/client_sessions/get", - Seam::Resources::ClientSession, - "client_session", - body: {client_session_id: client_session_id, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/client_sessions/get", {client_session_id: client_session_id, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::ClientSession.load_from_response(res.body["client_session"]) end def get_or_create(connect_webview_ids: nil, connected_account_ids: nil, expires_at: nil, user_identifier_key: nil, user_identity_ids: nil) - request_seam_object( - :post, - "/client_sessions/get_or_create", - Seam::Resources::ClientSession, - "client_session", - body: {connect_webview_ids: connect_webview_ids, connected_account_ids: connected_account_ids, expires_at: expires_at, user_identifier_key: user_identifier_key, user_identity_ids: user_identity_ids}.compact - ) + res = @client.post("/client_sessions/get_or_create", {connect_webview_ids: connect_webview_ids, connected_account_ids: connected_account_ids, expires_at: expires_at, user_identifier_key: user_identifier_key, user_identity_ids: user_identity_ids}.compact) + + Seam::Resources::ClientSession.load_from_response(res.body["client_session"]) end def grant_access(client_session_id: nil, connect_webview_ids: nil, connected_account_ids: nil, user_identifier_key: nil, user_identity_ids: nil) - request_seam( - :post, - "/client_sessions/grant_access", - body: {client_session_id: client_session_id, connect_webview_ids: connect_webview_ids, connected_account_ids: connected_account_ids, user_identifier_key: user_identifier_key, user_identity_ids: user_identity_ids}.compact - ) + @client.post("/client_sessions/grant_access", {client_session_id: client_session_id, connect_webview_ids: connect_webview_ids, connected_account_ids: connected_account_ids, user_identifier_key: user_identifier_key, user_identity_ids: user_identity_ids}.compact) nil end def list(client_session_id: nil, connect_webview_id: nil, user_identifier_key: nil, user_identity_id: nil, without_user_identifier_key: nil) - request_seam_object( - :post, - "/client_sessions/list", - Seam::Resources::ClientSession, - "client_sessions", - body: {client_session_id: client_session_id, connect_webview_id: connect_webview_id, user_identifier_key: user_identifier_key, user_identity_id: user_identity_id, without_user_identifier_key: without_user_identifier_key}.compact - ) + res = @client.post("/client_sessions/list", {client_session_id: client_session_id, connect_webview_id: connect_webview_id, user_identifier_key: user_identifier_key, user_identity_id: user_identity_id, without_user_identifier_key: without_user_identifier_key}.compact) + + Seam::Resources::ClientSession.load_from_response(res.body["client_sessions"]) end def revoke(client_session_id:) - request_seam( - :post, - "/client_sessions/revoke", - body: {client_session_id: client_session_id}.compact - ) + @client.post("/client_sessions/revoke", {client_session_id: client_session_id}.compact) nil end diff --git a/lib/seam/routes/clients/connect_webviews.rb b/lib/seam/routes/clients/connect_webviews.rb index bbfcf96..59a79fe 100644 --- a/lib/seam/routes/clients/connect_webviews.rb +++ b/lib/seam/routes/clients/connect_webviews.rb @@ -2,45 +2,34 @@ module Seam module Clients - class ConnectWebviews < BaseClient + class ConnectWebviews + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create(accepted_providers: nil, automatically_manage_new_devices: nil, custom_metadata: nil, custom_redirect_failure_url: nil, custom_redirect_url: nil, device_selection_mode: nil, provider_category: nil, wait_for_device_creation: nil) - request_seam_object( - :post, - "/connect_webviews/create", - Seam::Resources::ConnectWebview, - "connect_webview", - body: {accepted_providers: accepted_providers, automatically_manage_new_devices: automatically_manage_new_devices, custom_metadata: custom_metadata, custom_redirect_failure_url: custom_redirect_failure_url, custom_redirect_url: custom_redirect_url, device_selection_mode: device_selection_mode, provider_category: provider_category, wait_for_device_creation: wait_for_device_creation}.compact - ) + res = @client.post("/connect_webviews/create", {accepted_providers: accepted_providers, automatically_manage_new_devices: automatically_manage_new_devices, custom_metadata: custom_metadata, custom_redirect_failure_url: custom_redirect_failure_url, custom_redirect_url: custom_redirect_url, device_selection_mode: device_selection_mode, provider_category: provider_category, wait_for_device_creation: wait_for_device_creation}.compact) + + Seam::Resources::ConnectWebview.load_from_response(res.body["connect_webview"]) end def delete(connect_webview_id:) - request_seam( - :post, - "/connect_webviews/delete", - body: {connect_webview_id: connect_webview_id}.compact - ) + @client.post("/connect_webviews/delete", {connect_webview_id: connect_webview_id}.compact) nil end def get(connect_webview_id:) - request_seam_object( - :post, - "/connect_webviews/get", - Seam::Resources::ConnectWebview, - "connect_webview", - body: {connect_webview_id: connect_webview_id}.compact - ) + res = @client.post("/connect_webviews/get", {connect_webview_id: connect_webview_id}.compact) + + Seam::Resources::ConnectWebview.load_from_response(res.body["connect_webview"]) end def list(custom_metadata_has: nil, limit: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/connect_webviews/list", - Seam::Resources::ConnectWebview, - "connect_webviews", - body: {custom_metadata_has: custom_metadata_has, limit: limit, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/connect_webviews/list", {custom_metadata_has: custom_metadata_has, limit: limit, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::ConnectWebview.load_from_response(res.body["connect_webviews"]) end end end diff --git a/lib/seam/routes/clients/connected_accounts.rb b/lib/seam/routes/clients/connected_accounts.rb index 66e6311..580e09d 100644 --- a/lib/seam/routes/clients/connected_accounts.rb +++ b/lib/seam/routes/clients/connected_accounts.rb @@ -2,43 +2,32 @@ module Seam module Clients - class ConnectedAccounts < BaseClient + class ConnectedAccounts + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def delete(connected_account_id:, sync: nil) - request_seam( - :post, - "/connected_accounts/delete", - body: {connected_account_id: connected_account_id, sync: sync}.compact - ) + @client.post("/connected_accounts/delete", {connected_account_id: connected_account_id, sync: sync}.compact) nil end def get(connected_account_id: nil, email: nil) - request_seam_object( - :post, - "/connected_accounts/get", - Seam::Resources::ConnectedAccount, - "connected_account", - body: {connected_account_id: connected_account_id, email: email}.compact - ) + res = @client.post("/connected_accounts/get", {connected_account_id: connected_account_id, email: email}.compact) + + Seam::Resources::ConnectedAccount.load_from_response(res.body["connected_account"]) end def list(custom_metadata_has: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/connected_accounts/list", - Seam::Resources::ConnectedAccount, - "connected_accounts", - body: {custom_metadata_has: custom_metadata_has, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/connected_accounts/list", {custom_metadata_has: custom_metadata_has, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::ConnectedAccount.load_from_response(res.body["connected_accounts"]) end def update(connected_account_id:, automatically_manage_new_devices: nil, custom_metadata: nil) - request_seam( - :post, - "/connected_accounts/update", - body: {connected_account_id: connected_account_id, automatically_manage_new_devices: automatically_manage_new_devices, custom_metadata: custom_metadata}.compact - ) + @client.post("/connected_accounts/update", {connected_account_id: connected_account_id, automatically_manage_new_devices: automatically_manage_new_devices, custom_metadata: custom_metadata}.compact) nil end diff --git a/lib/seam/routes/clients/devices.rb b/lib/seam/routes/clients/devices.rb index 72104f7..29d9ada 100644 --- a/lib/seam/routes/clients/devices.rb +++ b/lib/seam/routes/clients/devices.rb @@ -2,61 +2,46 @@ module Seam module Clients - class Devices < BaseClient + class Devices + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def simulate - @simulate ||= Seam::Clients::DevicesSimulate.new(self) + @simulate ||= Seam::Clients::DevicesSimulate.new(client: @client, defaults: @defaults) end def unmanaged - @unmanaged ||= Seam::Clients::DevicesUnmanaged.new(self) + @unmanaged ||= Seam::Clients::DevicesUnmanaged.new(client: @client, defaults: @defaults) end def delete(device_id:) - request_seam( - :post, - "/devices/delete", - body: {device_id: device_id}.compact - ) + @client.post("/devices/delete", {device_id: device_id}.compact) nil end def get(device_id: nil, name: nil) - request_seam_object( - :post, - "/devices/get", - Seam::Resources::Device, - "device", - body: {device_id: device_id, name: name}.compact - ) + res = @client.post("/devices/get", {device_id: device_id, name: name}.compact) + + Seam::Resources::Device.load_from_response(res.body["device"]) end def list(connect_webview_id: nil, connected_account_id: nil, connected_account_ids: nil, created_before: nil, custom_metadata_has: nil, device_ids: nil, device_types: nil, exclude_if: nil, include_if: nil, limit: nil, manufacturer: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/devices/list", - Seam::Resources::Device, - "devices", - body: {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/devices/list", {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::Device.load_from_response(res.body["devices"]) end def list_device_providers(provider_category: nil) - request_seam_object( - :post, - "/devices/list_device_providers", - Seam::Resources::DeviceProvider, - "device_providers", - body: {provider_category: provider_category}.compact - ) + res = @client.post("/devices/list_device_providers", {provider_category: provider_category}.compact) + + Seam::Resources::DeviceProvider.load_from_response(res.body["device_providers"]) end def update(device_id:, custom_metadata: nil, is_managed: nil, name: nil, properties: nil) - request_seam( - :post, - "/devices/update", - body: {device_id: device_id, custom_metadata: custom_metadata, is_managed: is_managed, name: name, properties: properties}.compact - ) + @client.post("/devices/update", {device_id: device_id, custom_metadata: custom_metadata, is_managed: is_managed, name: name, properties: properties}.compact) nil end diff --git a/lib/seam/routes/clients/devices_simulate.rb b/lib/seam/routes/clients/devices_simulate.rb index 6fb9c9e..c4c0d8b 100644 --- a/lib/seam/routes/clients/devices_simulate.rb +++ b/lib/seam/routes/clients/devices_simulate.rb @@ -2,33 +2,26 @@ module Seam module Clients - class DevicesSimulate < BaseClient + class DevicesSimulate + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def connect(device_id:) - request_seam( - :post, - "/devices/simulate/connect", - body: {device_id: device_id}.compact - ) + @client.post("/devices/simulate/connect", {device_id: device_id}.compact) nil end def disconnect(device_id:) - request_seam( - :post, - "/devices/simulate/disconnect", - body: {device_id: device_id}.compact - ) + @client.post("/devices/simulate/disconnect", {device_id: device_id}.compact) nil end def remove(device_id:) - request_seam( - :post, - "/devices/simulate/remove", - body: {device_id: device_id}.compact - ) + @client.post("/devices/simulate/remove", {device_id: device_id}.compact) nil end diff --git a/lib/seam/routes/clients/devices_unmanaged.rb b/lib/seam/routes/clients/devices_unmanaged.rb index 745c174..3cfad07 100644 --- a/lib/seam/routes/clients/devices_unmanaged.rb +++ b/lib/seam/routes/clients/devices_unmanaged.rb @@ -2,33 +2,26 @@ module Seam module Clients - class DevicesUnmanaged < BaseClient + class DevicesUnmanaged + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(device_id: nil, name: nil) - request_seam_object( - :post, - "/devices/unmanaged/get", - Seam::Resources::UnmanagedDevice, - "device", - body: {device_id: device_id, name: name}.compact - ) + res = @client.post("/devices/unmanaged/get", {device_id: device_id, name: name}.compact) + + Seam::Resources::UnmanagedDevice.load_from_response(res.body["device"]) end def list(connect_webview_id: nil, connected_account_id: nil, connected_account_ids: nil, created_before: nil, custom_metadata_has: nil, device_ids: nil, device_types: nil, exclude_if: nil, include_if: nil, limit: nil, manufacturer: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/devices/unmanaged/list", - Seam::Resources::UnmanagedDevice, - "devices", - body: {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/devices/unmanaged/list", {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::UnmanagedDevice.load_from_response(res.body["devices"]) end def update(device_id:, is_managed:) - request_seam( - :post, - "/devices/unmanaged/update", - body: {device_id: device_id, is_managed: is_managed}.compact - ) + @client.post("/devices/unmanaged/update", {device_id: device_id, is_managed: is_managed}.compact) nil end diff --git a/lib/seam/routes/clients/events.rb b/lib/seam/routes/clients/events.rb index 67813bc..dca27d5 100644 --- a/lib/seam/routes/clients/events.rb +++ b/lib/seam/routes/clients/events.rb @@ -2,25 +2,22 @@ module Seam module Clients - class Events < BaseClient + class Events + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(device_id: nil, event_id: nil, event_type: nil) - request_seam_object( - :post, - "/events/get", - Seam::Resources::SeamEvent, - "event", - body: {device_id: device_id, event_id: event_id, event_type: event_type}.compact - ) + res = @client.post("/events/get", {device_id: device_id, event_id: event_id, event_type: event_type}.compact) + + Seam::Resources::SeamEvent.load_from_response(res.body["event"]) end def list(access_code_id: nil, access_code_ids: nil, acs_system_id: nil, acs_system_ids: nil, between: nil, connect_webview_id: nil, connected_account_id: nil, device_id: nil, device_ids: nil, event_type: nil, event_types: nil, limit: nil, since: nil, unstable_offset: nil) - request_seam_object( - :post, - "/events/list", - Seam::Resources::SeamEvent, - "events", - body: {access_code_id: access_code_id, access_code_ids: access_code_ids, acs_system_id: acs_system_id, acs_system_ids: acs_system_ids, between: between, connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, device_id: device_id, device_ids: device_ids, event_type: event_type, event_types: event_types, limit: limit, since: since, unstable_offset: unstable_offset}.compact - ) + res = @client.post("/events/list", {access_code_id: access_code_id, access_code_ids: access_code_ids, acs_system_id: acs_system_id, acs_system_ids: acs_system_ids, between: between, connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, device_id: device_id, device_ids: device_ids, event_type: event_type, event_types: event_types, limit: limit, since: since, unstable_offset: unstable_offset}.compact) + + Seam::Resources::SeamEvent.load_from_response(res.body["events"]) end end end diff --git a/lib/seam/routes/clients/index.rb b/lib/seam/routes/clients/index.rb index 7625080..d08e382 100644 --- a/lib/seam/routes/clients/index.rb +++ b/lib/seam/routes/clients/index.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "seam/base_client" require_relative "access_codes" require_relative "access_codes_simulate" require_relative "access_codes_unmanaged" diff --git a/lib/seam/routes/clients/locks.rb b/lib/seam/routes/clients/locks.rb index 70b6570..cd1cb37 100644 --- a/lib/seam/routes/clients/locks.rb +++ b/lib/seam/routes/clients/locks.rb @@ -4,49 +4,38 @@ module Seam module Clients - class Locks < BaseClient + class Locks + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(device_id: nil, name: nil) - request_seam_object( - :post, - "/locks/get", - Seam::Resources::Device, - "device", - body: {device_id: device_id, name: name}.compact - ) + res = @client.post("/locks/get", {device_id: device_id, name: name}.compact) + + Seam::Resources::Device.load_from_response(res.body["device"]) end def list(connect_webview_id: nil, connected_account_id: nil, connected_account_ids: nil, created_before: nil, custom_metadata_has: nil, device_ids: nil, device_types: nil, exclude_if: nil, include_if: nil, limit: nil, manufacturer: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/locks/list", - Seam::Resources::Device, - "devices", - body: {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/locks/list", {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::Device.load_from_response(res.body["devices"]) end def lock_door(device_id:, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/locks/lock_door", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/locks/lock_door", {device_id: device_id, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def unlock_door(device_id:, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/locks/unlock_door", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/locks/unlock_door", {device_id: device_id, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end end end diff --git a/lib/seam/routes/clients/networks.rb b/lib/seam/routes/clients/networks.rb index fef8687..720c5f0 100644 --- a/lib/seam/routes/clients/networks.rb +++ b/lib/seam/routes/clients/networks.rb @@ -2,25 +2,22 @@ module Seam module Clients - class Networks < BaseClient + class Networks + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def get(network_id:) - request_seam_object( - :post, - "/networks/get", - Seam::Resources::Network, - "network", - body: {network_id: network_id}.compact - ) + res = @client.post("/networks/get", {network_id: network_id}.compact) + + Seam::Resources::Network.load_from_response(res.body["network"]) end def list - request_seam_object( - :post, - "/networks/list", - Seam::Resources::Network, - "networks", - body: {}.compact - ) + res = @client.post("/networks/list") + + Seam::Resources::Network.load_from_response(res.body["networks"]) end end end diff --git a/lib/seam/routes/clients/noise_sensors.rb b/lib/seam/routes/clients/noise_sensors.rb index 771ea81..11d3ab2 100644 --- a/lib/seam/routes/clients/noise_sensors.rb +++ b/lib/seam/routes/clients/noise_sensors.rb @@ -2,23 +2,24 @@ module Seam module Clients - class NoiseSensors < BaseClient + class NoiseSensors + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def noise_thresholds - @noise_thresholds ||= Seam::Clients::NoiseSensorsNoiseThresholds.new(self) + @noise_thresholds ||= Seam::Clients::NoiseSensorsNoiseThresholds.new(client: @client, defaults: @defaults) end def simulate - @simulate ||= Seam::Clients::NoiseSensorsSimulate.new(self) + @simulate ||= Seam::Clients::NoiseSensorsSimulate.new(client: @client, defaults: @defaults) end def list(connect_webview_id: nil, connected_account_id: nil, connected_account_ids: nil, created_before: nil, custom_metadata_has: nil, device_ids: nil, device_types: nil, exclude_if: nil, include_if: nil, limit: nil, manufacturer: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/noise_sensors/list", - Seam::Resources::Device, - "devices", - body: {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/noise_sensors/list", {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::Device.load_from_response(res.body["devices"]) end end end diff --git a/lib/seam/routes/clients/noise_sensors_noise_thresholds.rb b/lib/seam/routes/clients/noise_sensors_noise_thresholds.rb index 2be00f0..eb4ed62 100644 --- a/lib/seam/routes/clients/noise_sensors_noise_thresholds.rb +++ b/lib/seam/routes/clients/noise_sensors_noise_thresholds.rb @@ -2,53 +2,38 @@ module Seam module Clients - class NoiseSensorsNoiseThresholds < BaseClient + class NoiseSensorsNoiseThresholds + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create(device_id:, ends_daily_at:, starts_daily_at:, name: nil, noise_threshold_decibels: nil, noise_threshold_nrs: nil, sync: nil) - request_seam_object( - :post, - "/noise_sensors/noise_thresholds/create", - Seam::Resources::NoiseThreshold, - "noise_threshold", - body: {device_id: device_id, ends_daily_at: ends_daily_at, starts_daily_at: starts_daily_at, name: name, noise_threshold_decibels: noise_threshold_decibels, noise_threshold_nrs: noise_threshold_nrs, sync: sync}.compact - ) + res = @client.post("/noise_sensors/noise_thresholds/create", {device_id: device_id, ends_daily_at: ends_daily_at, starts_daily_at: starts_daily_at, name: name, noise_threshold_decibels: noise_threshold_decibels, noise_threshold_nrs: noise_threshold_nrs, sync: sync}.compact) + + Seam::Resources::NoiseThreshold.load_from_response(res.body["noise_threshold"]) end def delete(device_id:, noise_threshold_id:, sync: nil) - request_seam( - :post, - "/noise_sensors/noise_thresholds/delete", - body: {device_id: device_id, noise_threshold_id: noise_threshold_id, sync: sync}.compact - ) + @client.post("/noise_sensors/noise_thresholds/delete", {device_id: device_id, noise_threshold_id: noise_threshold_id, sync: sync}.compact) nil end def get(noise_threshold_id:) - request_seam_object( - :post, - "/noise_sensors/noise_thresholds/get", - Seam::Resources::NoiseThreshold, - "noise_threshold", - body: {noise_threshold_id: noise_threshold_id}.compact - ) + res = @client.post("/noise_sensors/noise_thresholds/get", {noise_threshold_id: noise_threshold_id}.compact) + + Seam::Resources::NoiseThreshold.load_from_response(res.body["noise_threshold"]) end def list(device_id:, is_programmed: nil) - request_seam_object( - :post, - "/noise_sensors/noise_thresholds/list", - Seam::Resources::NoiseThreshold, - "noise_thresholds", - body: {device_id: device_id, is_programmed: is_programmed}.compact - ) + res = @client.post("/noise_sensors/noise_thresholds/list", {device_id: device_id, is_programmed: is_programmed}.compact) + + Seam::Resources::NoiseThreshold.load_from_response(res.body["noise_thresholds"]) end def update(device_id:, noise_threshold_id:, ends_daily_at: nil, name: nil, noise_threshold_decibels: nil, noise_threshold_nrs: nil, starts_daily_at: nil, sync: nil) - request_seam( - :post, - "/noise_sensors/noise_thresholds/update", - body: {device_id: device_id, noise_threshold_id: noise_threshold_id, ends_daily_at: ends_daily_at, name: name, noise_threshold_decibels: noise_threshold_decibels, noise_threshold_nrs: noise_threshold_nrs, starts_daily_at: starts_daily_at, sync: sync}.compact - ) + @client.post("/noise_sensors/noise_thresholds/update", {device_id: device_id, noise_threshold_id: noise_threshold_id, ends_daily_at: ends_daily_at, name: name, noise_threshold_decibels: noise_threshold_decibels, noise_threshold_nrs: noise_threshold_nrs, starts_daily_at: starts_daily_at, sync: sync}.compact) nil end diff --git a/lib/seam/routes/clients/noise_sensors_simulate.rb b/lib/seam/routes/clients/noise_sensors_simulate.rb index a992a73..6a461fd 100644 --- a/lib/seam/routes/clients/noise_sensors_simulate.rb +++ b/lib/seam/routes/clients/noise_sensors_simulate.rb @@ -2,13 +2,14 @@ module Seam module Clients - class NoiseSensorsSimulate < BaseClient + class NoiseSensorsSimulate + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def trigger_noise_threshold(device_id:) - request_seam( - :post, - "/noise_sensors/simulate/trigger_noise_threshold", - body: {device_id: device_id}.compact - ) + @client.post("/noise_sensors/simulate/trigger_noise_threshold", {device_id: device_id}.compact) nil end diff --git a/lib/seam/routes/clients/phones.rb b/lib/seam/routes/clients/phones.rb index 00f0dd5..dd43346 100644 --- a/lib/seam/routes/clients/phones.rb +++ b/lib/seam/routes/clients/phones.rb @@ -2,29 +2,26 @@ module Seam module Clients - class Phones < BaseClient + class Phones + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def simulate - @simulate ||= Seam::Clients::PhonesSimulate.new(self) + @simulate ||= Seam::Clients::PhonesSimulate.new(client: @client, defaults: @defaults) end def deactivate(device_id:) - request_seam( - :post, - "/phones/deactivate", - body: {device_id: device_id}.compact - ) + @client.post("/phones/deactivate", {device_id: device_id}.compact) nil end def list(acs_credential_id: nil, owner_user_identity_id: nil) - request_seam_object( - :post, - "/phones/list", - Seam::Resources::Phone, - "phones", - body: {acs_credential_id: acs_credential_id, owner_user_identity_id: owner_user_identity_id}.compact - ) + res = @client.post("/phones/list", {acs_credential_id: acs_credential_id, owner_user_identity_id: owner_user_identity_id}.compact) + + Seam::Resources::Phone.load_from_response(res.body["phones"]) end end end diff --git a/lib/seam/routes/clients/phones_simulate.rb b/lib/seam/routes/clients/phones_simulate.rb index 747a7a9..53d346c 100644 --- a/lib/seam/routes/clients/phones_simulate.rb +++ b/lib/seam/routes/clients/phones_simulate.rb @@ -2,15 +2,16 @@ module Seam module Clients - class PhonesSimulate < BaseClient + class PhonesSimulate + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create_sandbox_phone(user_identity_id:, assa_abloy_metadata: nil, custom_sdk_installation_id: nil, phone_metadata: nil) - request_seam_object( - :post, - "/phones/simulate/create_sandbox_phone", - Seam::Resources::Phone, - "phone", - body: {user_identity_id: user_identity_id, assa_abloy_metadata: assa_abloy_metadata, custom_sdk_installation_id: custom_sdk_installation_id, phone_metadata: phone_metadata}.compact - ) + res = @client.post("/phones/simulate/create_sandbox_phone", {user_identity_id: user_identity_id, assa_abloy_metadata: assa_abloy_metadata, custom_sdk_installation_id: custom_sdk_installation_id, phone_metadata: phone_metadata}.compact) + + Seam::Resources::Phone.load_from_response(res.body["phone"]) end end end diff --git a/lib/seam/routes/clients/thermostats.rb b/lib/seam/routes/clients/thermostats.rb index 9178f30..aa60e74 100644 --- a/lib/seam/routes/clients/thermostats.rb +++ b/lib/seam/routes/clients/thermostats.rb @@ -4,149 +4,102 @@ module Seam module Clients - class Thermostats < BaseClient + class Thermostats + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def schedules - @schedules ||= Seam::Clients::ThermostatsSchedules.new(self) + @schedules ||= Seam::Clients::ThermostatsSchedules.new(client: @client, defaults: @defaults) end def activate_climate_preset(climate_preset_key:, device_id:, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/thermostats/activate_climate_preset", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {climate_preset_key: climate_preset_key, device_id: device_id}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/thermostats/activate_climate_preset", {climate_preset_key: climate_preset_key, device_id: device_id}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def cool(device_id:, cooling_set_point_celsius: nil, cooling_set_point_fahrenheit: nil, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/thermostats/cool", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/thermostats/cool", {device_id: device_id, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def create_climate_preset(climate_preset_key:, device_id:, manual_override_allowed:, cooling_set_point_celsius: nil, cooling_set_point_fahrenheit: nil, fan_mode_setting: nil, heating_set_point_celsius: nil, heating_set_point_fahrenheit: nil, hvac_mode_setting: nil, name: nil) - request_seam( - :post, - "/thermostats/create_climate_preset", - body: {climate_preset_key: climate_preset_key, device_id: device_id, manual_override_allowed: manual_override_allowed, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, fan_mode_setting: fan_mode_setting, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, hvac_mode_setting: hvac_mode_setting, name: name}.compact - ) + @client.post("/thermostats/create_climate_preset", {climate_preset_key: climate_preset_key, device_id: device_id, manual_override_allowed: manual_override_allowed, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, fan_mode_setting: fan_mode_setting, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, hvac_mode_setting: hvac_mode_setting, name: name}.compact) nil end def delete_climate_preset(climate_preset_key:, device_id:) - request_seam( - :post, - "/thermostats/delete_climate_preset", - body: {climate_preset_key: climate_preset_key, device_id: device_id}.compact - ) + @client.post("/thermostats/delete_climate_preset", {climate_preset_key: climate_preset_key, device_id: device_id}.compact) nil end def get(device_id: nil, name: nil) - request_seam_object( - :post, - "/thermostats/get", - Seam::Resources::Device, - "thermostat", - body: {device_id: device_id, name: name}.compact - ) + res = @client.post("/thermostats/get", {device_id: device_id, name: name}.compact) + + Seam::Resources::Device.load_from_response(res.body["thermostat"]) end def heat(device_id:, heating_set_point_celsius: nil, heating_set_point_fahrenheit: nil, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/thermostats/heat", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/thermostats/heat", {device_id: device_id, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def heat_cool(device_id:, cooling_set_point_celsius: nil, cooling_set_point_fahrenheit: nil, heating_set_point_celsius: nil, heating_set_point_fahrenheit: nil, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/thermostats/heat_cool", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/thermostats/heat_cool", {device_id: device_id, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def list(connect_webview_id: nil, connected_account_id: nil, connected_account_ids: nil, created_before: nil, custom_metadata_has: nil, device_ids: nil, device_types: nil, exclude_if: nil, include_if: nil, limit: nil, manufacturer: nil, user_identifier_key: nil) - request_seam_object( - :post, - "/thermostats/list", - Seam::Resources::Device, - "devices", - body: {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/thermostats/list", {connect_webview_id: connect_webview_id, connected_account_id: connected_account_id, connected_account_ids: connected_account_ids, created_before: created_before, custom_metadata_has: custom_metadata_has, device_ids: device_ids, device_types: device_types, exclude_if: exclude_if, include_if: include_if, limit: limit, manufacturer: manufacturer, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::Device.load_from_response(res.body["devices"]) end def off(device_id:, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/thermostats/off", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/thermostats/off", {device_id: device_id, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def set_fallback_climate_preset(climate_preset_key:, device_id:) - request_seam( - :post, - "/thermostats/set_fallback_climate_preset", - body: {climate_preset_key: climate_preset_key, device_id: device_id}.compact - ) + @client.post("/thermostats/set_fallback_climate_preset", {climate_preset_key: climate_preset_key, device_id: device_id}.compact) nil end def set_fan_mode(device_id:, fan_mode: nil, fan_mode_setting: nil, sync: nil, wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/thermostats/set_fan_mode", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {device_id: device_id, fan_mode: fan_mode, fan_mode_setting: fan_mode_setting, sync: sync}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/thermostats/set_fan_mode", {device_id: device_id, fan_mode: fan_mode, fan_mode_setting: fan_mode_setting, sync: sync}.compact) + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end def set_temperature_threshold(device_id:, lower_limit_celsius: nil, lower_limit_fahrenheit: nil, upper_limit_celsius: nil, upper_limit_fahrenheit: nil) - request_seam( - :post, - "/thermostats/set_temperature_threshold", - body: {device_id: device_id, lower_limit_celsius: lower_limit_celsius, lower_limit_fahrenheit: lower_limit_fahrenheit, upper_limit_celsius: upper_limit_celsius, upper_limit_fahrenheit: upper_limit_fahrenheit}.compact - ) + @client.post("/thermostats/set_temperature_threshold", {device_id: device_id, lower_limit_celsius: lower_limit_celsius, lower_limit_fahrenheit: lower_limit_fahrenheit, upper_limit_celsius: upper_limit_celsius, upper_limit_fahrenheit: upper_limit_fahrenheit}.compact) nil end def update_climate_preset(climate_preset_key:, device_id:, manual_override_allowed:, cooling_set_point_celsius: nil, cooling_set_point_fahrenheit: nil, fan_mode_setting: nil, heating_set_point_celsius: nil, heating_set_point_fahrenheit: nil, hvac_mode_setting: nil, name: nil) - request_seam( - :post, - "/thermostats/update_climate_preset", - body: {climate_preset_key: climate_preset_key, device_id: device_id, manual_override_allowed: manual_override_allowed, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, fan_mode_setting: fan_mode_setting, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, hvac_mode_setting: hvac_mode_setting, name: name}.compact - ) + @client.post("/thermostats/update_climate_preset", {climate_preset_key: climate_preset_key, device_id: device_id, manual_override_allowed: manual_override_allowed, cooling_set_point_celsius: cooling_set_point_celsius, cooling_set_point_fahrenheit: cooling_set_point_fahrenheit, fan_mode_setting: fan_mode_setting, heating_set_point_celsius: heating_set_point_celsius, heating_set_point_fahrenheit: heating_set_point_fahrenheit, hvac_mode_setting: hvac_mode_setting, name: name}.compact) nil end diff --git a/lib/seam/routes/clients/thermostats_schedules.rb b/lib/seam/routes/clients/thermostats_schedules.rb index 6b24a58..baa4d87 100644 --- a/lib/seam/routes/clients/thermostats_schedules.rb +++ b/lib/seam/routes/clients/thermostats_schedules.rb @@ -2,53 +2,38 @@ module Seam module Clients - class ThermostatsSchedules < BaseClient + class ThermostatsSchedules + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create(climate_preset_key:, device_id:, ends_at:, starts_at:, max_override_period_minutes: nil, name: nil) - request_seam_object( - :post, - "/thermostats/schedules/create", - Seam::Resources::ThermostatSchedule, - "thermostat_schedule", - body: {climate_preset_key: climate_preset_key, device_id: device_id, ends_at: ends_at, starts_at: starts_at, max_override_period_minutes: max_override_period_minutes, name: name}.compact - ) + res = @client.post("/thermostats/schedules/create", {climate_preset_key: climate_preset_key, device_id: device_id, ends_at: ends_at, starts_at: starts_at, max_override_period_minutes: max_override_period_minutes, name: name}.compact) + + Seam::Resources::ThermostatSchedule.load_from_response(res.body["thermostat_schedule"]) end def delete(thermostat_schedule_id:) - request_seam( - :post, - "/thermostats/schedules/delete", - body: {thermostat_schedule_id: thermostat_schedule_id}.compact - ) + @client.post("/thermostats/schedules/delete", {thermostat_schedule_id: thermostat_schedule_id}.compact) nil end def get(thermostat_schedule_id:) - request_seam_object( - :post, - "/thermostats/schedules/get", - Seam::Resources::ThermostatSchedule, - "thermostat_schedule", - body: {thermostat_schedule_id: thermostat_schedule_id}.compact - ) + res = @client.post("/thermostats/schedules/get", {thermostat_schedule_id: thermostat_schedule_id}.compact) + + Seam::Resources::ThermostatSchedule.load_from_response(res.body["thermostat_schedule"]) end def list(device_id:, user_identifier_key: nil) - request_seam_object( - :post, - "/thermostats/schedules/list", - Seam::Resources::ThermostatSchedule, - "thermostat_schedules", - body: {device_id: device_id, user_identifier_key: user_identifier_key}.compact - ) + res = @client.post("/thermostats/schedules/list", {device_id: device_id, user_identifier_key: user_identifier_key}.compact) + + Seam::Resources::ThermostatSchedule.load_from_response(res.body["thermostat_schedules"]) end def update(thermostat_schedule_id:, climate_preset_key: nil, ends_at: nil, max_override_period_minutes: nil, name: nil, starts_at: nil) - request_seam( - :post, - "/thermostats/schedules/update", - body: {thermostat_schedule_id: thermostat_schedule_id, climate_preset_key: climate_preset_key, ends_at: ends_at, max_override_period_minutes: max_override_period_minutes, name: name, starts_at: starts_at}.compact - ) + @client.post("/thermostats/schedules/update", {thermostat_schedule_id: thermostat_schedule_id, climate_preset_key: climate_preset_key, ends_at: ends_at, max_override_period_minutes: max_override_period_minutes, name: name, starts_at: starts_at}.compact) nil end diff --git a/lib/seam/routes/clients/user_identities.rb b/lib/seam/routes/clients/user_identities.rb index 246a907..cc7fcce 100644 --- a/lib/seam/routes/clients/user_identities.rb +++ b/lib/seam/routes/clients/user_identities.rb @@ -2,127 +2,84 @@ module Seam module Clients - class UserIdentities < BaseClient + class UserIdentities + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def enrollment_automations - @enrollment_automations ||= Seam::Clients::UserIdentitiesEnrollmentAutomations.new(self) + @enrollment_automations ||= Seam::Clients::UserIdentitiesEnrollmentAutomations.new(client: @client, defaults: @defaults) end def add_acs_user(acs_user_id:, user_identity_id:) - request_seam( - :post, - "/user_identities/add_acs_user", - body: {acs_user_id: acs_user_id, user_identity_id: user_identity_id}.compact - ) + @client.post("/user_identities/add_acs_user", {acs_user_id: acs_user_id, user_identity_id: user_identity_id}.compact) nil end def create(email_address: nil, full_name: nil, phone_number: nil, user_identity_key: nil) - request_seam_object( - :post, - "/user_identities/create", - Seam::Resources::UserIdentity, - "user_identity", - body: {email_address: email_address, full_name: full_name, phone_number: phone_number, user_identity_key: user_identity_key}.compact - ) + res = @client.post("/user_identities/create", {email_address: email_address, full_name: full_name, phone_number: phone_number, user_identity_key: user_identity_key}.compact) + + Seam::Resources::UserIdentity.load_from_response(res.body["user_identity"]) end def delete(user_identity_id:) - request_seam( - :post, - "/user_identities/delete", - body: {user_identity_id: user_identity_id}.compact - ) + @client.post("/user_identities/delete", {user_identity_id: user_identity_id}.compact) nil end def get(user_identity_id: nil, user_identity_key: nil) - request_seam_object( - :post, - "/user_identities/get", - Seam::Resources::UserIdentity, - "user_identity", - body: {user_identity_id: user_identity_id, user_identity_key: user_identity_key}.compact - ) + res = @client.post("/user_identities/get", {user_identity_id: user_identity_id, user_identity_key: user_identity_key}.compact) + + Seam::Resources::UserIdentity.load_from_response(res.body["user_identity"]) end def grant_access_to_device(device_id:, user_identity_id:) - request_seam( - :post, - "/user_identities/grant_access_to_device", - body: {device_id: device_id, user_identity_id: user_identity_id}.compact - ) + @client.post("/user_identities/grant_access_to_device", {device_id: device_id, user_identity_id: user_identity_id}.compact) nil end def list(credential_manager_acs_system_id: nil) - request_seam_object( - :post, - "/user_identities/list", - Seam::Resources::UserIdentity, - "user_identities", - body: {credential_manager_acs_system_id: credential_manager_acs_system_id}.compact - ) + res = @client.post("/user_identities/list", {credential_manager_acs_system_id: credential_manager_acs_system_id}.compact) + + Seam::Resources::UserIdentity.load_from_response(res.body["user_identities"]) end def list_accessible_devices(user_identity_id:) - request_seam_object( - :post, - "/user_identities/list_accessible_devices", - Seam::Resources::Device, - "devices", - body: {user_identity_id: user_identity_id}.compact - ) + res = @client.post("/user_identities/list_accessible_devices", {user_identity_id: user_identity_id}.compact) + + Seam::Resources::Device.load_from_response(res.body["devices"]) end def list_acs_systems(user_identity_id:) - request_seam_object( - :post, - "/user_identities/list_acs_systems", - Seam::Resources::AcsSystem, - "acs_systems", - body: {user_identity_id: user_identity_id}.compact - ) + res = @client.post("/user_identities/list_acs_systems", {user_identity_id: user_identity_id}.compact) + + Seam::Resources::AcsSystem.load_from_response(res.body["acs_systems"]) end def list_acs_users(user_identity_id:) - request_seam_object( - :post, - "/user_identities/list_acs_users", - Seam::Resources::AcsUser, - "acs_users", - body: {user_identity_id: user_identity_id}.compact - ) + res = @client.post("/user_identities/list_acs_users", {user_identity_id: user_identity_id}.compact) + + Seam::Resources::AcsUser.load_from_response(res.body["acs_users"]) end def remove_acs_user(acs_user_id:, user_identity_id:) - request_seam( - :post, - "/user_identities/remove_acs_user", - body: {acs_user_id: acs_user_id, user_identity_id: user_identity_id}.compact - ) + @client.post("/user_identities/remove_acs_user", {acs_user_id: acs_user_id, user_identity_id: user_identity_id}.compact) nil end def revoke_access_to_device(device_id:, user_identity_id:) - request_seam( - :post, - "/user_identities/revoke_access_to_device", - body: {device_id: device_id, user_identity_id: user_identity_id}.compact - ) + @client.post("/user_identities/revoke_access_to_device", {device_id: device_id, user_identity_id: user_identity_id}.compact) nil end def update(user_identity_id:, email_address: nil, full_name: nil, phone_number: nil, user_identity_key: nil) - request_seam( - :post, - "/user_identities/update", - body: {user_identity_id: user_identity_id, email_address: email_address, full_name: full_name, phone_number: phone_number, user_identity_key: user_identity_key}.compact - ) + @client.post("/user_identities/update", {user_identity_id: user_identity_id, email_address: email_address, full_name: full_name, phone_number: phone_number, user_identity_key: user_identity_key}.compact) nil end diff --git a/lib/seam/routes/clients/user_identities_enrollment_automations.rb b/lib/seam/routes/clients/user_identities_enrollment_automations.rb index d7caf2e..7ced256 100644 --- a/lib/seam/routes/clients/user_identities_enrollment_automations.rb +++ b/lib/seam/routes/clients/user_identities_enrollment_automations.rb @@ -2,45 +2,34 @@ module Seam module Clients - class UserIdentitiesEnrollmentAutomations < BaseClient + class UserIdentitiesEnrollmentAutomations + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def delete(enrollment_automation_id:) - request_seam( - :post, - "/user_identities/enrollment_automations/delete", - body: {enrollment_automation_id: enrollment_automation_id}.compact - ) + @client.post("/user_identities/enrollment_automations/delete", {enrollment_automation_id: enrollment_automation_id}.compact) nil end def get(enrollment_automation_id:) - request_seam_object( - :post, - "/user_identities/enrollment_automations/get", - Seam::Resources::EnrollmentAutomation, - "enrollment_automation", - body: {enrollment_automation_id: enrollment_automation_id}.compact - ) + res = @client.post("/user_identities/enrollment_automations/get", {enrollment_automation_id: enrollment_automation_id}.compact) + + Seam::Resources::EnrollmentAutomation.load_from_response(res.body["enrollment_automation"]) end def launch(credential_manager_acs_system_id:, user_identity_id:, acs_credential_pool_id: nil, create_credential_manager_user: nil, credential_manager_acs_user_id: nil) - request_seam( - :post, - "/user_identities/enrollment_automations/launch", - body: {credential_manager_acs_system_id: credential_manager_acs_system_id, user_identity_id: user_identity_id, acs_credential_pool_id: acs_credential_pool_id, create_credential_manager_user: create_credential_manager_user, credential_manager_acs_user_id: credential_manager_acs_user_id}.compact - ) + @client.post("/user_identities/enrollment_automations/launch", {credential_manager_acs_system_id: credential_manager_acs_system_id, user_identity_id: user_identity_id, acs_credential_pool_id: acs_credential_pool_id, create_credential_manager_user: create_credential_manager_user, credential_manager_acs_user_id: credential_manager_acs_user_id}.compact) nil end def list(user_identity_id:) - request_seam_object( - :post, - "/user_identities/enrollment_automations/list", - Seam::Resources::EnrollmentAutomation, - "enrollment_automations", - body: {user_identity_id: user_identity_id}.compact - ) + res = @client.post("/user_identities/enrollment_automations/list", {user_identity_id: user_identity_id}.compact) + + Seam::Resources::EnrollmentAutomation.load_from_response(res.body["enrollment_automations"]) end end end diff --git a/lib/seam/routes/clients/webhooks.rb b/lib/seam/routes/clients/webhooks.rb index d7e4e89..7269fd7 100644 --- a/lib/seam/routes/clients/webhooks.rb +++ b/lib/seam/routes/clients/webhooks.rb @@ -2,53 +2,38 @@ module Seam module Clients - class Webhooks < BaseClient + class Webhooks + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create(url:, event_types: nil) - request_seam_object( - :post, - "/webhooks/create", - Seam::Resources::Webhook, - "webhook", - body: {url: url, event_types: event_types}.compact - ) + res = @client.post("/webhooks/create", {url: url, event_types: event_types}.compact) + + Seam::Resources::Webhook.load_from_response(res.body["webhook"]) end def delete(webhook_id:) - request_seam( - :post, - "/webhooks/delete", - body: {webhook_id: webhook_id}.compact - ) + @client.post("/webhooks/delete", {webhook_id: webhook_id}.compact) nil end def get(webhook_id:) - request_seam_object( - :post, - "/webhooks/get", - Seam::Resources::Webhook, - "webhook", - body: {webhook_id: webhook_id}.compact - ) + res = @client.post("/webhooks/get", {webhook_id: webhook_id}.compact) + + Seam::Resources::Webhook.load_from_response(res.body["webhook"]) end def list - request_seam_object( - :post, - "/webhooks/list", - Seam::Resources::Webhook, - "webhooks", - body: {}.compact - ) + res = @client.post("/webhooks/list") + + Seam::Resources::Webhook.load_from_response(res.body["webhooks"]) end def update(event_types:, webhook_id:) - request_seam( - :post, - "/webhooks/update", - body: {event_types: event_types, webhook_id: webhook_id}.compact - ) + @client.post("/webhooks/update", {event_types: event_types, webhook_id: webhook_id}.compact) nil end diff --git a/lib/seam/routes/clients/workspaces.rb b/lib/seam/routes/clients/workspaces.rb index 7ca4827..13f19d7 100644 --- a/lib/seam/routes/clients/workspaces.rb +++ b/lib/seam/routes/clients/workspaces.rb @@ -4,47 +4,36 @@ module Seam module Clients - class Workspaces < BaseClient + class Workspaces + def initialize(client:, defaults:) + @client = client + @defaults = defaults + end + def create(name:, company_name: nil, connect_partner_name: nil, is_sandbox: nil, webview_logo_shape: nil, webview_primary_button_color: nil, webview_primary_button_text_color: nil) - request_seam_object( - :post, - "/workspaces/create", - Seam::Resources::Workspace, - "workspace", - body: {name: name, company_name: company_name, connect_partner_name: connect_partner_name, is_sandbox: is_sandbox, webview_logo_shape: webview_logo_shape, webview_primary_button_color: webview_primary_button_color, webview_primary_button_text_color: webview_primary_button_text_color}.compact - ) + res = @client.post("/workspaces/create", {name: name, company_name: company_name, connect_partner_name: connect_partner_name, is_sandbox: is_sandbox, webview_logo_shape: webview_logo_shape, webview_primary_button_color: webview_primary_button_color, webview_primary_button_text_color: webview_primary_button_text_color}.compact) + + Seam::Resources::Workspace.load_from_response(res.body["workspace"]) end def get - request_seam_object( - :post, - "/workspaces/get", - Seam::Resources::Workspace, - "workspace", - body: {}.compact - ) + res = @client.post("/workspaces/get") + + Seam::Resources::Workspace.load_from_response(res.body["workspace"]) end def list - request_seam_object( - :post, - "/workspaces/list", - Seam::Resources::Workspace, - "workspaces", - body: {}.compact - ) + res = @client.post("/workspaces/list") + + Seam::Resources::Workspace.load_from_response(res.body["workspaces"]) end def reset_sandbox(wait_for_action_attempt: nil) - action_attempt = request_seam_object( - :post, - "/workspaces/reset_sandbox", - Seam::Resources::ActionAttempt, - "action_attempt", - body: {}.compact - ) - - Helpers::ActionAttempt.decide_and_wait(action_attempt, @client, wait_for_action_attempt) + res = @client.post("/workspaces/reset_sandbox") + + wait_for_action_attempt = wait_for_action_attempt.nil? ? @defaults.wait_for_action_attempt : wait_for_action_attempt + + Helpers::ActionAttempt.decide_and_wait(Seam::Resources::ActionAttempt.load_from_response(res.body["action_attempt"]), @client, wait_for_action_attempt) end end end diff --git a/lib/seam/routes/routes.rb b/lib/seam/routes/routes.rb index 0b5beeb..5128458 100644 --- a/lib/seam/routes/routes.rb +++ b/lib/seam/routes/routes.rb @@ -3,85 +3,92 @@ module Seam module Routes def access_codes - @access_codes ||= Seam::Clients::AccessCodes.new(self) + @access_codes ||= Seam::Clients::AccessCodes.new(client: @client, defaults: @defaults) end def acs - @acs ||= Seam::Clients::Acs.new(self) + @acs ||= Seam::Clients::Acs.new(client: @client, defaults: @defaults) end def action_attempts - @action_attempts ||= Seam::Clients::ActionAttempts.new(self) + @action_attempts ||= Seam::Clients::ActionAttempts.new(client: @client, defaults: @defaults) end def client_sessions - @client_sessions ||= Seam::Clients::ClientSessions.new(self) + @client_sessions ||= Seam::Clients::ClientSessions.new(client: @client, defaults: @defaults) end def connect_webviews - @connect_webviews ||= Seam::Clients::ConnectWebviews.new(self) + @connect_webviews ||= Seam::Clients::ConnectWebviews.new(client: @client, defaults: @defaults) end def connected_accounts - @connected_accounts ||= Seam::Clients::ConnectedAccounts.new(self) + @connected_accounts ||= Seam::Clients::ConnectedAccounts.new(client: @client, defaults: @defaults) end def devices - @devices ||= Seam::Clients::Devices.new(self) + @devices ||= Seam::Clients::Devices.new(client: @client, defaults: @defaults) end def events - @events ||= Seam::Clients::Events.new(self) + @events ||= Seam::Clients::Events.new(client: @client, defaults: @defaults) end def locks - @locks ||= Seam::Clients::Locks.new(self) + @locks ||= Seam::Clients::Locks.new(client: @client, defaults: @defaults) end def networks - @networks ||= Seam::Clients::Networks.new(self) + @networks ||= Seam::Clients::Networks.new(client: @client, defaults: @defaults) end def noise_sensors - @noise_sensors ||= Seam::Clients::NoiseSensors.new(self) + @noise_sensors ||= Seam::Clients::NoiseSensors.new(client: @client, defaults: @defaults) end def phones - @phones ||= Seam::Clients::Phones.new(self) + @phones ||= Seam::Clients::Phones.new(client: @client, defaults: @defaults) end def thermostats - @thermostats ||= Seam::Clients::Thermostats.new(self) + @thermostats ||= Seam::Clients::Thermostats.new(client: @client, defaults: @defaults) end def user_identities - @user_identities ||= Seam::Clients::UserIdentities.new(self) + @user_identities ||= Seam::Clients::UserIdentities.new(client: @client, defaults: @defaults) end def webhooks - @webhooks ||= Seam::Clients::Webhooks.new(self) + @webhooks ||= Seam::Clients::Webhooks.new(client: @client, defaults: @defaults) end def workspaces - @workspaces ||= Seam::Clients::Workspaces.new(self) + @workspaces ||= Seam::Clients::Workspaces.new(client: @client, defaults: @defaults) end def health - request_seam(:get, "/health") + @client.get("/health") end # @deprecated Please use {#devices.unmanaged} instead. def unmanaged_devices warn "[DEPRECATION] 'unmanaged_devices' is deprecated. Please use 'devices.unmanaged' instead." - @unmanaged_devices ||= Seam::Clients::DevicesUnmanaged.new(self) + @unmanaged_devices ||= Seam::Clients::DevicesUnmanaged.new(client: @client, defaults: @defaults) end # @deprecated Please use {#access_codes.unmanaged} instead. def unmanaged_access_codes warn "[DEPRECATION] 'unmanaged_access_codes' is deprecated. Please use 'access_codes.unmanaged' instead." - @unmanaged_access_codes ||= Seam::Clients::AccessCodesUnmanaged.new(self) + @unmanaged_access_codes ||= Seam::Clients::AccessCodesUnmanaged.new(client: @client, defaults: @defaults) + end + + private + + def initialize_routes(client:, defaults:) + @client = client + @defaults = defaults end end end From e5cec058ad4a84c32cefe9ee2771c434d51ed68a Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 15:20:01 +0100 Subject: [PATCH 09/15] Remove outdated test --- spec/resources/action_attempt_spec.rb | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/spec/resources/action_attempt_spec.rb b/spec/resources/action_attempt_spec.rb index 7ce36af..6a05d05 100644 --- a/spec/resources/action_attempt_spec.rb +++ b/spec/resources/action_attempt_spec.rb @@ -33,20 +33,6 @@ described_class.decide_and_wait(action_attempt, seam, wait_options) end end - - context "when wait_for_action_attempt is nil" do - it "uses the seam's actual default wait_for_action_attempt value" do - seam_default = seam.defaults.wait_for_action_attempt - - if seam_default - expect(described_class).to receive(:wait_until_finished).with(action_attempt, seam) - else - expect(described_class).not_to receive(:wait_until_finished) - end - - described_class.decide_and_wait(action_attempt, seam, nil) - end - end end describe ".wait_until_finished" do From 50b3e350042578435048e5cb700e95c070b30acb Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 31 Oct 2024 15:28:21 +0100 Subject: [PATCH 10/15] Remove unnecessary default value --- lib/seam/request.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/seam/request.rb b/lib/seam/request.rb index d1e4089..0ade55c 100644 --- a/lib/seam/request.rb +++ b/lib/seam/request.rb @@ -51,7 +51,7 @@ def on_complete(env) status_code = env.status request_id = env.response_headers["seam-request-id"] - retry_statuses = (@retry_options || {}).fetch(:retry_statuses, []) + retry_statuses = @retry_options.fetch(:retry_statuses, []) if retry_statuses.include?(status_code) raise Faraday::RetriableResponse.new(nil, env.response) end From b35a6b17c89dd35e50de60682dd1de7d9c501457 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Tue, 5 Nov 2024 14:50:12 +0100 Subject: [PATCH 11/15] Use default Faraday::Response::RaiseError when response error is not seam api error --- lib/seam/request.rb | 56 +++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/seam/request.rb b/lib/seam/request.rb index 0ade55c..8223438 100644 --- a/lib/seam/request.rb +++ b/lib/seam/request.rb @@ -39,7 +39,7 @@ def self.default_headers } end - class ResponseMiddleware < Faraday::Middleware + class ResponseMiddleware < Faraday::Response::RaiseError def initialize(app, options = {}) super(app) @retry_options = options[:retry_options] @@ -49,35 +49,51 @@ def on_complete(env) return if env.success? status_code = env.status - request_id = env.response_headers["seam-request-id"] retry_statuses = @retry_options.fetch(:retry_statuses, []) if retry_statuses.include?(status_code) raise Faraday::RetriableResponse.new(nil, env.response) end + request_id = env.response_headers["seam-request-id"] + raise Http::UnauthorizedError.new(request_id) if status_code == 401 - body = begin - JSON.parse(env.body) - rescue - {} - end - error = body["error"] || {} - error_type = error["type"] || "unknown_error" - error_message = error["message"] || "Unknown error" - error_details = { - type: error_type, - message: error_message, - data: error["data"] - } - - if error_type == "invalid_input" - error_details["validation_errors"] = error["validation_errors"] - raise Http::InvalidInputError.new(error_details, status_code, request_id) + if seam_api_error_response?(env) + body = JSON.parse(env.body) + error = body["error"] + error_details = { + type: error["type"] || "unknown_error", + message: error["message"] || "Unknown error", + data: error["data"] + } + + if error["type"] == "invalid_input" + error_details["validation_errors"] = error["validation_errors"] + raise Http::InvalidInputError.new(error_details, status_code, request_id) + end + + raise Http::ApiError.new(error_details, status_code, request_id) end - raise Http::ApiError.new(error_details, status_code, request_id) + super + end + + def seam_api_error_response?(env) + return false unless env.response_headers + + content_type = env.response_headers["Content-Type"] + return false unless content_type&.start_with?("application/json") + + begin + body = JSON.parse(env.body) + return false unless body.is_a?(Hash) && body["error"].is_a?(Hash) + + error = body["error"] + error["type"].is_a?(String) && error["message"].is_a?(String) + rescue JSON::ParserError + false + end end end From fdde700beedbf7204568fafe53cf4452e00e4b10 Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <84702959+andrii-balitskyi@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:51:01 +0100 Subject: [PATCH 12/15] Update lib/seam/helpers/action_attempt.rb Co-authored-by: Evan Sosenko --- lib/seam/helpers/action_attempt.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/seam/helpers/action_attempt.rb b/lib/seam/helpers/action_attempt.rb index ece3de1..a13b7c7 100644 --- a/lib/seam/helpers/action_attempt.rb +++ b/lib/seam/helpers/action_attempt.rb @@ -35,7 +35,7 @@ def self.wait_until_finished(action_attempt, client, timeout: nil, polling_inter end def self.update_action_attempt(action_attempt, client) - response = client.post("/action_attempts/get", {action_attempt_id: action_attempt.action_attempt_id}) + response = client.get("/action_attempts/get", {action_attempt_id: action_attempt.action_attempt_id}) action_attempt.update_from_response(response.body["action_attempt"]) action_attempt From 1578c5c9a4ad9d5f4c6dcf047d00c6460d1f86ff Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Tue, 5 Nov 2024 19:05:11 +0100 Subject: [PATCH 13/15] Fix action attempt polling test --- spec/resources/action_attempt_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/resources/action_attempt_spec.rb b/spec/resources/action_attempt_spec.rb index 6a05d05..e30f7b9 100644 --- a/spec/resources/action_attempt_spec.rb +++ b/spec/resources/action_attempt_spec.rb @@ -38,10 +38,10 @@ describe ".wait_until_finished" do before do stub_seam_request( - :post, + :get, "/action_attempts/get", {action_attempt: action_attempt_hash} - ).with { |req| req.body == {"action_attempt_id" => action_attempt_id}.to_json } + ).with(query: {action_attempt_id: action_attempt_id}) .times(2) .then .to_return( @@ -67,10 +67,10 @@ let(:updated_action_attempt_hash) { action_attempt_hash.merge(status: "finished") } before do stub_seam_request( - :post, + :get, "/action_attempts/get", {action_attempt: updated_action_attempt_hash} - ).with { |req| req.body == {action_attempt_id: action_attempt_id}.to_json } + ).with(query: {action_attempt_id: action_attempt_id}) end it "updates the ActionAttempt" do From 5f777cd429877ca9558d97ed72ce00871f4402ca Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Tue, 5 Nov 2024 19:12:43 +0100 Subject: [PATCH 14/15] Test that faraday errors are thrown if error response is not seam api error --- spec/seam_client/request_spec.rb | 51 ++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/spec/seam_client/request_spec.rb b/spec/seam_client/request_spec.rb index 9fa48fb..397cdf8 100644 --- a/spec/seam_client/request_spec.rb +++ b/spec/seam_client/request_spec.rb @@ -48,32 +48,45 @@ end end - context "when other API error occurs" do - let(:error_message) { "An unknown error occurred" } + context "when non-Seam API error occurs" do let(:error_status) { 500 } - let(:error_response) do - { - error: { - type: "api_error", - message: error_message, - data: nil - } - }.to_json + let(:error_response) { "Internal Server Error" } + + before do + stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/list") + .to_return(status: error_status, body: error_response, headers: {"Content-Type" => "text/plain"}) + end + + it "raises Faraday error" do + expect { seam.devices.list }.to raise_error(Faraday::ServerError) end + end + + context "when malformed JSON response" do + let(:error_status) { 500 } + let(:error_response) { "{invalid json" } before do stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/list") - .to_return(status: error_status, body: error_response, headers: {"Content-Type" => "application/json", - "seam-request-id" => request_id}) + .to_return(status: error_status, body: error_response, headers: {"Content-Type" => "application/json"}) end - it "raises ApiError with the correct details" do - expect { seam.devices.list }.to raise_error(Seam::Http::ApiError) do |error| - expect(error.message).to eq(error_message) - expect(error.status_code).to eq(error_status) - expect(error.request_id).to eq(request_id) - expect(error.data).to be_nil - end + it "raises Faraday error" do + expect { seam.devices.list }.to raise_error(Faraday::ServerError) + end + end + + context "when JSON response without error object" do + let(:error_status) { 500 } + let(:error_response) { '{"message": "Some error"}' } + + before do + stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/list") + .to_return(status: error_status, body: error_response, headers: {"Content-Type" => "application/json"}) + end + + it "raises Faraday error" do + expect { seam.devices.list }.to raise_error(Faraday::ServerError) end end end From bbcf4e5062f4317f3c688c05ab93e837cd6d6c3d Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Wed, 6 Nov 2024 15:13:19 +0100 Subject: [PATCH 15/15] Remove retry logic from ResponseMiddleware, use retry middleware last on the faraday connection builder --- lib/seam/request.rb | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/seam/request.rb b/lib/seam/request.rb index 8223438..1a712af 100644 --- a/lib/seam/request.rb +++ b/lib/seam/request.rb @@ -23,9 +23,9 @@ def self.create_faraday_client(endpoint, auth_headers, faraday_options = {}, far Faraday.new(options) do |builder| builder.request :json - builder.request :retry, faraday_retry_options builder.response :json - builder.use ResponseMiddleware, retry_options: faraday_retry_options + builder.use ResponseMiddleware + builder.request :retry, faraday_retry_options end end @@ -40,21 +40,10 @@ def self.default_headers end class ResponseMiddleware < Faraday::Response::RaiseError - def initialize(app, options = {}) - super(app) - @retry_options = options[:retry_options] - end - def on_complete(env) return if env.success? status_code = env.status - - retry_statuses = @retry_options.fetch(:retry_statuses, []) - if retry_statuses.include?(status_code) - raise Faraday::RetriableResponse.new(nil, env.response) - end - request_id = env.response_headers["seam-request-id"] raise Http::UnauthorizedError.new(request_id) if status_code == 401