Current section
Files
Jump to
Current section
Files
lib/teac/api/streams.ex
defmodule Teac.Api.Streams do
alias Teac.Api
@doc """
Gets a list of all streams.
## Authorization
Requires an app access token or user access token.
## Options
Combined total of `:user_ids`, `:user_logins`, and `:game_ids` may not exceed 100.
* `:user_ids` - optional. Filter by user IDs.
* `:user_logins` - optional. Filter by user login names.
* `:game_ids` - optional. Filter by game IDs.
* `:type` - optional. Filter by stream type. Valid values: `"all"`, `"live"`.
* `:first` - optional. Maximum items to return. Default: 20, max: 100.
* `:after` - optional. Cursor for forward pagination.
* `:before` - optional. Cursor for backward pagination.
"""
@spec get(Teac.Client.t(), keyword()) ::
{:ok, list(map()), String.t() | nil} | {:error, Teac.Error.t()}
def get(%Teac.Client{} = client, opts \\ []) do
user_ids = opts |> Keyword.get(:user_ids, []) |> List.wrap() |> Enum.map(&to_string/1)
user_logins = opts |> Keyword.get(:user_logins, []) |> List.wrap() |> Enum.map(&to_string/1)
game_ids = opts |> Keyword.get(:game_ids, []) |> List.wrap() |> Enum.map(&to_string/1)
total = length(user_ids) + length(user_logins) + length(game_ids)
if total > 100 do
{:error, "Total IDs and login names cannot exceed 100"}
else
after_cursor = Keyword.get(opts, :after, nil)
before_cursor = Keyword.get(opts, :before, nil)
first = Keyword.get(opts, :first, nil)
params = Enum.map(user_ids, &{:user_id, &1}) ++ Enum.map(user_logins, &{:user_login, &1})
params =
case Keyword.get(opts, :type) do
nil -> params
type when type in ["all", "live"] -> params ++ [type: type]
_ -> params
end
params =
params ++
([after: after_cursor, before: before_cursor, first: first]
|> Enum.reject(fn {_k, v} -> is_nil(v) end))
[
base_url: Api.uri("streams"),
params: params,
headers: Api.headers(client)
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.get()
|> Api.handle_paginated_response()
end
end
@doc "Streams all live streams matching the given opts, 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
end