Current section
Files
Jump to
Current section
Files
lib/teac/api/games.ex
defmodule Teac.Api.Games do
alias Teac.Api
@schema NimbleOptions.new!(
ids: [type: {:list, :string}],
names: [type: {:list, :string}],
igdb_ids: [type: {:list, :string}]
)
@doc """
Gets information about specified categories or games.
## Authorization
Requires an app access token or user access token.
## Options
At least one of the following is required. Total across all lists may not exceed 100.
* `:ids` - list of game IDs.
* `:names` - list of game names.
* `:igdb_ids` - list of IGDB IDs.
"""
@spec get(Teac.Client.t(), keyword()) :: {:ok, list(map()), Teac.RateLimit.t()} | {:error, Teac.Error.t()}
def get(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @schema) do
{:ok, opts} ->
ids = opts[:ids] || []
names = opts[:names] || []
igdb_ids = opts[:igdb_ids] || []
if ids == [] and names == [] and igdb_ids == [] do
{:error, "requires at least one of ids, names, or igdb_ids"}
else
total = length(ids) + length(names) + length(igdb_ids)
if total > 100 do
{:error, "exceeds more than 100 total ids, names, and igdb_ids"}
else
params =
Enum.map(ids, &{:id, &1}) ++
Enum.map(names, &{:name, &1}) ++
Enum.map(igdb_ids, &{:igdb_id, &1})
[
base_url: Api.uri("games"),
params: params,
headers: Api.headers(client)
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.get()
|> Api.handle_response()
end
end
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
end