Current section

Files

Jump to
teac lib teac api streams markers.ex
Raw

lib/teac/api/streams/markers.ex

defmodule Teac.Api.Streams.Markers do
alias Teac.Api
@get_schema NimbleOptions.new!(
user_id: [type: :string],
video_id: [type: :string],
first: [type: :pos_integer],
before: [type: :string],
after: [type: :string]
)
@post_schema NimbleOptions.new!(
user_id: [type: :string, required: true],
description: [type: :string]
)
@doc """
Gets a list of markers from the user's most recent stream or from the specified VOD.
## Authorization
Requires a user access token with the `user:read:broadcast` scope.
## Options
One of `:user_id` or `:video_id` is required.
* `:user_id` - returns markers from this user's most recent stream.
* `:video_id` - returns markers from this VOD.
* `:first` - optional. Maximum items to return. Default: 20, max: 100.
* `:before` - optional. Cursor for backward pagination.
* `:after` - optional. Cursor for forward pagination.
"""
@spec get(Teac.Client.t(), keyword()) ::
{:ok, list(map()), String.t() | nil} | {:error, Teac.Error.t()}
def get(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @get_schema) do
{:ok, opts} ->
params =
[
user_id: opts[:user_id],
video_id: opts[:video_id],
first: opts[:first],
before: opts[:before],
after: opts[:after]
]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[
base_url: Api.uri("streams/markers"),
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 markers, 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 """
Creates a marker in the stream of a user currently broadcasting.
## Authorization
Requires a user access token with the `channel:manage:broadcast` scope.
## Options
* `:user_id` - required. The ID of the broadcaster to create the marker for.
* `:description` - optional. A short description of the marker. Max 140 characters.
"""
@spec post(Teac.Client.t(), keyword()) :: {:ok, list(map())} | {:error, Teac.Error.t()}
def post(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @post_schema) do
{:ok, opts} ->
body =
%{user_id: opts[:user_id], description: opts[:description]}
|> Enum.reject(fn {_, v} -> is_nil(v) end)
|> Map.new()
[
base_url: Api.uri("streams/markers"),
headers: Api.headers(client),
json: body
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.post!()
|> Api.handle_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
end