Current section
Files
Jump to
Current section
Files
lib/teac/api/event_sub/conduits/shards.ex
defmodule Teac.Api.EventSub.Conduits.Shards do
alias Teac.Api
@get_schema NimbleOptions.new!(
conduit_id: [type: :string, required: true],
status: [type: :string],
after: [type: :string]
)
@patch_schema NimbleOptions.new!(
conduit_id: [type: :string, required: true],
shards: [type: :any, required: true]
)
@doc """
Gets a list of shards for a conduit.
## Authorization
Requires an app access token.
## Options
* `:conduit_id` - required. The ID of the conduit whose shards to get.
* `:status` - optional. Filter by shard status.
* `: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 =
[conduit_id: opts[:conduit_id], status: opts[:status], after: opts[:after]]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[
base_url: Api.uri("eventsub/conduits/shards"),
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 shards for a conduit, 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 """
Updates shard transport information for a conduit.
## Authorization
Requires an app access token.
## Options
* `:conduit_id` - required. The ID of the conduit whose shards to update.
* `:shards` - required. List of shard maps to update. Each map must include `:id` and `:transport`.
"""
@spec patch(Teac.Client.t(), keyword()) :: {:ok, map()} | {:error, Teac.Error.t()}
def patch(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @patch_schema) do
{:ok, opts} ->
[
base_url: Api.uri("eventsub/conduits/shards"),
headers: Api.headers(client),
json: %{conduit_id: opts[:conduit_id], shards: opts[:shards]}
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.patch!()
|> Api.handle_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
end