TamaEx (TamaEx v0.1.18)
View SourceDocumentation for TamaEx.
Summary
Functions
Creates a new HTTP client with authentication.
Handles API response and parses data using the provided schema module.
Adds a namespace to the client's base URL.
Validates that the client's base URL namespace is valid for the operation.
Functions
Creates a new HTTP client with authentication.
This function performs OAuth2 client credentials authentication and returns an authenticated HTTP client. The base_url should be the root URL of the API server.
Parameters
- base_url - The root base URL for the API server (e.g., "https://api.example.com")
- client_id - The OAuth2 client ID
- client_secret - The OAuth2 client secret
- options - Optional configuration (e.g., scopes)
Returns
{:ok, %{client: client, expires_in: seconds}}on successful authentication{:error, reason}on authentication failure
Examples
# Create client with root URL
{:ok, %{client: client}} = TamaEx.client("https://api.example.com", "client_id", "client_secret")
# Add namespace for specific operations
namespaced_client = TamaEx.put_namespace(client, "provision")
Handles API response and parses data using the provided schema module.
Parameters
- response - The response from Req.get/post/etc
- schema_module - The module to use for parsing (e.g., TamaEx.Neural.Space)
Examples
iex> defmodule DocSchema do
...> def parse(data), do: %{parsed: data}
...> end
iex> TamaEx.handle_response({:ok, %Req.Response{status: 200, body: %{"data" => %{"id" => "123"}}}}, DocSchema)
{:ok, %{parsed: %{"id" => "123"}}}
iex> TamaEx.handle_response({:ok, %Req.Response{status: 404}}, DocSchema)
{:error, :not_found}
Adds a namespace to the client's base URL.
Parameters
- client - The HTTP client
- namespace - The namespace to append (e.g., "provision", "ingest")
Returns
- Updated client with namespaced base URL
Examples
iex> client = Req.new(base_url: "https://api.example.com")
iex> namespaced_client = TamaEx.put_namespace(client, "provision")
iex> namespaced_client.options[:base_url]
"https://api.example.com/provision"
Validates that the client's base URL namespace is valid for the operation.
Parameters
- client - The HTTP client with base_url
- valid_namespaces - List of valid namespace strings
Examples
iex> client = %Req.Request{options: %{base_url: "https://api.example.com/provision"}}
iex> {:ok, validated_client} = TamaEx.validate_client(client, ["provision"])
iex> validated_client == client
true
iex> client = %Req.Request{options: %{base_url: "https://api.example.com/ingest"}}
iex> try do
...> TamaEx.validate_client(client, ["provision"])
...> rescue
...> ArgumentError -> :error_raised
...> end
:error_raised