Skip to content

Commit

Permalink
Move transformation to http client
Browse files Browse the repository at this point in the history
  • Loading branch information
lizkenyon committed Nov 30, 2023
1 parent fa1f205 commit c8ce8ba
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/shopify_api/clients/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def query(query:, variables: nil, headers: nil, tries: 1)
body_type: "application/json",
tries: tries,
),
response_as_struct: Context.response_as_struct || false,
)
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/shopify_api/clients/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, response_as_struct: T::Boolean).returns(HttpResponse) }
def request(request, response_as_struct: false)
request.verify

headers = @headers
Expand All @@ -60,6 +60,11 @@ def request(request)
body = res.body
end

if response_as_struct && 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"]
Expand Down
9 changes: 2 additions & 7 deletions lib/shopify_api/clients/http_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions lib/shopify_api/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Context
@active_session = T.let(Concurrent::ThreadLocalVar.new { nil }, T.nilable(Concurrent::ThreadLocalVar))
@user_agent_prefix = T.let(nil, T.nilable(String))
@old_api_secret_key = T.let(nil, T.nilable(String))
@graphql_response_object = T.let(false, T.nilable(T::Boolean))
@response_as_struct = T.let(false, T.nilable(T::Boolean))

@rest_resource_loader = T.let(nil, T.nilable(Zeitwerk::Loader))

Expand All @@ -44,7 +44,7 @@ class << self
user_agent_prefix: T.nilable(String),
old_api_secret_key: T.nilable(String),
api_host: T.nilable(String),
graphql_response_object: T.nilable(T::Boolean),
response_as_struct: T.nilable(T::Boolean),
).void
end
def setup(
Expand All @@ -62,7 +62,7 @@ def setup(
user_agent_prefix: nil,
old_api_secret_key: nil,
api_host: nil,
graphql_response_object: false
response_as_struct: false
)
unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
raise Errors::UnsupportedVersionError,
Expand All @@ -81,7 +81,7 @@ def setup(
@private_shop = private_shop
@user_agent_prefix = user_agent_prefix
@old_api_secret_key = old_api_secret_key
@graphql_response_object = graphql_response_object
@response_as_struct = response_as_struct
@log_level = if valid_log_level?(log_level)
log_level.to_sym
else
Expand Down Expand Up @@ -133,7 +133,7 @@ def load_rest_resources(api_version:)
attr_reader :log_level

sig { returns T.nilable(T::Boolean) }
attr_reader :graphql_response_object
attr_reader :response_as_struct

sig { returns(T::Boolean) }
def private?
Expand Down
10 changes: 10 additions & 0 deletions test/clients/http_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ def test_json_parser_error
assert_equal(502, error.code)
end

def test_response_as_struct
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, response_as_struct: true)
assert_kind_of(OpenStruct, response.body)
assert_equal("nested_value", response.body.key.nested_key)
end

private

def simple_http_test(http_method)
Expand Down
9 changes: 0 additions & 9 deletions test/clients/http_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def setup
private_shop: T.nilable(String),
user_agent_prefix: T.nilable(String),
old_api_secret_key: T.nilable(String),
graphql_response_object: T.nilable(T::Boolean),
response_as_struct: T.nilable(T::Boolean),
).void
end
def modify_context(
Expand All @@ -66,7 +66,7 @@ def modify_context(
private_shop: "do-not-set",
user_agent_prefix: nil,
old_api_secret_key: nil,
graphql_response_object: nil
response_as_struct: nil
)
ShopifyAPI::Context.setup(
api_key: api_key ? api_key : ShopifyAPI::Context.api_key,
Expand All @@ -81,7 +81,7 @@ def modify_context(
user_agent_prefix: user_agent_prefix ? user_agent_prefix : ShopifyAPI::Context.user_agent_prefix,
old_api_secret_key: old_api_secret_key ? old_api_secret_key : ShopifyAPI::Context.old_api_secret_key,
log_level: :off,
graphql_response_object: graphql_response_object || ShopifyAPI::Context.graphql_response_object,
response_as_struct: response_as_struct || ShopifyAPI::Context.response_as_struct,
)
end
end
Expand Down

0 comments on commit c8ce8ba

Please sign in to comment.