Current section
Files
Jump to
Current section
Files
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.
## Options
* `:broadcaster_ids` - required. List of broadcaster IDs to get information for. Max 100.
"""
@spec get(Teac.Client.t(), keyword()) :: {:ok, list(map())} | {:error, Teac.Error.t()}
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 with the `channel:manage:broadcast` scope.
## Options
* `:broadcaster_id` - required. The ID of the broadcaster whose channel to update.
* `:game_id` - optional. The ID of the game the broadcaster is playing.
* `:broadcaster_language` - optional. The ISO 639-1 language code of the broadcast.
* `:title` - optional. Title of the stream. Max 140 characters.
* `:delay` - optional. Stream delay in seconds. Requires Partner status.
* `:tags` - optional. List of tag strings to apply to the channel.
* `:content_classification_labels` - optional. List of content classification label maps.
* `:is_branded_content` - optional. If `true`, the channel has branded content.
"""
@spec patch(Teac.Client.t(), keyword()) :: {:ok, nil} | {:error, Teac.Error.t()}
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