defmodule Teac.Api.EventSub.Subscriptions do alias Teac.Api @get_schema NimbleOptions.new!(after: [type: :string]) @post_schema NimbleOptions.new!(payload: [type: :any, required: true]) def get(%Teac.Client{} = client, opts \\ []) do case NimbleOptions.validate(opts, @get_schema) do {:ok, opts} -> params = if opts[:after], do: [after: opts[:after]], else: [] [ base_url: Api.uri("eventsub/subscriptions"), 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 def stream(%Teac.Client{} = client, opts \\ []) do Teac.Api.Pagination.stream(client, &get/2, opts) end def post(%Teac.Client{} = client, opts \\ []) do case NimbleOptions.validate(opts, @post_schema) do {:ok, opts} -> [ base_url: Api.uri("eventsub/subscriptions"), headers: Api.headers(client), json: opts[:payload] ] |> Keyword.merge(Application.get_env(:teac, :api_req_options, [])) |> Req.post!() |> Api.handle_response() {:error, %NimbleOptions.ValidationError{} = err} -> {:error, err.message} end end def delete(%Teac.Client{} = client, _opts \\ []) do [base_url: Api.uri("eventsub/subscriptions"), headers: Api.headers(client)] |> Keyword.merge(Application.get_env(:teac, :api_req_options, [])) |> Req.delete!() |> Api.handle_response() end end