philomena/lib/philomena/http.ex

28 lines
858 B
Elixir
Raw Normal View History

2019-11-28 18:12:10 +01:00
defmodule Philomena.Http do
2019-11-29 01:34:22 +01:00
@user_agent ["User-Agent": "Mozilla/5.0 (X11; Philomena; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0"]
2019-11-28 18:12:10 +01:00
def get!(url, headers \\ [], options \\ []) do
2019-11-29 01:56:36 +01:00
headers = Keyword.merge(@user_agent, headers) |> add_host(url)
2019-11-28 18:12:10 +01:00
options = Keyword.merge(options, proxy: proxy_host())
HTTPoison.get!(url, headers, options)
end
def head!(url, headers \\ [], options \\ []) do
2019-11-29 01:56:36 +01:00
headers = Keyword.merge(@user_agent, headers) |> add_host(url)
2019-11-28 18:12:10 +01:00
options = Keyword.merge(options, proxy: proxy_host())
HTTPoison.head!(url, headers, options)
end
2019-11-29 01:56:36 +01:00
# Add host for caching proxies, since hackney doesn't do it for us
defp add_host(headers, url) do
%{host: host} = URI.parse(url)
Keyword.merge(["Host": host], headers)
end
2019-11-28 18:12:10 +01:00
defp proxy_host do
Application.get_env(:philomena, :proxy_host)
end
end