-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from danschultzer/http-adapter
Add HTTP adapter for HTTPoison
- Loading branch information
Showing
4 changed files
with
40 additions
and
4 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
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,32 @@ | ||
defmodule Premailex.HTTPoisonAdapter do | ||
@moduledoc """ | ||
Adapter module for HTTPoison. | ||
""" | ||
alias HTTPoison.Response | ||
|
||
@httpoison_opts [follow_redirect: true] | ||
|
||
@doc """ | ||
Fetches an URL and returns the body if the response has a 200 HTTP status | ||
code. | ||
## Examples | ||
iex> Premailex.HTTPoisonAdapter.get("http://localhost:4000/styles.css") | ||
{:ok, "body {color: #000;}"} | ||
iex> Premailex.HTTPoisonAdapter.get("http://localhost:4000/nonexistant.css") | ||
{:error, %HTTPoison.Response{status_code: 404}} | ||
""" | ||
@spec get(String.t()) :: {:ok, String.t()} | {:error, term()} | ||
def get(url) do | ||
url | ||
|> HTTPoison.get([], @httpoison_opts) | ||
|> process() | ||
end | ||
|
||
defp process({:ok, %Response{status_code: 200, body: body}}), do: {:ok, body} | ||
defp process({:ok, resp}), do: {:error, resp} | ||
defp process(any), do: any | ||
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