-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.ex
85 lines (72 loc) · 2.39 KB
/
channels.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
defmodule Holodex.Api.Channels do
@moduledoc """
Holodex `Channel` resource API module.
Includes client functions for interacting with the `Channel` resource.
"""
alias Holodex.Api.Client
alias Holodex.Model.Channel
@type opts() ::
%{
lang: binary() | nil,
limit: 0..50 | nil,
offset: integer() | nil,
order: binary() | nil,
org: binary() | nil,
sort: binary() | nil,
type: binary() | nil
}
| %{}
@doc """
Get all channels from Holodex, optionally with given parameters.
Raises an exception in case of failure.
"""
@spec list_channels!(opts()) :: [Channel.t()] | no_return()
def list_channels!(opts \\ %{}) do
with url <- build_channels_url(opts),
response <- Client.get!(url) do
Poison.decode!(response.body, %{as: [%Channel{}]})
end
end
@doc """
Get all channels from Holodex, optionally with given parameters.
Returns a tuple containing the response body or an error.
"""
@spec list_channels(opts()) ::
{:ok, [Channel.t()]} | {:error, HTTPoison.Error.t()} | {:error, Exception.t()}
def list_channels(opts \\ %{}) do
with url <- build_channels_url(opts),
{:ok, response} <- Client.get(url),
{:ok, decoded} <- Poison.decode(response.body, %{as: [%Channel{}], keys: :atoms!}) do
{:ok, decoded}
end
end
@doc """
Get info about a particular channel by its Youtube channel ID.
Raises an exception in case of failure.
"""
@spec get_channel!(String.t()) :: Channel.t() | no_return()
def get_channel!(channel_id) do
with response <- Client.get!("/channels/#{channel_id}") do
Poison.decode!(response.body, %{as: %Channel{}})
end
end
@doc """
Get info about a particular channel by its Youtube channel ID.
Returns a tuple containing the channel info or an error.
"""
@spec get_channel(String.t()) ::
{:ok, Channel.t()} | {:error, HTTPoison.Error.t()} | {:error, Exception.t()}
def get_channel(channel_id) do
with {:ok, response} <- Client.get("/channels/#{channel_id}"),
{:ok, decoded} <- Poison.decode(response.body, %{as: %Channel{}, keys: :atoms!}) do
{:ok, decoded}
end
end
defp build_channels_url(opts) do
query = URI.encode_query(opts)
"/channels"
|> URI.parse()
|> Map.put(:query, query)
|> URI.to_string()
end
end