-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_api.ex
62 lines (49 loc) · 1.33 KB
/
json_api.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
defmodule JsonAPI do
def query(cat, id \\ 0, keys \\ [] ) do
categories = %{
"posts" => 100,
"comments" => 500,
"albums" => 100,
"photos" => 5000,
"todos" => 200,
"users" => 10
}
base = "https://jsonplaceholder.typicode.com/"
cond do
cat not in Map.keys(categories) ->
{:error, ~s('#{cat}' is not a valid category.)}
id > categories[cat] ->
{:error, ~s(The maximum value of '#{cat}' is #{categories[cat]}.)}
is_list(keys) and length(keys) > 0 and id > 0 ->
resp = get_data([base, cat, "/", to_string(id)])
case resp do
{:ok, body } ->
{:ok, get_in(body, keys)}
_ -> resp
end
id > 0 ->
get_data([base, cat, "/", to_string(id)])
true ->
get_data([base, cat])
end
end
defp get_data(url) do
url
|> :erlang.iolist_to_binary
|> HTTPoison.get
|> handle_response
end
defp handle_response( {:ok, %{ status_code: 200, body: body } = _response} ) do
data = body
|> Poison.Parser.parse!(%{})
{:ok, data}
end
defp handle_response( {:ok, %{ status_code: code, body: body } = _response} ) do
message = body
|> Poison.Parser.parse!(%{})
{:error, code, message}
end
defp handle_response( response ) do
response
end
end