-
Notifications
You must be signed in to change notification settings - Fork 4
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 #18 from Kociamber/update_versions_and_code
Update to Elixir 1.17, update deps, tests and major refactor
- Loading branch information
Showing
26 changed files
with
606 additions
and
607 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
elixir 1.12.1 | ||
elixir 1.17.1-otp-27 | ||
erlang 27.0 |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
defmodule ExOwm.Application do | ||
@moduledoc false | ||
require Logger | ||
use Application | ||
|
||
|
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 |
---|---|---|
@@ -1,47 +1,65 @@ | ||
defmodule ExOwm.Api do | ||
@moduledoc """ | ||
This module contains OpenWeatherMap API related functions. | ||
This module contains functions for interacting with the OpenWeatherMap API. | ||
It prepares request strings, makes API calls, and parses the responses. | ||
""" | ||
alias ExOwm.RequestString | ||
alias HTTPoison.{Error, Response} | ||
|
||
@doc """ | ||
Prepares request string basing on given params, calls OWM API, parses and | ||
decodes the answers. | ||
Prepares a request string based on the given parameters, calls the OWM API, | ||
and parses the JSON response. | ||
## Parameters | ||
- `api_call_type` (atom): The type of API call (e.g., `:get_weather`, `:get_current_weather`). | ||
- `location` (map): The location parameters (e.g., city, coordinates, zip code). | ||
- `opts` (term): Optional parameters for the API call (e.g., type, mode, units, cnt, lang). | ||
## Returns | ||
- (map): The parsed JSON response. | ||
- `{:error, term, term}`: An error tuple containing the error type and the response. | ||
""" | ||
@spec send_and_parse_request(atom, map, term) :: map | {:error, term, term} | ||
def send_and_parse_request(api_call_type, location, opts) do | ||
RequestString.build(api_call_type, location, opts) | ||
api_call_type | ||
|> RequestString.build(location, opts) | ||
|> call_api() | ||
|> parse_json() | ||
|> parse_response() | ||
end | ||
|
||
defp call_api(string) do | ||
case HTTPoison.get(string) do | ||
{:ok, %HTTPoison.Response{status_code: 200, body: json_body}} -> | ||
@spec call_api(String.t()) :: {:ok, String.t()} | {:error, atom, term} | {:error, term} | ||
defp call_api(url) do | ||
case HTTPoison.get(url) do | ||
{:ok, %Response{status_code: 200, body: json_body}} -> | ||
{:ok, json_body} | ||
|
||
{:ok, %HTTPoison.Response{status_code: 404, body: json_body}} -> | ||
{:ok, %Response{status_code: 404, body: json_body}} -> | ||
{:error, :not_found, json_body} | ||
|
||
{:ok, %HTTPoison.Response{status_code: 400, body: json_body}} -> | ||
{:error, :not_found, json_body} | ||
{:ok, %Response{status_code: 400, body: json_body}} -> | ||
{:error, :bad_request, json_body} | ||
|
||
{:ok, %HTTPoison.Response{status_code: 401, body: json_body}} -> | ||
{:ok, %Response{status_code: 401, body: json_body}} -> | ||
{:error, :api_key_invalid, json_body} | ||
|
||
{:ok, response} -> | ||
{:error, :unknown_api_response, response} | ||
|
||
{:error, reason} -> | ||
{:error, %Error{} = reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp parse_json({:ok, json}), do: Jason.decode(json) | ||
@spec parse_response({:ok, String.t()} | {:error, atom, String.t()} | {:error, term}) :: | ||
map | {:error, term, term} | ||
defp parse_response({:ok, json}), do: Jason.decode(json) | ||
|
||
defp parse_json({:error, :unknown_api_response, response}), | ||
defp parse_response({:error, :unknown_api_response, response}), | ||
do: {:error, :unknown_api_response, response} | ||
|
||
defp parse_json({:error, reason, json_body}), do: {:error, reason, Jason.decode!(json_body)} | ||
defp parse_json({:error, %HTTPoison.Error{} = reason}), do: {:error, reason} | ||
defp parse_response({:error, reason, json_body}), do: {:error, reason, Jason.decode(json_body)} | ||
|
||
defp parse_response({:error, %Error{} = reason}), do: {:error, reason} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
defmodule ExOwm.Cache do | ||
@moduledoc false | ||
use Nebulex.Cache, otp_app: :ex_owm, adapter: Nebulex.Adapters.Local | ||
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
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
Oops, something went wrong.