Current section
Files
Jump to
Current section
Files
lib/meili/batch.ex
defmodule Meili.Batch do
@moduledoc """
Manages Meilisearch task batches.
"""
alias Meili.Client
@doc """
Lists all task batches.
Supports optional query params for filtering and pagination.
## Examples
Meili.Batch.list()
Meili.Batch.list(limit: 5)
Meili.Batch.list(client, limit: 5)
"""
@spec list(Meili.Client.t() | Keyword.t() | nil, Keyword.t()) ::
{:ok, map()} | {:error, Meili.Error.t()}
def list(client_or_opts \\ nil, opts \\ []) do
case client_or_opts do
%Client{} = client ->
do_list(client, opts)
nil ->
do_list(Meili.default_client(), [])
opts when is_list(opts) ->
do_list(Meili.default_client(), opts)
end
end
@doc """
Lists all task batches, raising on error.
"""
@spec list!(Meili.Client.t() | Keyword.t() | nil, Keyword.t()) :: map() | no_return()
def list!(client_or_opts \\ nil, opts \\ []) do
case list(client_or_opts, opts) do
{:ok, body} -> body
{:error, exception} -> raise exception
end
end
defp do_list(client, opts) do
params =
opts
|> Map.new()
|> Meili.Util.camelize_keys()
|> Enum.to_list()
Client.request(client, :get, "/batches", params: params)
end
@doc """
Retrieves a single batch by UID.
## Examples
Meili.Batch.get(1)
Meili.Batch.get(client, 1)
"""
@spec get(Meili.Client.t() | integer() | String.t(), integer() | String.t() | nil) ::
{:ok, map()} | {:error, Meili.Error.t()}
def get(client_or_uid, uid_or_nil \\ nil) do
case client_or_uid do
%Client{} = client ->
Client.request(client, :get, "/batches/#{uid_or_nil}")
uid when is_integer(uid) or is_binary(uid) ->
Client.request(Meili.default_client(), :get, "/batches/#{uid}")
end
end
@doc """
Retrieves a single batch, raising on error.
"""
@spec get!(Meili.Client.t() | integer() | String.t(), integer() | String.t() | nil) ::
map() | no_return()
def get!(client_or_uid, uid_or_nil \\ nil) do
case get(client_or_uid, uid_or_nil) do
{:ok, body} -> body
{:error, exception} -> raise exception
end
end
end