-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce Response and ResponseHandler protocol
- Loading branch information
Showing
6 changed files
with
96 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
~r/__impl__.*does\ not\ exist\./ | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Stow.Response do | ||
@moduledoc """ | ||
Struct containing a standardised response from various data sources. | ||
""" | ||
|
||
defstruct [:body, :headers, :state, :status] | ||
|
||
@type t :: %__MODULE__{ | ||
body: nil | iodata(), | ||
headers: nil | [{binary(), binary()}], | ||
status: nil | pos_integer(), | ||
state: nil | :ok | {:error, term()} | ||
} | ||
|
||
def put_body(response, body) when is_binary(body) do | ||
%{response | body: body} | ||
end | ||
|
||
def put_body(response, body) when is_list(body) do | ||
%{response | body: if(IO.iodata_length(body) > 0, do: body, else: nil)} | ||
end | ||
|
||
def put_headers(response, [{k, v} | _] = headers) when is_binary(k) and is_binary(v) do | ||
%{response | headers: headers} | ||
end | ||
|
||
def put_headers(response, [{k, v} | _] = headers) | ||
when is_list(k) and is_list(v) and is_integer(hd(k)) and is_integer(hd(v)) do | ||
%{response | headers: headers |> Enum.map(fn {k, v} -> {to_string(k), to_string(v)} end)} | ||
end | ||
|
||
def put_status(response, status) when is_integer(status) do | ||
%{response | status: status} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defprotocol Stow.ResponseHandler do | ||
@moduledoc ~S""" | ||
The `Stow.Response` protocol is responsible for | ||
converting various data sources response to the `Stow.Response.t()` struct. | ||
The only function that must be implemented is | ||
`to_response/1` which does the conversion. | ||
""" | ||
|
||
@dialyzer {:nowarn_function, to_response: 1} | ||
|
||
@fallback_to_any true | ||
@spec to_response(term()) :: Stow.Response.t() | ||
def to_response(value) | ||
end | ||
|
||
defimpl Stow.ResponseHandler, for: Tuple do | ||
alias Stow.Response | ||
import Stow.Response | ||
|
||
# httpc response | ||
def to_response({:ok, {{[?H, ?T, ?T, ?P | _], status, _}, headers, body}}) do | ||
%Response{state: :ok} | ||
|> put_body(body |> IO.iodata_to_binary()) | ||
|> put_status(status) | ||
|> put_headers(headers) | ||
end | ||
|
||
def to_response({:error, term}), do: %Stow.Response{state: {:error, term}} | ||
end | ||
|
||
defimpl Stow.ResponseHandler, for: Any do | ||
def to_response(_value), do: %Stow.Response{} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters