Skip to content

Commit

Permalink
Options to facilitate opts concerns of source, sink implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
boonious committed Mar 4, 2024
1 parent b58c284 commit baa2eb2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/stow/source/extras.ex → lib/stow/options.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Stow.Source.Extras do
defmodule Stow.Options do
@moduledoc false
alias Stow.Http.Headers

defstruct headers: %Headers{}
@type t :: %__MODULE__{headers: Headers.t()}
end
10 changes: 5 additions & 5 deletions lib/stow/plug/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Stow.Plug.Source do
import Plug.Conn, only: [halt: 1, resp: 3]
import Utils, only: [fetch_uri: 2, put_headers: 3, set_uri_params: 2, update_private: 3]

@plug_opts [:uri, :extras]
@plug_opts [:uri, :options]
@schemes ["http", "https"]

@impl true
Expand Down Expand Up @@ -37,13 +37,13 @@ defmodule Stow.Plug.Source do

defp get_req_headers(conn, opts) do
fetch_headers(conn.private.stow.source, :req) ||
fetch_headers(Keyword.get(opts, :extras), :req) || []
fetch_headers(Keyword.get(opts, :options), :req) || []
end

# to fix: refactor this to the plug utils module
defp fetch_headers(nil, _type), do: nil
defp fetch_headers(%Source{extras: %{headers: %{req: h}}}, :req) when h != [], do: h
defp fetch_headers(%Source{extras: %{headers: %{resp: h}}}, :resp) when h != [], do: h
defp fetch_headers(%Source{options: %{headers: %{req: h}}}, :req) when h != [], do: h
defp fetch_headers(%Source{options: %{headers: %{resp: h}}}, :resp) when h != [], do: h
defp fetch_headers(%{headers: %{req: h}}, :req) when h != [], do: h
defp fetch_headers(%{headers: %{resp: h}}, :resp) when h != [], do: h
defp fetch_headers(%{}, _), do: nil
Expand All @@ -54,7 +54,7 @@ defmodule Stow.Plug.Source do
|> Macro.camelize()
|> then(fn source -> Module.concat(Source, source) end)
|> apply(:get, [conn, opts |> Keyword.drop(@plug_opts)])
|> update_conn(conn, uri, fetch_headers(Keyword.get(opts, :extras), :resp) || [])
|> update_conn(conn, uri, fetch_headers(Keyword.get(opts, :options), :resp) || [])
end

defp normalise_scheme_name(scheme) when scheme in [:http, :https], do: "http"
Expand Down
8 changes: 4 additions & 4 deletions lib/stow/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ defmodule Stow.Source do

alias Stow.Http.Client, as: HttpClient
alias Stow.Http.Headers
alias Stow.Source.Extras
alias Stow.Options

defstruct [:uri, :status, extras: %Extras{}]
defstruct [:uri, :status, options: %Options{}]

@type t :: %__MODULE__{
uri: binary() | nil,
extras: Extras.t(),
options: Options.t(),
status: nil | :ok | {:error, term()}
}

Expand All @@ -23,7 +23,7 @@ defmodule Stow.Source do
def new(uri, headers \\ []) do
%__MODULE__{
uri: uri,
extras: %Extras{
options: %Options{
headers: %Headers{
req: Keyword.get(headers, :req_headers, []),
resp: Keyword.get(headers, :resp_headers, [])
Expand Down
2 changes: 1 addition & 1 deletion test/stow/pipeline/source_sink_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ defmodule Stow.Pipeline.SourceSinkTest do
source: %Source{
status: :ok,
uri: ^src_uri,
extras: %{headers: %{req: [], resp: []}}
options: %{headers: %{req: [], resp: []}}
},
sink: %Sink{uri: ^sink_uri, status: :ok}
} = conn.private.stow
Expand Down
4 changes: 2 additions & 2 deletions test/stow/plug/source_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Stow.Plug.SourceTest do

plug(Source,
uri: "http://localhost/path/to/source?foo=bar",
extras: %{
options: %{
headers: %{
req: [{"accept", "text/html"}, {"accept-charset", "utf-8"}],
resp: [{"server", "apache/2.4.1 (unix)"}, {"cache-control", "max-age=604800"}]
Expand Down Expand Up @@ -60,7 +60,7 @@ defmodule Stow.Plug.SourceTest do
conn =
Source.call(conn,
uri: uri,
extras: %{
options: %{
headers: %{
req: context.req_headers,
resp: context.resp_headers
Expand Down

0 comments on commit baa2eb2

Please sign in to comment.