Skip to content

Commit

Permalink
Merge pull request #91 from balexand/headers
Browse files Browse the repository at this point in the history
Add `headers` to `Shopify.Response` struct
  • Loading branch information
nsweeting authored Dec 9, 2019
2 parents 00cdddb + 6fb0924 commit 3bdddfd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/shopify/adapters/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ defmodule Shopify.Adapters.HTTP do
|> handle_response(request.resource)
end

def handle_response({:ok, %HTTPoison.Response{status_code: code, body: body}}, resource) do
Shopify.Response.new(code, body, resource)
def handle_response({:ok, %HTTPoison.Response{} = resp}, resource) do
Shopify.Response.new(%{body: resp.body, code: resp.status_code, headers: resp.headers}, resource)
end

def handle_response({:error, %HTTPoison.Error{reason: reason}}, _resource) do
Expand Down
8 changes: 4 additions & 4 deletions lib/shopify/adapters/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ defmodule Shopify.Adapters.Mock do
auth = request |> authorize

case auth do
{:passed, _} -> Response.new(200, nil, request.resource)
{:passed, _} -> Response.new(%{code: 200, body: nil, headers: []}, request.resource)
{:failed, _} -> respond(auth)
end
end

def respond({:failed, request}) do
Response.new(401, nil, request.resource)
Response.new(%{code: 401, body: nil, headers: []}, request.resource)
end

def respond({:passed, request}) do
Expand All @@ -55,11 +55,11 @@ defmodule Shopify.Adapters.Mock do
end

def respond({:ok, body}, request) do
Response.new(200, body, request.resource)
Response.new(%{code: 200, body: body, headers: []}, request.resource)
end

def respond({:error, _}, request) do
Response.new(404, nil, request.resource)
Response.new(%{code: 404, body: nil, headers: []}, request.resource)
end

def load_resource(%Request{path: path, body: nil}) do
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify/resources/inventory_level.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ defmodule Shopify.InventoryLevel do
@doc false
def all_url, do: @plural <> ".json"

defp unprocessable_entity(msg), do: Shopify.Response.new(422, msg, empty_resource())
defp unprocessable_entity(msg), do: Shopify.Response.new(%{body: msg, code: 422, headers: []}, empty_resource())
end
11 changes: 6 additions & 5 deletions lib/shopify/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ defmodule Shopify.Response do

defstruct [
:code,
:data
:data,
:headers
]

def new(code, body, resource) when code < 300 do
{:ok, %Response{code: code, data: resource |> parse_json(body)}}
def new(%{body: body, code: code, headers: headers}, resource) when code < 300 do
{:ok, %Response{code: code, data: resource |> parse_json(body), headers: headers}}
end

def new(code, body, error) do
{:error, %Response{code: code, data: error |> parse_json(body)}}
def new(%{body: body, code: code, headers: headers}, error) do
{:error, %Response{code: code, data: error |> parse_json(body), headers: headers}}
end

defp parse_json(_res, body) when is_nil(body) or body == "" do
Expand Down
16 changes: 15 additions & 1 deletion test/adapters/http_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
defmodule Shopify.Adapters.HTTPTest do
use ExUnit.Case, async: true

alias Shopify.{Adapters.HTTP, Session, Request, Error, Product}
alias Shopify.{Adapters.HTTP, Session, Request, Response, Error, Product}

defmodule RequestOption do
def get(%Request{opts: opts}) do
opts
end
end

test "it returns correct Response struct" do
result = {:ok, %HTTPoison.Response{
body: "{\"product\":{\"id\":123}}",
headers: [{"X-Shopify-Shop-Api-Call-Limit", "1/80"}],
status_code: 200
}}

assert {:ok, %Response{} = response} = HTTP.handle_response(result, %{"product" => %Product{}})

assert response.code == 200
assert response.data == %Product{id: 123}
assert response.headers == [{"X-Shopify-Shop-Api-Call-Limit", "1/80"}]
end

test "it handles httpoison errors" do
result = {:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}

Expand Down

0 comments on commit 3bdddfd

Please sign in to comment.