defmodule Teac.Api.Streams.Followed do alias Teac.Api @schema NimbleOptions.new!( user_id: [type: :string, required: true], after: [type: :string], first: [type: :pos_integer] ) @doc """ Gets the list of broadcasters that the user follows and that are streaming live. ## Authorization Requires a user access token with the `user:read:follows` scope. ## Options * `:user_id` - required. The ID of the user whose followed streams to get. * `:first` - optional. Maximum items to return. Default: 100, max: 100. * `: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, @schema) do {:ok, opts} -> params = [user_id: opts[:user_id], after: opts[:after], first: opts[:first]] |> Enum.reject(fn {_, v} -> is_nil(v) end) [base_url: Api.uri("streams/followed"), 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 followed live streams, 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