use crate::client::Client;
use crate::common::EnvironmentalImpacts;
use crate::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PermissionType {
Admin,
CreatePublicCollection,
ReadMetric,
ProvideModels,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum LimitType {
Tpm,
Tpd,
Rpm,
Rpd,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Limit {
pub router_id: i64,
#[serde(rename = "type")]
pub kind: LimitType,
#[serde(default)]
pub value: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UserInfo {
pub id: i64,
pub email: String,
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub organization: Option<i64>,
#[serde(default)]
pub budget: Option<f64>,
#[serde(default)]
pub permissions: Vec<PermissionType>,
#[serde(default)]
pub limits: Vec<Limit>,
#[serde(default)]
pub expires: Option<i64>,
#[serde(default)]
pub priority: i64,
pub created: i64,
pub updated: i64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UpdateUserInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub current_password: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub password: Option<String>,
}
impl UpdateUserInfo {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
#[must_use]
pub fn email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
#[must_use]
pub fn password(mut self, current: impl Into<String>, new: impl Into<String>) -> Self {
self.current_password = Some(current.into());
self.password = Some(new.into());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateKey {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<i64>,
}
impl CreateKey {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
expires: None,
}
}
#[must_use]
pub fn expires(mut self, expires: i64) -> Self {
self.expires = Some(expires);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CreateKeyResponse {
pub id: i64,
pub token: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Key {
pub id: i64,
pub name: String,
pub token: String,
#[serde(default)]
pub expires: Option<i64>,
pub created: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Keys {
pub data: Vec<Key>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MetricsUsage {
#[serde(default)]
pub latency: Option<i64>,
#[serde(default)]
pub ttft: Option<i64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UsageDetail {
#[serde(default)]
pub prompt_tokens: Option<i64>,
#[serde(default)]
pub completion_tokens: Option<i64>,
#[serde(default)]
pub total_tokens: Option<i64>,
#[serde(default)]
pub cost: Option<f64>,
#[serde(default)]
pub impacts: EnvironmentalImpacts,
#[serde(default)]
pub metrics: MetricsUsage,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UsageRecord {
#[serde(default)]
pub model: Option<String>,
#[serde(default)]
pub key: Option<String>,
#[serde(default)]
pub endpoint: Option<String>,
#[serde(default)]
pub method: Option<String>,
#[serde(default)]
pub status: Option<i64>,
#[serde(default)]
pub usage: UsageDetail,
pub created: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Usages {
pub data: Vec<UsageRecord>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum EndpointUsage {
#[serde(rename = "/v1/audio/transcriptions")]
AudioTranscriptions,
#[serde(rename = "/v1/chat/completions")]
ChatCompletions,
#[serde(rename = "/v1/embeddings")]
Embeddings,
#[serde(rename = "/v1/ocr")]
Ocr,
#[serde(rename = "/v1/rerank")]
Rerank,
#[serde(rename = "/v1/search")]
Search,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct KeysQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub order_by: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub order_direction: Option<String>,
}
impl KeysQuery {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn offset(mut self, offset: u32) -> Self {
self.offset = Some(offset);
self
}
#[must_use]
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
#[must_use]
pub fn order(mut self, by: impl Into<String>, direction: impl Into<String>) -> Self {
self.order_by = Some(by.into());
self.order_direction = Some(direction.into());
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UsageQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub start_time: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub end_time: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub endpoint: Option<EndpointUsage>,
}
impl UsageQuery {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn offset(mut self, offset: u32) -> Self {
self.offset = Some(offset);
self
}
#[must_use]
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
#[must_use]
pub fn time_range(mut self, start: i64, end: i64) -> Self {
self.start_time = Some(start);
self.end_time = Some(end);
self
}
#[must_use]
pub fn endpoint(mut self, endpoint: EndpointUsage) -> Self {
self.endpoint = Some(endpoint);
self
}
}
impl Client {
pub async fn me_info(&self) -> Result<UserInfo, Error> {
let url = self.endpoint("/v1/me/info")?;
self.get_json(url).await
}
pub async fn update_me_info(&self, update: &UpdateUserInfo) -> Result<(), Error> {
let url = self.endpoint("/v1/me/info")?;
self.patch_no_content(url, update).await
}
pub async fn create_key(&self, key: &CreateKey) -> Result<CreateKeyResponse, Error> {
let url = self.endpoint("/v1/me/keys")?;
self.post_json(url, key).await
}
pub async fn keys(&self, query: &KeysQuery) -> Result<Keys, Error> {
let url = self.endpoint("/v1/me/keys")?;
self.get_json_with_query(url, query).await
}
pub async fn key(&self, key_id: impl std::fmt::Display) -> Result<Key, Error> {
let url = self.endpoint_with_segment("/v1/me/keys/", &key_id.to_string())?;
self.get_json(url).await
}
pub async fn delete_key(&self, key_id: impl std::fmt::Display) -> Result<(), Error> {
let url = self.endpoint_with_segment("/v1/me/keys/", &key_id.to_string())?;
self.delete_no_content(url).await
}
pub async fn usage(&self, query: &UsageQuery) -> Result<Usages, Error> {
let url = self.endpoint("/v1/me/usage")?;
self.get_json_with_query(url, query).await
}
}