Gemini.Auth (GeminiEx v0.9.0)

Copy Markdown View Source

Authentication strategy behavior and implementations for Gemini and Vertex AI.

This module provides a unified interface for different authentication methods:

  • Gemini API: Simple API key authentication
  • Vertex AI: OAuth2/Service Account authentication

Summary

Functions

Build authenticated headers for the given strategy and credentials.

Build the full path for an API endpoint.

Get the base URL for the given strategy and credentials.

Get the appropriate authentication strategy based on configuration.

Refresh credentials if needed (mainly for Vertex AI OAuth tokens).

Types

auth_type()

@type auth_type() :: :gemini | :vertex_ai

credentials()

@type credentials() ::
  %{api_key: String.t()}
  | %{access_token: String.t(), project_id: String.t(), location: String.t()}

Functions

build_headers(auth_type, credentials)

@spec build_headers(auth_type(), map()) ::
  {:ok, [{String.t(), String.t()}]} | {:error, term()}

Build authenticated headers for the given strategy and credentials.

Returns {:ok, headers} on success, or {:error, reason} if authentication fails (e.g., service account token generation failure).

build_path(auth_type, model, endpoint, credentials)

@spec build_path(auth_type(), String.t(), String.t(), map()) :: String.t()

Build the full path for an API endpoint.

get_base_url(auth_type, credentials)

@spec get_base_url(auth_type(), map()) :: String.t() | {:error, term()}

Get the base URL for the given strategy and credentials.

get_strategy(auth_type)

@spec get_strategy(auth_type()) :: module()

Get the appropriate authentication strategy based on configuration.

refresh_credentials(auth_type, credentials)

@spec refresh_credentials(auth_type(), map()) :: {:ok, map()} | {:error, term()}

Refresh credentials if needed (mainly for Vertex AI OAuth tokens).