Current section
Files
Jump to
Current section
Files
lib/teac/api/schedule.ex
defmodule Teac.Api.Schedule do
alias Teac.Api
@get_schema NimbleOptions.new!(
broadcaster_id: [type: :string, required: true],
after: [type: :string],
first: [type: :pos_integer],
start_time: [type: :string],
utc_offset: [type: :string]
)
@doc """
Gets the broadcaster's streaming schedule.
## Authorization
Requires an app access token or user access token.
## Options
* `:broadcaster_id` - required. The broadcaster whose schedule to get.
* `:start_time` - optional. RFC3339 timestamp. Returns segments on or after this time.
* `:utc_offset` - optional. A UTC offset for the schedule. Format: `"+HH:MM"` or `"-HH:MM"`.
* `:first` - optional. Maximum items to return. Default: 20, max: 25.
* `:after` - optional. Cursor for forward pagination.
"""
@spec get(Teac.Client.t(), keyword()) ::
{:ok, list(map()), String.t() | nil, Teac.RateLimit.t()} | {:error, Teac.Error.t()}
def get(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @get_schema) do
{:ok, opts} ->
params =
[
broadcaster_id: opts[:broadcaster_id],
after: opts[:after],
first: opts[:first],
start_time: opts[:start_time],
utc_offset: opts[:utc_offset]
]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[base_url: Api.uri("schedule"), params: params, headers: Api.headers(client)]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.get()
|> Api.handle_paginated_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
@doc "Streams all schedule segments, automatically following pagination cursors."
@spec stream(Teac.Client.t(), keyword()) :: Enumerable.t()
def stream(%Teac.Client{} = client, opts \\ []) do
Teac.Api.Pagination.stream(client, &get/2, opts)
end
@doc """
Deletes all of the broadcaster's scheduled streams.
## Authorization
Requires a user access token with the `channel:manage:schedule` scope.
"""
@spec delete(Teac.Client.t(), keyword()) :: {:ok, nil, Teac.RateLimit.t()} | {:error, Teac.Error.t()}
def delete(%Teac.Client{} = client, _opts \\ []) do
[base_url: Api.uri("schedule/"), headers: Api.headers(client)]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.delete()
|> Api.handle_response()
end
end