objectiveai_sdk/auth/http.rs
1//! HTTP client functions for authentication endpoints.
2//!
3//! This module provides async functions for interacting with the ObjectiveAI
4//! authentication API, including API key management, credit queries, and
5//! OpenRouter BYOK configuration.
6//!
7//! All functions require an [`HttpClient`] instance configured with valid
8//! authentication credentials.
9
10use crate::{HttpClient, HttpError};
11
12/// Creates a new API key.
13///
14/// # Arguments
15///
16/// * `client` - The HTTP client to use for the request.
17/// * `request` - The request containing the key name, description, and expiration.
18///
19/// # Returns
20///
21/// The newly created API key with its metadata.
22///
23/// # Errors
24///
25/// Returns an [`HttpError`] if the request fails or the server returns an error.
26pub async fn create_api_key(
27 client: &HttpClient,
28 request: super::request::CreateApiKeyRequest,
29) -> Result<super::response::CreateApiKeyResponse, HttpError> {
30 client
31 .send_unary(reqwest::Method::POST, "auth/keys", Some(request))
32 .await
33}
34
35/// Creates or updates the OpenRouter BYOK API key.
36///
37/// This associates an OpenRouter API key with the user's account, allowing
38/// requests to be routed through OpenRouter's model marketplace.
39///
40/// # Arguments
41///
42/// * `client` - The HTTP client to use for the request.
43/// * `request` - The request containing the OpenRouter API key.
44///
45/// # Returns
46///
47/// The configured OpenRouter API key.
48///
49/// # Errors
50///
51/// Returns an [`HttpError`] if the request fails or the server returns an error.
52pub async fn create_openrouter_byok_api_key(
53 client: &HttpClient,
54 request: super::request::CreateOpenRouterByokApiKeyRequest,
55) -> Result<super::response::CreateOpenRouterByokApiKeyResponse, HttpError> {
56 client
57 .send_unary(
58 reqwest::Method::POST,
59 "auth/keys/openrouter",
60 Some(request),
61 )
62 .await
63}
64
65/// Disables an existing API key.
66///
67/// Once disabled, the API key can no longer be used for authentication.
68/// The key's metadata is preserved with a `disabled` timestamp.
69///
70/// # Arguments
71///
72/// * `client` - The HTTP client to use for the request.
73/// * `request` - The request containing the API key to disable.
74///
75/// # Returns
76///
77/// The disabled API key with updated metadata.
78///
79/// # Errors
80///
81/// Returns an [`HttpError`] if the request fails or the server returns an error.
82pub async fn disable_api_key(
83 client: &HttpClient,
84 request: super::request::DisableApiKeyRequest,
85) -> Result<super::response::DisableApiKeyResponse, HttpError> {
86 client
87 .send_unary(reqwest::Method::DELETE, "auth/keys", Some(request))
88 .await
89}
90
91/// Deletes the OpenRouter BYOK API key.
92///
93/// Removes the OpenRouter API key association from the user's account.
94///
95/// # Arguments
96///
97/// * `client` - The HTTP client to use for the request.
98///
99/// # Errors
100///
101/// Returns an [`HttpError`] if the request fails or the server returns an error.
102pub async fn delete_openrouter_byok_api_key(
103 client: &HttpClient,
104) -> Result<(), HttpError> {
105 client
106 .send_unary_no_response(
107 reqwest::Method::DELETE,
108 "auth/keys/openrouter",
109 None::<String>,
110 )
111 .await
112}
113
114/// Lists all API keys for the authenticated user.
115///
116/// # Arguments
117///
118/// * `client` - The HTTP client to use for the request.
119///
120/// # Returns
121///
122/// A list of all API keys with their metadata and accumulated costs.
123///
124/// # Errors
125///
126/// Returns an [`HttpError`] if the request fails or the server returns an error.
127pub async fn list_api_keys(
128 client: &HttpClient,
129) -> Result<super::response::ListApiKeyResponse, HttpError> {
130 client
131 .send_unary(reqwest::Method::GET, "auth/keys", None::<String>)
132 .await
133}
134
135/// Retrieves the configured OpenRouter BYOK API key.
136///
137/// # Arguments
138///
139/// * `client` - The HTTP client to use for the request.
140///
141/// # Returns
142///
143/// The OpenRouter API key if configured, or `None` if not set.
144///
145/// # Errors
146///
147/// Returns an [`HttpError`] if the request fails or the server returns an error.
148pub async fn get_openrouter_byok_api_key(
149 client: &HttpClient,
150) -> Result<super::response::GetOpenRouterByokApiKeyResponse, HttpError> {
151 client
152 .send_unary(
153 reqwest::Method::GET,
154 "auth/keys/openrouter",
155 None::<String>,
156 )
157 .await
158}
159
160/// Retrieves the user's credit balance.
161///
162/// Returns the current balance, total purchased, and total used credits.
163///
164/// # Arguments
165///
166/// * `client` - The HTTP client to use for the request.
167///
168/// # Returns
169///
170/// The user's complete credit information.
171///
172/// # Errors
173///
174/// Returns an [`HttpError`] if the request fails or the server returns an error.
175pub async fn get_credits(
176 client: &HttpClient,
177) -> Result<super::response::GetCreditsResponse, HttpError> {
178 client
179 .send_unary(reqwest::Method::GET, "auth/credits", None::<String>)
180 .await
181}