Current section
Files
Jump to
Current section
Files
lib/teac/api/search/channels.ex
defmodule Teac.Api.Search.Channels do
alias Teac.Api
@schema NimbleOptions.new!(
query: [type: :string, required: true],
live_only: [type: :boolean],
after: [type: :string],
first: [type: :pos_integer]
)
@doc """
Gets the channels that match the specified query.
## Authorization
Requires an app access token or user access token.
## Options
* `:query` - required. The search query.
* `:live_only` - optional. If `true`, returns only live channels. Default: `false`.
* `:first` - optional. Maximum items to return. Default: 20, 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 =
[
query: opts[:query],
live_only: opts[:live_only],
after: opts[:after],
first: opts[:first]
]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[base_url: Api.uri("search/channels"), 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 matching channels, 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