Skip to main content

mesa_dev/resources/
admin.rs

1//! Admin operations (API key management).
2//!
3//! Access via [`MesaClient::admin`](crate::MesaClient::admin):
4//!
5//! ```rust,no_run
6//! # use mesa_dev::{Mesa, MesaError, models::*};
7//! # async fn run() -> Result<(), MesaError> {
8//! # let client = Mesa::new("key");
9//! let keys = client.admin("my-org").list_api_keys().await?;
10//! # Ok(())
11//! # }
12//! ```
13
14use http::Method;
15
16use crate::client::MesaClient;
17use crate::error::MesaError;
18use crate::http_client::HttpClient;
19use crate::models::{
20    ApiKey, ApiKeyCreated, CreateApiKeyRequest, ListApiKeysResponse, SuccessResponse,
21};
22
23/// Operations on API keys for an organization.
24pub struct AdminResource<'c, C: HttpClient> {
25    client: &'c MesaClient<C>,
26    org: String,
27}
28
29impl<'c, C: HttpClient> AdminResource<'c, C> {
30    pub(crate) fn new(client: &'c MesaClient<C>, org: String) -> Self {
31        Self { client, org }
32    }
33
34    /// Create a new API key.
35    ///
36    /// # Errors
37    ///
38    /// Returns [`MesaError`] if the API request fails.
39    pub async fn create_api_key(
40        &self,
41        req: &CreateApiKeyRequest,
42    ) -> Result<ApiKeyCreated, MesaError> {
43        let path = format!("/{}/api-keys", self.org);
44        self.client
45            .request(Method::POST, &path, &[], Some(req))
46            .await
47    }
48
49    /// List all API keys.
50    ///
51    /// # Errors
52    ///
53    /// Returns [`MesaError`] if the API request fails.
54    pub async fn list_api_keys(&self) -> Result<Vec<ApiKey>, MesaError> {
55        let path = format!("/{}/api-keys", self.org);
56        let resp: ListApiKeysResponse = self
57            .client
58            .request(Method::GET, &path, &[], None::<&()>)
59            .await?;
60        Ok(resp.api_keys)
61    }
62
63    /// Revoke an API key by ID.
64    ///
65    /// # Errors
66    ///
67    /// Returns [`MesaError`] if the API request fails.
68    pub async fn revoke_api_key(&self, id: &str) -> Result<SuccessResponse, MesaError> {
69        let path = format!("/{}/api-keys/{id}", self.org);
70        self.client
71            .request(Method::DELETE, &path, &[], None::<&()>)
72            .await
73    }
74}