Packages

A well-typed, idiomatic Gleam client for Anthropic's Claude API with streaming support and tool use

Current section

Files

Jump to
anthropic_gleam src anthropic@config.erl
Raw

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([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([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 config() :: {config,
binary(),
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", 66).
?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", 77).
?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", 82).
?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", 87).
?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", 95).
?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", 100).
?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", 157).
-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", 176).
-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", 148).
-spec load_env_api_key() -> {ok, binary()} | {error, nil}.
load_env_api_key() ->
Value = getenv(<<"ANTHROPIC_API_KEY"/utf8>>, <<""/utf8>>),
case normalise_string_option({some, Value}) of
{some, Key} ->
{ok, Key};
none ->
{error, nil}
end.
-file("src/anthropic/config.gleam", 141).
-spec pick_api_key(gleam@option:option(binary())) -> {ok, binary()} |
{error, nil}.
pick_api_key(Provided) ->
case normalise_string_option(Provided) of
{some, Key} ->
{ok, Key};
none ->
load_env_api_key()
end.
-file("src/anthropic/config.gleam", 181).
-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", 188).
-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", 116).
?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@types@error:anthropic_error()}.
load_config(Options) ->
Api_key = begin
_pipe = pick_api_key(erlang:element(2, Options)),
gleam@result:map_error(
_pipe,
fun(_) ->
anthropic@types@error:config_error(
<<"API key is required. Provide ConfigOptions.api_key or set ANTHROPIC_API_KEY."/utf8>>
)
end
)
end,
_pipe@1 = Api_key,
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
).