From 97421aba7ec39fa38d196824ddabe41e13ee8b34 Mon Sep 17 00:00:00 2001 From: Elizabeth Kenyon Date: Wed, 29 Nov 2023 11:40:16 -0600 Subject: [PATCH] Move transformation to http client --- lib/shopify_api/clients/graphql/client.rb | 1 + lib/shopify_api/clients/http_client.rb | 9 +++++++-- lib/shopify_api/clients/http_response.rb | 9 ++------- test/clients/http_client_test.rb | 11 +++++++++++ test/clients/http_response_test.rb | 9 --------- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/shopify_api/clients/graphql/client.rb b/lib/shopify_api/clients/graphql/client.rb index 958cc2166..97439c2c2 100644 --- a/lib/shopify_api/clients/graphql/client.rb +++ b/lib/shopify_api/clients/graphql/client.rb @@ -42,6 +42,7 @@ def query(query:, variables: nil, headers: nil, tries: 1) body_type: "application/json", tries: tries, ), + graphql_response_object: Context.graphql_response_object || false, ) end end diff --git a/lib/shopify_api/clients/http_client.rb b/lib/shopify_api/clients/http_client.rb index 260ade137..8f32b2dd5 100644 --- a/lib/shopify_api/clients/http_client.rb +++ b/lib/shopify_api/clients/http_client.rb @@ -32,8 +32,8 @@ def initialize(base_path:, session: nil) end end - sig { params(request: HttpRequest).returns(HttpResponse) } - def request(request) + sig { params(request: HttpRequest, graphql_response_object: T::Boolean).returns(HttpResponse) } + def request(request, graphql_response_object: false) request.verify headers = @headers @@ -60,6 +60,11 @@ def request(request) body = res.body end + if graphql_response_object && body.is_a?(Hash) + json_body = body.to_json + body = JSON.parse(json_body, object_class: OpenStruct) + end + response = HttpResponse.new(code: res.code.to_i, headers: res.headers.to_h, body: body) if response.headers["x-shopify-api-deprecated-reason"] diff --git a/lib/shopify_api/clients/http_response.rb b/lib/shopify_api/clients/http_response.rb index 985e2fbb2..0196a6e3f 100644 --- a/lib/shopify_api/clients/http_response.rb +++ b/lib/shopify_api/clients/http_response.rb @@ -22,18 +22,13 @@ class HttpResponse params( code: Integer, headers: T::Hash[String, T::Array[String]], - body: T.any(T::Hash[String, T.untyped], String), + body: T.any(T::Hash[String, T.untyped], String, OpenStruct), ).void end def initialize(code:, headers:, body:) @code = code @headers = headers - @body = T.let(body, T.any(OpenStruct, T::Hash[String, T.untyped], String)) - - if Context.graphql_response_object && body.is_a?(Hash) - json_body = body.to_json -+ @body = JSON.parse(json_body, object_class: OpenStruct) - end + @body = body @prev_page_info = T.let(nil, T.nilable(String)) @next_page_info = T.let(nil, T.nilable(String)) diff --git a/test/clients/http_client_test.rb b/test/clients/http_client_test.rb index 2f065bb7a..540e65f56 100644 --- a/test/clients/http_client_test.rb +++ b/test/clients/http_client_test.rb @@ -270,6 +270,17 @@ def test_json_parser_error assert_equal(502, error.code) end + def test_graphql_response_object + stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}") + .with(body: @request.body.to_json, query: @request.query, headers: @expected_headers) + .to_return(body: { "key" => { "nested_key" => "nested_value" }}.to_json, headers: @response_headers) + + response = @client.request(@request, graphql_response_object: true) + assert_kind_of(OpenStruct, response.body) + assert_equal("nested_value", response.body.key.nested_key) + + end + private def simple_http_test(http_method) diff --git a/test/clients/http_response_test.rb b/test/clients/http_response_test.rb index 226c763d9..98eefff30 100644 --- a/test/clients/http_response_test.rb +++ b/test/clients/http_response_test.rb @@ -45,15 +45,6 @@ def test_next_and_prev assert_equal("page-info", response.prev_page_info) assert_equal("other-page-info", response.next_page_info) end - - def test_object_response - modify_context(graphql_response_object: true) - - # We need to verify that the response body is an OpenStruct object - response = ShopifyAPI::Clients::HttpResponse.new(code: 200, headers: {}, body: { "key" => { "nested_key" => "nested_value" }}) - assert_kind_of(OpenStruct, response.body) - assert_equal("nested_value", response.body.key.nested_key) - end end end end