allow http headers
This commit is contained in:
1 parent
fd317f2af4
commit
b0da248ff0
1 file changed
+15
-3
@@ -24,9 +24,15 @@ defmodule FeedReader.ReqTransport do
|
|||||||
def request(url, opts) when is_binary(url) do
|
def request(url, opts) when is_binary(url) do
|
||||||
http_options = Keyword.get(opts, :http_options, [])
|
http_options = Keyword.get(opts, :http_options, [])
|
||||||
|
|
||||||
|
headers = Keyword.get(http_options, :headers, @default_headers)
|
||||||
|
etag = Keyword.get(opts, :previous_etag)
|
||||||
|
|
||||||
|
headers =
|
||||||
|
if is_binary(etag), do: conditional_headers(headers, etag), else: headers
|
||||||
|
|
||||||
request_options =
|
request_options =
|
||||||
http_options
|
http_options
|
||||||
|> Keyword.put_new(:headers, @default_headers)
|
|> Keyword.put(:headers, headers)
|
||||||
|> Keyword.put(:into, :self)
|
|> Keyword.put(:into, :self)
|
||||||
|> Keyword.put(:raw, true)
|
|> Keyword.put(:raw, true)
|
||||||
|> Keyword.put(:decode_body, false)
|
|> Keyword.put(:decode_body, false)
|
||||||
@@ -49,13 +55,13 @@ defmodule FeedReader.ReqTransport do
|
|||||||
|
|
||||||
defp handle_response(request, response) do
|
defp handle_response(request, response) do
|
||||||
case response.status do
|
case response.status do
|
||||||
status when status in 200..299 ->
|
status when status in 200..299 or status == 304 ->
|
||||||
{:ok,
|
{:ok,
|
||||||
%{
|
%{
|
||||||
status: status,
|
status: status,
|
||||||
headers: Req.get_headers_list(response),
|
headers: Req.get_headers_list(response),
|
||||||
final_url: URI.to_string(request.url),
|
final_url: URI.to_string(request.url),
|
||||||
body: response.body
|
body: if(status == 304, do: [], else: response.body)
|
||||||
}}
|
}}
|
||||||
|
|
||||||
403 ->
|
403 ->
|
||||||
@@ -75,6 +81,12 @@ defmodule FeedReader.ReqTransport do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp conditional_headers(headers, etag) when is_binary(etag) do
|
||||||
|
headers
|
||||||
|
|> Enum.reject(fn {name, _value} -> String.downcase(name) == "if-none-match" end)
|
||||||
|
|> Kernel.++([{"if-none-match", etag}])
|
||||||
|
end
|
||||||
|
|
||||||
defp retry_attempts(opts) do
|
defp retry_attempts(opts) do
|
||||||
case Keyword.get(opts, :http_retry_attempts, 5) do
|
case Keyword.get(opts, :http_retry_attempts, 5) do
|
||||||
attempts when is_integer(attempts) and attempts >= 0 -> attempts
|
attempts when is_integer(attempts) and attempts >= 0 -> attempts
|
||||||
|
|||||||
Reference in new issue
Block a user