Current section

Files

Jump to
teac lib teac api channels.ex
Raw

lib/teac/api/channels.ex

defmodule Teac.Api.Channels do
alias Teac.Api
@get_schema NimbleOptions.new!(broadcaster_ids: [type: {:list, :string}, required: true])
@patch_schema NimbleOptions.new!(
broadcaster_id: [type: :string, required: true],
game_id: [type: :string],
broadcaster_language: [type: :string],
title: [type: :string],
delay: [type: :integer],
tags: [type: {:list, :string}],
content_classification_labels: [type: :any],
is_branded_content: [type: :boolean]
)
@doc """
Gets information about one or more channels.
## Authorization
Requires an app access token or user access token.
## Opts
* broadcaster_ids: required — list of broadcaster IDs, max 100
"""
@spec get(client :: Teac.Client.t(), opts :: [broadcaster_ids: [String.t()]]) ::
{:ok, map()} | {:error, any()}
def get(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @get_schema) do
{:ok, opts} ->
if length(opts[:broadcaster_ids]) > 100 do
{:error, "broadcaster_ids may contain at most 100 IDs"}
else
params = Enum.map(opts[:broadcaster_ids], &{:broadcaster_id, &1})
[
base_url: Api.uri("channels"),
params: params,
headers: Api.headers(client)
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.get!()
|> Api.handle_response()
end
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
@doc """
Updates a channel's properties.
## Authorization
Requires a user access token that includes the `channel:manage:broadcast` scope.
## Opts
* broadcaster_id: required
* game_id, broadcaster_language, title, delay, tags, content_classification_labels, is_branded_content: optional
"""
@spec patch(client :: Teac.Client.t(), opts :: [broadcaster_id: String.t()]) ::
{:ok, map()} | {:error, any()}
def patch(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @patch_schema) do
{:ok, opts} ->
body =
opts
|> Keyword.drop([:broadcaster_id])
|> Enum.reject(fn {_, v} -> is_nil(v) end)
|> Map.new()
[
base_url: Api.uri("channels"),
params: [broadcaster_id: opts[:broadcaster_id]],
json: body,
headers: Api.headers(client)
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.patch!()
|> Api.handle_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
end