Current section
Files
Jump to
Current section
Files
lib/teac/api/games/top.ex
defmodule Teac.Api.Games.Top do
alias Teac.Api
@schema NimbleOptions.new!(
after: [type: :string],
before: [type: :string],
first: [type: :pos_integer]
)
@doc """
Gets games sorted by number of current viewers, most popular first.
## Authorization
Requires an app access token or user access token.
## Options
* `:first` - optional. Maximum number of 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
case NimbleOptions.validate(opts, @schema) do
{:ok, opts} ->
params =
[after: opts[:after], before: opts[:before], first: opts[:first]]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[
base_url: Api.uri("games/top"),
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 top games, 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