Store.APIUsage (fnord v0.9.29)

View Source

Module for recording and checking API usage data. Cross OS process coordination is handled with regard to file reads/writes. It is up to the caller to ensure that requests are internally ordered for consistency.

Summary

Functions

Checks if a request can be made for the given model based on the last recorded usage data for the model. If a request can be made, returns :ok. Otherwise, returns {:wait, milliseconds} indicating how long to wait before attempting another request.

Records API usage for a provided model. If model is nil, returns http_result unchanged. For 2xx {:ok, %{headers: headers, status: code}} results and a binary model, records usage data and updates the store. Other responses or errors are returned unchanged.

Returns the path to the usage store file.

Types

http_result()

@type http_result() ::
  ok_result()
  | {:http_error, {status(), String.t()}}
  | {:transport_error, any()}

model_usage()

@type model_usage() :: %{
  updated_at: non_neg_integer(),
  requests_max: non_neg_integer(),
  requests_left: non_neg_integer(),
  requests_reset: non_neg_integer(),
  tokens_max: non_neg_integer(),
  tokens_left: non_neg_integer(),
  tokens_reset: non_neg_integer()
}

ok_result()

@type ok_result() :: {:ok, %{body: map(), headers: list(), status: status()}}

status()

@type status() :: non_neg_integer()

usage()

@type usage() :: %{optional(binary()) => model_usage()}

Functions

check(model)

@spec check(binary()) :: :ok | {:wait, non_neg_integer()} | {:error, term()}

Checks if a request can be made for the given model based on the last recorded usage data for the model. If a request can be made, returns :ok. Otherwise, returns {:wait, milliseconds} indicating how long to wait before attempting another request.

record_for_model(model, http_result)

@spec record_for_model(binary() | nil, http_result()) :: http_result()

Records API usage for a provided model. If model is nil, returns http_result unchanged. For 2xx {:ok, %{headers: headers, status: code}} results and a binary model, records usage data and updates the store. Other responses or errors are returned unchanged.

store_path()

@spec store_path() :: binary()

Returns the path to the usage store file.