From 7b7ea8032a397a8af5902031fd383ac2325dffda Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 16:10:06 -0400 Subject: [PATCH 01/15] enable catalog test --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index 3693ac3f..ab8b3e6d 100644 --- a/Rakefile +++ b/Rakefile @@ -11,6 +11,7 @@ Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" t.test_files = FileList[ + 'test/square/integration/catalog/test_client.rb', 'test/square_legacy/api/test_*.rb' ] t.warning = false From ef613bab500db8984fd0859038a432125385fe0c Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 18:24:40 -0400 Subject: [PATCH 02/15] modify test setup --- .../square/integration/catalog/test_client.rb | 116 ++++++++---------- test/square/test_helper.rb | 3 +- 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/test/square/integration/catalog/test_client.rb b/test/square/integration/catalog/test_client.rb index 61a7c01c..a7c095c1 100644 --- a/test/square/integration/catalog/test_client.rb +++ b/test/square/integration/catalog/test_client.rb @@ -5,72 +5,62 @@ describe Square::Catalog::Client do describe "#batch_upsert" do it "creates multiple catalog objects" do - skip "Skipping for now." - response = client.catalog.batch_upsert( - request: { - idempotency_key: SecureRandom.uuid, - batches: [ - { - objects: [ - { - type: "ITEM", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_data: { - name: "Coffee", - description: "Strong coffee", - abbreviation: "C", - variations: [ - { - type: "ITEM_VARIATION", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_variation_data: { - name: "Kona Coffee", - track_inventory: false, - pricing_type: "FIXED_PRICING", - price_money: { - amount: 1000, - currency: "USD" - } - } - } - ] - } - }, - { - type: "ITEM", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_data: { - name: "Tea", - description: "Strong tea", - abbreviation: "T", - variations: [ - { - type: "ITEM_VARIATION", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_variation_data: { - name: "Gunpowder Green", - track_inventory: false, - pricing_type: "FIXED_PRICING", - price_money: { - amount: 2000, - currency: "USD" - } - } - } - ] - } - } - ] - } - ] - } + # The new client should work with SDK types, not plain hashes + # Let's try creating the request object properly + request = Square::Catalog::Types::BatchUpsertCatalogObjectsRequest.new( + idempotency_key: SecureRandom.uuid, + batches: [ + Square::Types::CatalogObjectBatch.new( + objects: [ + Square::Types::CatalogObjectItem.new( + item_data: Square::Types::CatalogItem.new( + name: "Coffee", + description: "Strong coffee", + abbreviation: "C", + variations: [ + Square::Types::CatalogObjectItemVariation.new( + item_variation_data: Square::Types::CatalogItemVariation.new( + name: "Kona Coffee", + track_inventory: false, + pricing_type: Square::Types::CatalogPricingType::FIXED_PRICING, + price_money: Square::Types::Money.new( + amount: 1000, + currency: Square::Types::Country::US + ) + ) + ) + ] + ) + ), + Square::Types::CatalogObjectItem.new( + item_data: Square::Types::CatalogItem.new( + name: "Tea", + description: "Strong tea", + abbreviation: "T", + variations: [ + Square::Types::CatalogObjectItemVariation.new( + item_variation_data: Square::Types::CatalogItemVariation.new( + name: "Gunpowder Green", + track_inventory: false, + pricing_type: Square::Types::CatalogPricingType::FIXED_PRICING, + price_money: Square::Types::Money.new( + amount: 2000, + currency: Square::Types::Country::US + ) + ) + ) + ] + ) + ) + ] + ) + ] ) + # Try calling with the request object + response = client.catalog.batch_upsert(request: request) + refute_nil response end end diff --git a/test/square/test_helper.rb b/test/square/test_helper.rb index 958ef9f0..37e8131f 100644 --- a/test/square/test_helper.rb +++ b/test/square/test_helper.rb @@ -8,7 +8,8 @@ def test_token def client @client ||= Square::Client.new( - token: test_token + token: test_token, + base_url: Square::Environment::SANDBOX ) end From 29d2e066792aeef9e4cb85120075f221c2d7a26a Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 19:31:04 -0400 Subject: [PATCH 03/15] messy status --- Rakefile | 4 +- lib/square/catalog/client.rb | 15 ++- lib/square/client.rb | 2 +- lib/square/internal/http/raw_client.rb | 17 ++- lib/square/inventory/client.rb | 11 +- .../square/integration/catalog/test_client.rb | 115 ++++++++++-------- .../inventory/test_inventory_client.rb | 19 +++ 7 files changed, 119 insertions(+), 64 deletions(-) create mode 100644 test/square/integration/inventory/test_inventory_client.rb diff --git a/Rakefile b/Rakefile index ab8b3e6d..be691dd2 100644 --- a/Rakefile +++ b/Rakefile @@ -11,8 +11,8 @@ Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" t.test_files = FileList[ - 'test/square/integration/catalog/test_client.rb', - 'test/square_legacy/api/test_*.rb' + 'test/square/integration/inventory/test_inventory_client.rb' + # 'test/square_legacy/api/test_*.rb' ] t.warning = false end diff --git a/lib/square/catalog/client.rb b/lib/square/catalog/client.rb index a44310f8..2359f74a 100644 --- a/lib/square/catalog/client.rb +++ b/lib/square/catalog/client.rb @@ -69,8 +69,19 @@ def batch_get(request_options: {}, **params) # # @return [Square::Types::BatchUpsertCatalogObjectsResponse] def batch_upsert(request_options: {}, **params) - _request = params - _response = @client.send(_request) + _body_content = Types::BatchUpsertCatalogObjectsRequest.new(params[:request]) + _body = _body_content.to_h + _request = Internal::Multipart::Request.new( + base_url: Square::Environment::SANDBOX, + path: "/v2/catalog/batch-upsert", + method: "POST", + body: _body, + request_options: request_options + ) + puts "@client: #{@client}\n" + puts "@client.inspect: #{@client.inspect}\n" + puts "@client.methods: #{@client.methods}\n" + _response = @client.send_request(_request) if _response.code >= "200" && _response.code < "300" return Square::Types::BatchUpsertCatalogObjectsResponse.load(_response.body) else diff --git a/lib/square/client.rb b/lib/square/client.rb index 92775395..183f5d06 100644 --- a/lib/square/client.rb +++ b/lib/square/client.rb @@ -43,7 +43,7 @@ def cards end # @return [Square::Catalog::Client] def catalog - @catalog ||= Square::Catalog::Client.new(client: @raw_client) + @catalog ||= Square::Catalog::Client.new(@raw_client) end # @return [Square::Customers::Client] def customers diff --git a/lib/square/internal/http/raw_client.rb b/lib/square/internal/http/raw_client.rb index ed7a9423..a9876ebf 100644 --- a/lib/square/internal/http/raw_client.rb +++ b/lib/square/internal/http/raw_client.rb @@ -22,8 +22,12 @@ def initialize(base_url:, max_retries: 2, timeout: 60.0, headers: {}) # @param request [Square::Internal::Http::BaseRequest] The HTTP request. # @return [HTTP::Response] The HTTP response. - def send(request) + def send_request(request) url = build_url(request) + puts "[DEBUG] RawClient#send - URL: #{url}" + puts "[DEBUG] RawClient#send - Method: #{request.method}" + puts "[DEBUG] RawClient#send - Headers: #{request.encode_headers}" + puts "[DEBUG] RawClient#send - Body: #{request.encode_body}" http_request = build_http_request( url:, @@ -31,14 +35,23 @@ def send(request) headers: request.encode_headers, body: request.encode_body ) + puts "[DEBUG] RawClient#send - Built HTTP request: #{http_request.class}" conn = connect(url) + puts "[DEBUG] RawClient#send - Connected to: #{url}" conn.open_timeout = @timeout conn.read_timeout = @timeout conn.write_timeout = @timeout conn.continue_timeout = @timeout + puts "[DEBUG] RawClient#send - Timeouts set: #{@timeout}s" - conn.request(http_request) + puts "[DEBUG] RawClient#send - Sending request..." + response = conn.request(http_request) + puts "[DEBUG] RawClient#send - Response received: #{response.class}" + puts "[DEBUG] RawClient#send - Response status: #{response.code}" + puts "[DEBUG] RawClient#send - Response headers: #{response.to_hash}" + + response # begin # conn.request(http_request) # rescue StandardError => e diff --git a/lib/square/inventory/client.rb b/lib/square/inventory/client.rb index 13c95c97..c3950b03 100644 --- a/lib/square/inventory/client.rb +++ b/lib/square/inventory/client.rb @@ -123,12 +123,15 @@ def batch_create_changes(request_options: {}, **params) # # @return [Square::Types::BatchGetInventoryChangesResponse] def batch_get_changes(request_options: {}, **params) - _request = Square::Internal::Http::JSONRequest.new( - method: POST, + _body = Square::Types::BatchRetrieveInventoryChangesRequest.new(params[:request]).to_h + _request = Square::Internal::Multipart::Request.new( + base_url: Square::Environment::SANDBOX, + method: "POST", path: "v2/inventory/changes/batch-retrieve", - body: Square::Types::BatchRetrieveInventoryChangesRequest.new(params[:request]).to_h, + body: _body, + request_options: request_options ) - _response = @client.send(_request) + _response = @client[:client].send(_request) if _response.code >= "200" && _response.code < "300" return Square::Types::BatchGetInventoryChangesResponse.load(_response.body) else diff --git a/test/square/integration/catalog/test_client.rb b/test/square/integration/catalog/test_client.rb index a7c095c1..1d4d1c56 100644 --- a/test/square/integration/catalog/test_client.rb +++ b/test/square/integration/catalog/test_client.rb @@ -6,61 +6,70 @@ describe "#batch_upsert" do it "creates multiple catalog objects" do - # The new client should work with SDK types, not plain hashes - # Let's try creating the request object properly - request = Square::Catalog::Types::BatchUpsertCatalogObjectsRequest.new( - idempotency_key: SecureRandom.uuid, - batches: [ - Square::Types::CatalogObjectBatch.new( - objects: [ - Square::Types::CatalogObjectItem.new( - item_data: Square::Types::CatalogItem.new( - name: "Coffee", - description: "Strong coffee", - abbreviation: "C", - variations: [ - Square::Types::CatalogObjectItemVariation.new( - item_variation_data: Square::Types::CatalogItemVariation.new( - name: "Kona Coffee", - track_inventory: false, - pricing_type: Square::Types::CatalogPricingType::FIXED_PRICING, - price_money: Square::Types::Money.new( - amount: 1000, - currency: Square::Types::Country::US - ) - ) - ) - ] - ) - ), - Square::Types::CatalogObjectItem.new( - item_data: Square::Types::CatalogItem.new( - name: "Tea", - description: "Strong tea", - abbreviation: "T", - variations: [ - Square::Types::CatalogObjectItemVariation.new( - item_variation_data: Square::Types::CatalogItemVariation.new( - name: "Gunpowder Green", - track_inventory: false, - pricing_type: Square::Types::CatalogPricingType::FIXED_PRICING, - price_money: Square::Types::Money.new( - amount: 2000, - currency: Square::Types::Country::US - ) - ) - ) - ] - ) - ) - ] - ) - ] + response = client.catalog.batch_upsert( + request: { + idempotency_key: SecureRandom.uuid, + batches: [ + { + objects: [ + { + type: "ITEM", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_data: { + name: "Coffee", + description: "Strong coffee", + abbreviation: "C", + variations: [ + { + type: "ITEM_VARIATION", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_variation_data: { + name: "Kona Coffee", + track_inventory: false, + pricing_type: "FIXED_PRICING", + price_money: { + amount: 1000, + currency: "USD" + } + } + } + ] + } + }, + { + type: "ITEM", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_data: { + name: "Tea", + description: "Strong tea", + abbreviation: "T", + variations: [ + { + type: "ITEM_VARIATION", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_variation_data: { + name: "Gunpowder Green", + track_inventory: false, + pricing_type: "FIXED_PRICING", + price_money: { + amount: 2000, + currency: "USD" + } + } + } + ] + } + } + ] + } + ] + } ) - # Try calling with the request object - response = client.catalog.batch_upsert(request: request) - refute_nil response end end diff --git a/test/square/integration/inventory/test_inventory_client.rb b/test/square/integration/inventory/test_inventory_client.rb new file mode 100644 index 00000000..1e64c2e7 --- /dev/null +++ b/test/square/integration/inventory/test_inventory_client.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require_relative "../../test_helper" + +describe Square::Inventory::Client do + describe "#batch_get_changes" do + it "gets inventory changes" do + + response = client.inventory.batch_get_changes( + request: { + catalog_object_ids: ["W62UWFY35CWMYGVWK6TWJDNI"], + location_ids: ["C6W5YS5QM06F5"] + } + ) + + refute_nil response + end + end +end From ad73e6d242defeb2a127611d450e81a9c8d52f0f Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 22:00:45 -0400 Subject: [PATCH 04/15] current state --- Rakefile | 2 +- lib/square/catalog/client.rb | 15 +++++++-------- lib/square/client.rb | 2 +- lib/square/internal/http/raw_client.rb | 17 ++--------------- lib/square/internal/json/request.rb | 3 ++- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/Rakefile b/Rakefile index be691dd2..702092a7 100644 --- a/Rakefile +++ b/Rakefile @@ -11,7 +11,7 @@ Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" t.test_files = FileList[ - 'test/square/integration/inventory/test_inventory_client.rb' + 'test/square/integration/catalog/test_client.rb' # 'test/square_legacy/api/test_*.rb' ] t.warning = false diff --git a/lib/square/catalog/client.rb b/lib/square/catalog/client.rb index 2359f74a..c8b8a697 100644 --- a/lib/square/catalog/client.rb +++ b/lib/square/catalog/client.rb @@ -5,7 +5,7 @@ class Client # @option client [Square::Internal::Http::RawClient] # # @return [Square::Catalog::Client] - def initialize(client) + def initialize(client:) @client = client end @@ -69,19 +69,18 @@ def batch_get(request_options: {}, **params) # # @return [Square::Types::BatchUpsertCatalogObjectsResponse] def batch_upsert(request_options: {}, **params) - _body_content = Types::BatchUpsertCatalogObjectsRequest.new(params[:request]) - _body = _body_content.to_h - _request = Internal::Multipart::Request.new( + _body = Types::BatchUpsertCatalogObjectsRequest.new(params[:request]) + puts "batch_upsert._body.class: #{_body.class}" + _request = Internal::JSON::Request.new( base_url: Square::Environment::SANDBOX, path: "/v2/catalog/batch-upsert", method: "POST", body: _body, request_options: request_options ) - puts "@client: #{@client}\n" - puts "@client.inspect: #{@client.inspect}\n" - puts "@client.methods: #{@client.methods}\n" - _response = @client.send_request(_request) + puts "batch_upsert._request.class: #{_request.class}" + _response = @client.send(_request) + puts "batch_upsert._response.class: #{_response.class}" if _response.code >= "200" && _response.code < "300" return Square::Types::BatchUpsertCatalogObjectsResponse.load(_response.body) else diff --git a/lib/square/client.rb b/lib/square/client.rb index 183f5d06..92775395 100644 --- a/lib/square/client.rb +++ b/lib/square/client.rb @@ -43,7 +43,7 @@ def cards end # @return [Square::Catalog::Client] def catalog - @catalog ||= Square::Catalog::Client.new(@raw_client) + @catalog ||= Square::Catalog::Client.new(client: @raw_client) end # @return [Square::Customers::Client] def customers diff --git a/lib/square/internal/http/raw_client.rb b/lib/square/internal/http/raw_client.rb index a9876ebf..ed7a9423 100644 --- a/lib/square/internal/http/raw_client.rb +++ b/lib/square/internal/http/raw_client.rb @@ -22,12 +22,8 @@ def initialize(base_url:, max_retries: 2, timeout: 60.0, headers: {}) # @param request [Square::Internal::Http::BaseRequest] The HTTP request. # @return [HTTP::Response] The HTTP response. - def send_request(request) + def send(request) url = build_url(request) - puts "[DEBUG] RawClient#send - URL: #{url}" - puts "[DEBUG] RawClient#send - Method: #{request.method}" - puts "[DEBUG] RawClient#send - Headers: #{request.encode_headers}" - puts "[DEBUG] RawClient#send - Body: #{request.encode_body}" http_request = build_http_request( url:, @@ -35,23 +31,14 @@ def send_request(request) headers: request.encode_headers, body: request.encode_body ) - puts "[DEBUG] RawClient#send - Built HTTP request: #{http_request.class}" conn = connect(url) - puts "[DEBUG] RawClient#send - Connected to: #{url}" conn.open_timeout = @timeout conn.read_timeout = @timeout conn.write_timeout = @timeout conn.continue_timeout = @timeout - puts "[DEBUG] RawClient#send - Timeouts set: #{@timeout}s" - puts "[DEBUG] RawClient#send - Sending request..." - response = conn.request(http_request) - puts "[DEBUG] RawClient#send - Response received: #{response.class}" - puts "[DEBUG] RawClient#send - Response status: #{response.code}" - puts "[DEBUG] RawClient#send - Response headers: #{response.to_hash}" - - response + conn.request(http_request) # begin # conn.request(http_request) # rescue StandardError => e diff --git a/lib/square/internal/json/request.rb b/lib/square/internal/json/request.rb index 86d7fc08..5d2fa370 100644 --- a/lib/square/internal/json/request.rb +++ b/lib/square/internal/json/request.rb @@ -2,7 +2,7 @@ module Square module Internal - module Multipart + module JSON # @api private class Request < Square::Internal::Http::BaseRequest attr_reader :body @@ -17,6 +17,7 @@ class Request < Square::Internal::Http::BaseRequest def initialize(base_url:, path:, method:, headers: {}, query: {}, body: nil, request_options: {}) super(base_url:, path:, method:, headers:, query:, request_options:) + puts "json.request: body.class: #{body.class}" @body = body end From 4b01ed3a61135a1b81daee058ce8d8237277b243 Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 22:03:01 -0400 Subject: [PATCH 05/15] fix typo in multipart request --- lib/square/internal/multipart/multipart_request.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/square/internal/multipart/multipart_request.rb b/lib/square/internal/multipart/multipart_request.rb index e6fd3361..1833d282 100644 --- a/lib/square/internal/multipart/multipart_request.rb +++ b/lib/square/internal/multipart/multipart_request.rb @@ -4,7 +4,7 @@ module Square module Internal module Multipart # @api private - class Request Square::Internal::Http::BaseRequest + class Request < Square::Internal::Http::BaseRequest attr_reader :body # @param base_url [String] The base URL for the request From 81e5f463282f535f0fbe3c1b0c8b2feff5435e55 Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 22:49:30 -0400 Subject: [PATCH 06/15] simplify state --- lib/square/catalog/client.rb | 4 +++- lib/square/internal/http/raw_client.rb | 6 ++++++ lib/square/internal/json/request.rb | 11 +++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/square/catalog/client.rb b/lib/square/catalog/client.rb index c8b8a697..77151419 100644 --- a/lib/square/catalog/client.rb +++ b/lib/square/catalog/client.rb @@ -71,11 +71,13 @@ def batch_get(request_options: {}, **params) def batch_upsert(request_options: {}, **params) _body = Types::BatchUpsertCatalogObjectsRequest.new(params[:request]) puts "batch_upsert._body.class: #{_body.class}" + _body_hash = _body.to_h + puts "batch_upsert._body_hash.class: #{_body_hash.class}" _request = Internal::JSON::Request.new( base_url: Square::Environment::SANDBOX, path: "/v2/catalog/batch-upsert", method: "POST", - body: _body, + body: _body_hash, request_options: request_options ) puts "batch_upsert._request.class: #{_request.class}" diff --git a/lib/square/internal/http/raw_client.rb b/lib/square/internal/http/raw_client.rb index ed7a9423..701e8c82 100644 --- a/lib/square/internal/http/raw_client.rb +++ b/lib/square/internal/http/raw_client.rb @@ -32,6 +32,9 @@ def send(request) body: request.encode_body ) + puts "raw_client.send: http_request.class: #{http_request.class}" + puts "raw_client.send: http_request.inspect: #{http_request.inspect}" + conn = connect(url) conn.open_timeout = @timeout conn.read_timeout = @timeout @@ -61,6 +64,9 @@ def build_url(request) # @param body [String, nil] The body for the request. # @return [HTTP::Request] The HTTP request. def build_http_request(url:, method:, headers: {}, body: nil) + puts "raw_client.build_http_request: body.class: #{body.class}" + puts "raw_client.build_http_request: body: #{body}" + puts "----------------------------------------------------" request = Net::HTTPGenericRequest.new( method, !body.nil?, diff --git a/lib/square/internal/json/request.rb b/lib/square/internal/json/request.rb index 5d2fa370..ce1d51cf 100644 --- a/lib/square/internal/json/request.rb +++ b/lib/square/internal/json/request.rb @@ -17,7 +17,6 @@ class Request < Square::Internal::Http::BaseRequest def initialize(base_url:, path:, method:, headers: {}, query: {}, body: nil, request_options: {}) super(base_url:, path:, method:, headers:, query:, request_options:) - puts "json.request: body.class: #{body.class}" @body = body end @@ -31,7 +30,15 @@ def encode_headers # @return [String, nil] The encoded HTTP request body. def encode_body - @body.nil? ? nil : ::JSON.generate(@body) + if @body.nil? + nil + else + puts "json.request.encode_body: @body.to_h.class: #{@body.to_h.class}" + puts "json.request.encode_body: @body.to_h: #{@body.to_h}" + puts "----------------------------------------------------" + ::JSON.generate(@body.to_h) + end + # @body.nil? ? nil : ::JSON.generate(@body) end end end From 23467461101345317b19985b9f308262d777ac71 Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 23:01:49 -0400 Subject: [PATCH 07/15] progress, some nested values converting --- lib/square/internal/types/model.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/square/internal/types/model.rb b/lib/square/internal/types/model.rb index 3707af1a..3b472a77 100644 --- a/lib/square/internal/types/model.rb +++ b/lib/square/internal/types/model.rb @@ -170,6 +170,26 @@ def to_h next if value.nil? && field.optional && !field.nullable + # # Recursively convert nested objects to hashes if they have a to_h method + puts "model.to_h: value.class: #{value.class}" + puts "model.to_h: value: #{value}" + puts "----------------------------------------------------" + if value.is_a?(::Array) + puts "model.to_h: value is an array" + value = value.map { |item| item.respond_to?(:to_h) ? item.to_h : item } + puts "model.to_h: value after mapping from array" + puts "model.to_h: value.class: #{value.class}" + puts "model.to_h: value: #{value}" + puts "----------------------------------------------------" + elsif value.respond_to?(:to_h) + puts "model.to_h:converting value to hash" + value = value.to_h + puts "model.to_h: value after to_h" + puts "model.to_h: value.class: #{value.class}" + puts "model.to_h: value: #{value}" + puts "----------------------------------------------------" + end + acc[field.api_name] = value end end From d1200c935c444dbcb3d7e5309434e64e07420e14 Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 23:12:52 -0400 Subject: [PATCH 08/15] progress, some nested values converting --- lib/square/internal/types/union.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/square/internal/types/union.rb b/lib/square/internal/types/union.rb index 3fea19c7..b0940daa 100644 --- a/lib/square/internal/types/union.rb +++ b/lib/square/internal/types/union.rb @@ -60,6 +60,9 @@ def discriminant(key) def coerce(value, strict: strict?) type = resolve_member(value) + puts "union.coerce: type.class: #{type.class}" + puts "union.coerce: type: #{type}" + puts "----------------------------------------------------" unless type return value unless strict @@ -72,9 +75,19 @@ def coerce(value, strict: strict?) raise Errors::TypeError, "could not resolve to member of union #{self}" end + puts "union.coerce: value.class: #{value.class}" + puts "union.coerce: value: #{value}" + puts "----------------------------------------------------" value = value.except(@discriminant) if type <= Model && value.is_a?(::Hash) - - Utils.coerce(type, value, strict: strict) + puts "union.coerce: value.class after except: #{value.class}" + puts "union.coerce: value after except: #{value}" + puts "----------------------------------------------------" + + _res = Utils.coerce(type, value, strict: strict) + puts "union.coerce: _res.class: #{_res.class}" + puts "union.coerce: _res: #{_res}" + puts "----------------------------------------------------" + _res end end end From 8edc5eae9d7e6bd647795fc55621d7635d212623 Mon Sep 17 00:00:00 2001 From: jsklan Date: Mon, 18 Aug 2025 23:15:31 -0400 Subject: [PATCH 09/15] log in coerce --- lib/square/internal/types/union.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/square/internal/types/union.rb b/lib/square/internal/types/union.rb index b0940daa..0f3a79c7 100644 --- a/lib/square/internal/types/union.rb +++ b/lib/square/internal/types/union.rb @@ -75,14 +75,12 @@ def coerce(value, strict: strict?) raise Errors::TypeError, "could not resolve to member of union #{self}" end + puts "union.coerce: value before except" puts "union.coerce: value.class: #{value.class}" puts "union.coerce: value: #{value}" - puts "----------------------------------------------------" value = value.except(@discriminant) if type <= Model && value.is_a?(::Hash) puts "union.coerce: value.class after except: #{value.class}" puts "union.coerce: value after except: #{value}" - puts "----------------------------------------------------" - _res = Utils.coerce(type, value, strict: strict) puts "union.coerce: _res.class: #{_res.class}" puts "union.coerce: _res: #{_res}" From a411936fa0bafe102dd4e520e6c173d9fb6fb5fc Mon Sep 17 00:00:00 2001 From: jsklan Date: Tue, 19 Aug 2025 00:22:56 -0400 Subject: [PATCH 10/15] restore request --- lib/square/internal/json/request.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/square/internal/json/request.rb b/lib/square/internal/json/request.rb index ce1d51cf..0e82f037 100644 --- a/lib/square/internal/json/request.rb +++ b/lib/square/internal/json/request.rb @@ -30,15 +30,7 @@ def encode_headers # @return [String, nil] The encoded HTTP request body. def encode_body - if @body.nil? - nil - else - puts "json.request.encode_body: @body.to_h.class: #{@body.to_h.class}" - puts "json.request.encode_body: @body.to_h: #{@body.to_h}" - puts "----------------------------------------------------" - ::JSON.generate(@body.to_h) - end - # @body.nil? ? nil : ::JSON.generate(@body) + @body.nil? ? nil : ::JSON.generate(@body) end end end From cfcb1562c126c7fe95f7ed4051ad730eb65e7d59 Mon Sep 17 00:00:00 2001 From: jsklan Date: Tue, 19 Aug 2025 00:29:32 -0400 Subject: [PATCH 11/15] request succeeds --- lib/square/internal/types/model.rb | 13 ------------- lib/square/internal/types/union.rb | 19 +++++-------------- lib/square/types/catalog_object.rb | 2 +- 3 files changed, 6 insertions(+), 28 deletions(-) diff --git a/lib/square/internal/types/model.rb b/lib/square/internal/types/model.rb index 3b472a77..0b6fb44a 100644 --- a/lib/square/internal/types/model.rb +++ b/lib/square/internal/types/model.rb @@ -171,23 +171,10 @@ def to_h next if value.nil? && field.optional && !field.nullable # # Recursively convert nested objects to hashes if they have a to_h method - puts "model.to_h: value.class: #{value.class}" - puts "model.to_h: value: #{value}" - puts "----------------------------------------------------" if value.is_a?(::Array) - puts "model.to_h: value is an array" value = value.map { |item| item.respond_to?(:to_h) ? item.to_h : item } - puts "model.to_h: value after mapping from array" - puts "model.to_h: value.class: #{value.class}" - puts "model.to_h: value: #{value}" - puts "----------------------------------------------------" elsif value.respond_to?(:to_h) - puts "model.to_h:converting value to hash" value = value.to_h - puts "model.to_h: value after to_h" - puts "model.to_h: value.class: #{value.class}" - puts "model.to_h: value: #{value}" - puts "----------------------------------------------------" end acc[field.api_name] = value diff --git a/lib/square/internal/types/union.rb b/lib/square/internal/types/union.rb index 0f3a79c7..974a6e69 100644 --- a/lib/square/internal/types/union.rb +++ b/lib/square/internal/types/union.rb @@ -60,9 +60,6 @@ def discriminant(key) def coerce(value, strict: strict?) type = resolve_member(value) - puts "union.coerce: type.class: #{type.class}" - puts "union.coerce: type: #{type}" - puts "----------------------------------------------------" unless type return value unless strict @@ -75,17 +72,11 @@ def coerce(value, strict: strict?) raise Errors::TypeError, "could not resolve to member of union #{self}" end - puts "union.coerce: value before except" - puts "union.coerce: value.class: #{value.class}" - puts "union.coerce: value: #{value}" - value = value.except(@discriminant) if type <= Model && value.is_a?(::Hash) - puts "union.coerce: value.class after except: #{value.class}" - puts "union.coerce: value after except: #{value}" - _res = Utils.coerce(type, value, strict: strict) - puts "union.coerce: _res.class: #{_res.class}" - puts "union.coerce: _res: #{_res}" - puts "----------------------------------------------------" - _res + # TODO: This check doesn't matter right now since undiscriminated unions are not supported + # In its current state, it breaks discriminated unions + # value = value.except(@discriminant) if type <= Model && value.is_a?(::Hash) + + Utils.coerce(type, value, strict: strict) end end end diff --git a/lib/square/types/catalog_object.rb b/lib/square/types/catalog_object.rb index b6ce57f5..80cab7e5 100644 --- a/lib/square/types/catalog_object.rb +++ b/lib/square/types/catalog_object.rb @@ -12,7 +12,7 @@ module Types # # For a more detailed discussion of the Catalog data model, please see the # [Design a Catalog](https://developer.squareup.com/docs/catalog-api/design-a-catalog) guide. - class CatalogObject + class CatalogObject < Internal::Types::Model extend Square::Internal::Types::Union discriminant :type From 56581ebf8c10ff6824583201d7c6e2a485c55d17 Mon Sep 17 00:00:00 2001 From: jsklan Date: Tue, 19 Aug 2025 00:30:52 -0400 Subject: [PATCH 12/15] remove logging from client and raw client --- lib/square/catalog/client.rb | 13 +++---------- lib/square/internal/http/raw_client.rb | 6 ------ 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/square/catalog/client.rb b/lib/square/catalog/client.rb index 77151419..fb07fb9e 100644 --- a/lib/square/catalog/client.rb +++ b/lib/square/catalog/client.rb @@ -69,20 +69,13 @@ def batch_get(request_options: {}, **params) # # @return [Square::Types::BatchUpsertCatalogObjectsResponse] def batch_upsert(request_options: {}, **params) - _body = Types::BatchUpsertCatalogObjectsRequest.new(params[:request]) - puts "batch_upsert._body.class: #{_body.class}" - _body_hash = _body.to_h - puts "batch_upsert._body_hash.class: #{_body_hash.class}" - _request = Internal::JSON::Request.new( + _response = @client.send(Internal::JSON::Request.new( base_url: Square::Environment::SANDBOX, path: "/v2/catalog/batch-upsert", method: "POST", - body: _body_hash, + body: Types::BatchUpsertCatalogObjectsRequest.new(params[:request]).to_h, request_options: request_options - ) - puts "batch_upsert._request.class: #{_request.class}" - _response = @client.send(_request) - puts "batch_upsert._response.class: #{_response.class}" + )) if _response.code >= "200" && _response.code < "300" return Square::Types::BatchUpsertCatalogObjectsResponse.load(_response.body) else diff --git a/lib/square/internal/http/raw_client.rb b/lib/square/internal/http/raw_client.rb index 701e8c82..ed7a9423 100644 --- a/lib/square/internal/http/raw_client.rb +++ b/lib/square/internal/http/raw_client.rb @@ -32,9 +32,6 @@ def send(request) body: request.encode_body ) - puts "raw_client.send: http_request.class: #{http_request.class}" - puts "raw_client.send: http_request.inspect: #{http_request.inspect}" - conn = connect(url) conn.open_timeout = @timeout conn.read_timeout = @timeout @@ -64,9 +61,6 @@ def build_url(request) # @param body [String, nil] The body for the request. # @return [HTTP::Request] The HTTP request. def build_http_request(url:, method:, headers: {}, body: nil) - puts "raw_client.build_http_request: body.class: #{body.class}" - puts "raw_client.build_http_request: body: #{body}" - puts "----------------------------------------------------" request = Net::HTTPGenericRequest.new( method, !body.nil?, From 053c1861fcd26a9386039b687e8274218e84a952 Mon Sep 17 00:00:00 2001 From: jsklan Date: Tue, 19 Aug 2025 00:39:02 -0400 Subject: [PATCH 13/15] test passes, response parsed --- lib/square/internal/types/model.rb | 2 +- test/square/integration/catalog/test_client.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/square/internal/types/model.rb b/lib/square/internal/types/model.rb index 0b6fb44a..efaab7b2 100644 --- a/lib/square/internal/types/model.rb +++ b/lib/square/internal/types/model.rb @@ -112,7 +112,7 @@ def add_extra_field_definition(name:, type:) end end - def coerce(value, strict: strict?) + def coerce(value, strict: (respond_to?(:strict?) ? strict? : false)) return value if value.is_a?(self) return value unless value.is_a?(::Hash) diff --git a/test/square/integration/catalog/test_client.rb b/test/square/integration/catalog/test_client.rb index 1d4d1c56..d535f294 100644 --- a/test/square/integration/catalog/test_client.rb +++ b/test/square/integration/catalog/test_client.rb @@ -71,6 +71,7 @@ ) refute_nil response + puts "response #{response.to_h}" end end end From c9af05ce50d8d4a3d4ee360697eb0f009ed6ce3a Mon Sep 17 00:00:00 2001 From: jsklan Date: Tue, 19 Aug 2025 11:00:13 -0400 Subject: [PATCH 14/15] update catalog client --- lib/square/inventory/client.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/square/inventory/client.rb b/lib/square/inventory/client.rb index a165f6d1..8203fe61 100644 --- a/lib/square/inventory/client.rb +++ b/lib/square/inventory/client.rb @@ -122,12 +122,11 @@ def batch_create_changes(request_options: {}, **params) # @return [Square::Types::BatchGetInventoryChangesResponse] def batch_get_changes(request_options: {}, **params) _request = Square::Internal::JSON::Request.new( - method: "POST", + method: POST, path: "v2/inventory/changes/batch-retrieve", body: Square::Types::BatchRetrieveInventoryChangesRequest.new(params[:request]).to_h, - request_options: request_options ) - _response = @client[:client].send(_request) + _response = @client.send(_request) if _response.code >= "200" && _response.code < "300" return Square::Types::BatchGetInventoryChangesResponse.load(_response.body) else From c85b299bd3fb3bbf9c2f25358488e4542286df36 Mon Sep 17 00:00:00 2001 From: jsklan Date: Tue, 19 Aug 2025 11:17:36 -0400 Subject: [PATCH 15/15] add verbose mode handling in integration test helper --- Rakefile | 4 +- .../square/integration/catalog/test_client.rb | 119 +++++++++--------- test/square/test_helper.rb | 15 +++ 3 files changed, 78 insertions(+), 60 deletions(-) diff --git a/Rakefile b/Rakefile index 0d13ea05..7f869fa4 100644 --- a/Rakefile +++ b/Rakefile @@ -11,8 +11,8 @@ Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" t.test_files = FileList[ - 'test/square/integration/catalog/test_client.rb' - # 'test/square_legacy/api/test_*.rb' + 'test/square/integration/catalog/test_client.rb', + 'test/square_legacy/api/test_*.rb' ] t.warning = false end diff --git a/test/square/integration/catalog/test_client.rb b/test/square/integration/catalog/test_client.rb index d535f294..7b14d644 100644 --- a/test/square/integration/catalog/test_client.rb +++ b/test/square/integration/catalog/test_client.rb @@ -6,72 +6,75 @@ describe "#batch_upsert" do it "creates multiple catalog objects" do - response = client.catalog.batch_upsert( - request: { - idempotency_key: SecureRandom.uuid, - batches: [ - { - objects: [ - { - type: "ITEM", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_data: { - name: "Coffee", - description: "Strong coffee", - abbreviation: "C", - variations: [ - { - type: "ITEM_VARIATION", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_variation_data: { - name: "Kona Coffee", - track_inventory: false, - pricing_type: "FIXED_PRICING", - price_money: { - amount: 1000, - currency: "USD" - } + _request = Square::Catalog::Types::BatchUpsertCatalogObjectsRequest.new( + idempotency_key: SecureRandom.uuid, + batches: [ + { + objects: [ + { + type: "ITEM", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_data: { + name: "Coffee", + description: "Strong coffee", + abbreviation: "C", + variations: [ + { + type: "ITEM_VARIATION", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_variation_data: { + name: "Kona Coffee", + track_inventory: false, + pricing_type: "FIXED_PRICING", + price_money: { + amount: 1000, + currency: "USD" } } - ] - } - }, - { - type: "ITEM", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_data: { - name: "Tea", - description: "Strong tea", - abbreviation: "T", - variations: [ - { - type: "ITEM_VARIATION", - id: "##{SecureRandom.uuid}", - present_at_all_locations: true, - item_variation_data: { - name: "Gunpowder Green", - track_inventory: false, - pricing_type: "FIXED_PRICING", - price_money: { - amount: 2000, - currency: "USD" - } + } + ] + } + }, + { + type: "ITEM", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_data: { + name: "Tea", + description: "Strong tea", + abbreviation: "T", + variations: [ + { + type: "ITEM_VARIATION", + id: "##{SecureRandom.uuid}", + present_at_all_locations: true, + item_variation_data: { + name: "Gunpowder Green", + track_inventory: false, + pricing_type: "FIXED_PRICING", + price_money: { + amount: 2000, + currency: "USD" } } - ] - } + } + ] } - ] - } - ] - } + } + ] + } + ] ) + puts "request #{_request.to_h}" if verbose? + + response = client.catalog.batch_upsert(request: _request.to_h) refute_nil response - puts "response #{response.to_h}" + + puts "response #{response.to_h}" if verbose? + end end end diff --git a/test/square/test_helper.rb b/test/square/test_helper.rb index 37e8131f..9cde6344 100644 --- a/test/square/test_helper.rb +++ b/test/square/test_helper.rb @@ -13,6 +13,21 @@ def client ) end +def verbose_mode? + @verbose_mode ||= ENV.fetch("VERBOSE", "false") == "true" +end + +def minitest_verbose? + return false unless defined?(Minitest) + + # Check TESTOPTS environment variable for --verbose flag + ENV['TESTOPTS']&.include?('--verbose') || ARGV.include?('--verbose') +end + +def verbose? + verbose_mode? || minitest_verbose? +end + require "minitest/autorun" require "minitest/rg"