Current section
Files
Jump to
Current section
Files
lib/teac/api/bits/extensions.ex
defmodule Teac.Api.Bits.Extensions do
alias Teac.Api
@get_schema NimbleOptions.new!(should_include_all: [type: :boolean, default: false])
@put_schema NimbleOptions.new!(
sku: [type: :string, required: true],
cost_amount: [type: :integer, required: true],
cost_type: [type: :string, required: true],
display_name: [type: :string, required: true],
in_development: [type: :boolean],
expiration: [type: :string],
is_broadcast: [type: :boolean]
)
@doc """
Gets a list of Bits products that belongs to an extension.
## Authorization
Requires an app access token. The client ID in the access token must match the extension's client ID.
## Options
* `:should_include_all` - optional. If `true`, includes both enabled and disabled products. Default: `false`.
"""
@spec get(Teac.Client.t(), keyword()) :: {:ok, list(map())} | {:error, Teac.Error.t()}
def get(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @get_schema) do
{:ok, opts} ->
[
base_url: Api.uri("bits/extensions"),
params: [should_include_all: opts[:should_include_all]],
headers: Api.headers(client)
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.get!()
|> Api.handle_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
@doc """
Adds or updates a Bits product that the extension created.
## Authorization
Requires an app access token. The client ID in the access token must match the extension's client ID.
## Options
* `:sku` - required. The product's SKU. Must be unique across all products for the extension.
* `:cost_amount` - required. The cost of the product in Bits.
* `:cost_type` - required. The cost type. Valid value: `"bits"`.
* `:display_name` - required. The product's name displayed in the extension.
* `:in_development` - optional. If `true`, the product is in development and not visible to users.
* `:expiration` - optional. RFC3339 timestamp for when the product expires.
* `:is_broadcast` - optional. If `true`, Twitch broadcasts the purchase to all viewers.
"""
@spec put(Teac.Client.t(), keyword()) :: {:ok, map()} | {:error, Teac.Error.t()}
def put(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @put_schema) do
{:ok, opts} ->
body =
%{
sku: opts[:sku],
cost: %{amount: opts[:cost_amount], type: opts[:cost_type]},
display_name: opts[:display_name],
in_development: opts[:in_development],
expiration: opts[:expiration],
is_broadcast: opts[:is_broadcast]
}
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|> Map.new()
[
base_url: Api.uri("bits/extensions"),
headers: Api.headers(client),
json: body
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.put!()
|> Api.handle_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
end