Packages
A well-typed, idiomatic Gleam client for Anthropic's Claude API with streaming support and tool use
Current section
Files
Jump to
Current section
Files
src/anthropic@config.erl
-module(anthropic@config).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/anthropic/config.gleam").
-export([api_key/1, api_key_unchecked/1, api_key_to_string/1, api_key_error_to_string/1, config_options/0, with_api_key/2, with_base_url/2, with_default_model/2, with_timeout_ms/2, with_max_retries/2, load_config/1]).
-export_type([api_key_error/0, api_key/0, config/0, config_options/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Configuration management for the Anthropic client\n"
"\n"
" This module defines the configuration structure and helpers for loading\n"
" settings from explicit options or environment variables.\n"
).
-type api_key_error() :: empty_api_key.
-opaque api_key() :: {api_key, binary()}.
-type config() :: {config,
api_key(),
binary(),
gleam@option:option(binary()),
integer(),
integer()}.
-type config_options() :: {config_options,
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(integer()),
gleam@option:option(integer())}.
-file("src/anthropic/config.gleam", 55).
?DOC(
" Create a validated API key.\n"
"\n"
" Returns `Ok(ApiKey)` if the key is non-empty after trimming whitespace.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" api_key(\"sk-ant-...\") // Ok(ApiKey)\n"
" api_key(\"\") // Error(EmptyApiKey)\n"
" api_key(\" \") // Error(EmptyApiKey)\n"
" ```\n"
).
-spec api_key(binary()) -> {ok, api_key()} | {error, api_key_error()}.
api_key(Raw) ->
Trimmed = gleam@string:trim(Raw),
case string:length(Trimmed) of
0 ->
{error, empty_api_key};
_ ->
{ok, {api_key, Trimmed}}
end.
-file("src/anthropic/config.gleam", 70).
?DOC(
" Create an ApiKey without validation.\n"
"\n"
" Use this only when you trust the input, such as:\n"
" - Values from environment variables (already validated during load)\n"
" - Values from secure configuration systems\n"
"\n"
" For user input or untrusted sources, use `api_key()` instead.\n"
).
-spec api_key_unchecked(binary()) -> api_key().
api_key_unchecked(Raw) ->
{api_key, Raw}.
-file("src/anthropic/config.gleam", 77).
?DOC(
" Get the raw string value from an ApiKey.\n"
"\n"
" Use this when you need to include the key in HTTP headers.\n"
).
-spec api_key_to_string(api_key()) -> binary().
api_key_to_string(Key) ->
erlang:element(2, Key).
-file("src/anthropic/config.gleam", 82).
?DOC(" Convert an ApiKeyError to a human-readable string.\n").
-spec api_key_error_to_string(api_key_error()) -> binary().
api_key_error_to_string(Error) ->
case Error of
empty_api_key ->
<<"API key cannot be empty"/utf8>>
end.
-file("src/anthropic/config.gleam", 129).
?DOC(" Create empty configuration options\n").
-spec config_options() -> config_options().
config_options() ->
{config_options, none, none, none, none, none}.
-file("src/anthropic/config.gleam", 140).
?DOC(" Set an explicit API key on configuration options\n").
-spec with_api_key(config_options(), binary()) -> config_options().
with_api_key(Options, Api_key) ->
{config_options,
{some, Api_key},
erlang:element(3, Options),
erlang:element(4, Options),
erlang:element(5, Options),
erlang:element(6, Options)}.
-file("src/anthropic/config.gleam", 145).
?DOC(" Set a custom base URL on configuration options\n").
-spec with_base_url(config_options(), binary()) -> config_options().
with_base_url(Options, Base_url) ->
{config_options,
erlang:element(2, Options),
{some, Base_url},
erlang:element(4, Options),
erlang:element(5, Options),
erlang:element(6, Options)}.
-file("src/anthropic/config.gleam", 150).
?DOC(" Set a default model on configuration options\n").
-spec with_default_model(config_options(), binary()) -> config_options().
with_default_model(Options, Default_model) ->
{config_options,
erlang:element(2, Options),
erlang:element(3, Options),
{some, Default_model},
erlang:element(5, Options),
erlang:element(6, Options)}.
-file("src/anthropic/config.gleam", 158).
?DOC(" Set a timeout override on configuration options\n").
-spec with_timeout_ms(config_options(), integer()) -> config_options().
with_timeout_ms(Options, Timeout_ms) ->
{config_options,
erlang:element(2, Options),
erlang:element(3, Options),
erlang:element(4, Options),
{some, Timeout_ms},
erlang:element(6, Options)}.
-file("src/anthropic/config.gleam", 163).
?DOC(" Set a retry override on configuration options\n").
-spec with_max_retries(config_options(), integer()) -> config_options().
with_max_retries(Options, Max_retries) ->
{config_options,
erlang:element(2, Options),
erlang:element(3, Options),
erlang:element(4, Options),
erlang:element(5, Options),
{some, Max_retries}}.
-file("src/anthropic/config.gleam", 220).
-spec normalise_string_option(gleam@option:option(binary())) -> gleam@option:option(binary()).
normalise_string_option(Value) ->
case Value of
{some, Str} ->
Trimmed = gleam@string:trim(Str),
case string:length(Trimmed) of
0 ->
none;
_ ->
{some, Trimmed}
end;
none ->
none
end.
-file("src/anthropic/config.gleam", 239).
-spec getenv(binary(), binary()) -> binary().
getenv(Variable, Default) ->
_pipe = os:getenv(
unicode:characters_to_list(Variable),
unicode:characters_to_list(Default)
),
unicode:characters_to_binary(_pipe).
-file("src/anthropic/config.gleam", 211).
-spec load_env_api_key() -> {ok, api_key()} | {error, nil}.
load_env_api_key() ->
Value = getenv(<<"ANTHROPIC_API_KEY"/utf8>>, <<""/utf8>>),
case normalise_string_option({some, Value}) of
{some, Key} ->
{ok, api_key_unchecked(Key)};
none ->
{error, nil}
end.
-file("src/anthropic/config.gleam", 204).
-spec pick_api_key(gleam@option:option(binary())) -> {ok, api_key()} |
{error, nil}.
pick_api_key(Provided) ->
case normalise_string_option(Provided) of
{some, Key} ->
{ok, api_key_unchecked(Key)};
none ->
load_env_api_key()
end.
-file("src/anthropic/config.gleam", 244).
-spec choose_string(gleam@option:option(binary()), binary()) -> binary().
choose_string(Value, Default) ->
case normalise_string_option(Value) of
{some, Str} ->
Str;
none ->
Default
end.
-file("src/anthropic/config.gleam", 251).
-spec choose_int(gleam@option:option(integer()), integer()) -> integer().
choose_int(Value, Default) ->
case Value of
{some, Num} ->
Num;
none ->
Default
end.
-file("src/anthropic/config.gleam", 179).
?DOC(
" Load configuration using explicit options first, then environment variables.\n"
"\n"
" Sources of configuration (in order of precedence):\n"
" 1. Explicit options passed to the client\n"
" 2. Environment variables (ANTHROPIC_API_KEY)\n"
).
-spec load_config(config_options()) -> {ok, config()} |
{error, anthropic@error:anthropic_error()}.
load_config(Options) ->
Key_result = begin
_pipe = pick_api_key(erlang:element(2, Options)),
gleam@result:map_error(
_pipe,
fun(_) ->
anthropic@error:config_error(
<<"API key is required. Provide ConfigOptions.api_key or set ANTHROPIC_API_KEY."/utf8>>
)
end
)
end,
_pipe@1 = Key_result,
gleam@result:map(
_pipe@1,
fun(Key) ->
{config,
Key,
choose_string(
erlang:element(3, Options),
<<"https://api.anthropic.com"/utf8>>
),
normalise_string_option(erlang:element(4, Options)),
choose_int(erlang:element(5, Options), 60000),
choose_int(erlang:element(6, Options), 3)}
end
).