Packages

This application provides the current weather data for the given geo coordinates (latitude/longitude) based on openweathermap.org v2.5.

Current section

Files

Jump to
elixir_weather_data lib open_weather_map_api_http_client.ex
Raw

lib/open_weather_map_api_http_client.ex

defmodule ElixirWeatherData.OpenWeatherMapApi.HttpClient do
@behaviour ElixirWeatherData.OpenWeatherMapApi
def send_request(url) do
request_api(url)
end
defp request_api(url) do
try do
# case HTTPoison.get(url, [], [timeout: 500, recv_timeout: 500]) do
# {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, body}
# {:ok, %HTTPoison.Response{status_code: 404}} -> {:error, :page_not_found}
# {:ok, %HTTPoison.Response{status_code: _}} -> {:error, :unknown_error}
# {:error, %HTTPoison.Error{reason: reason}} -> {:error, reason}
# _ -> {:error, :unknown_error}
# end
# IO.inspect "HTTPoison.get!('#{url}', [], [timeout: 500, recv_timeout: 500])"
case HTTPoison.get!(url, [], [timeout: 500, recv_timeout: 500]) do
%HTTPoison.Response{status_code: 200, body: body} -> {:ok, body}
%HTTPoison.Response{status_code: 404} -> {:error, :page_not_found}
%HTTPoison.Response{status_code: _} -> {:error, :unknown_error}
%HTTPoison.Error{reason: reason} -> {:error, reason}
_ -> {:error, :unknown_error}
end
rescue
_ -> {:error, :timeout}
catch
_ -> {:error, :timeout}
end
end
end