defmodule Teac.Api.EventSub.Conduits do alias Teac.Api @post_schema NimbleOptions.new!(shard_count: [type: :integer, required: true]) @patch_schema NimbleOptions.new!( id: [type: :string, required: true], shard_count: [type: :integer, required: true] ) @delete_schema NimbleOptions.new!(id: [type: :string, required: true]) @doc """ Gets the conduits for the client ID in the access token. ## Authorization Requires an app access token. """ @spec get(Teac.Client.t(), keyword()) :: {:ok, list(map())} | {:error, Teac.Error.t()} def get(%Teac.Client{} = client, _opts \\ []) do [base_url: Api.uri("eventsub/conduits"), headers: Api.headers(client)] |> Keyword.merge(Application.get_env(:teac, :api_req_options, [])) |> Req.get() |> Api.handle_response() end @doc """ Creates a new conduit. ## Authorization Requires an app access token. ## Options * `:shard_count` - required. The number of shards to create for the conduit. """ @spec post(Teac.Client.t(), keyword()) :: {:ok, list(map())} | {:error, Teac.Error.t()} def post(%Teac.Client{} = client, opts \\ []) do case NimbleOptions.validate(opts, @post_schema) do {:ok, opts} -> [ base_url: Api.uri("eventsub/conduits"), headers: Api.headers(client), json: %{shard_count: opts[:shard_count]} ] |> Keyword.merge(Application.get_env(:teac, :api_req_options, [])) |> Req.post() |> Api.handle_response() {:error, %NimbleOptions.ValidationError{} = err} -> {:error, err.message} end end @doc """ Updates a conduit's shard count. ## Authorization Requires an app access token. ## Options * `:id` - required. The ID of the conduit to update. * `:shard_count` - required. The new number of shards for the conduit. """ @spec patch(Teac.Client.t(), keyword()) :: {:ok, list(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"), headers: Api.headers(client), json: %{id: opts[:id], shard_count: opts[:shard_count]} ] |> Keyword.merge(Application.get_env(:teac, :api_req_options, [])) |> Req.patch() |> Api.handle_response() {:error, %NimbleOptions.ValidationError{} = err} -> {:error, err.message} end end @doc """ Deletes a conduit. ## Authorization Requires an app access token. ## Options * `:id` - required. The ID of the conduit to delete. """ @spec delete(Teac.Client.t(), keyword()) :: {:ok, nil} | {:error, Teac.Error.t()} def delete(%Teac.Client{} = client, opts \\ []) do case NimbleOptions.validate(opts, @delete_schema) do {:ok, opts} -> [ base_url: Api.uri("eventsub/conduits"), params: [id: opts[:id]], headers: Api.headers(client) ] |> Keyword.merge(Application.get_env(:teac, :api_req_options, [])) |> Req.delete() |> Api.handle_response() {:error, %NimbleOptions.ValidationError{} = err} -> {:error, err.message} end end end