Skip to main content

openapp_sdk_core/resources/
api_keys.rs

1//! `API Keys` resource group.
2
3use std::sync::Arc;
4
5use reqwest::Method;
6
7use super::types;
8use crate::{
9    error::SdkError,
10    transport::{RequestSpec, Transport},
11};
12
13#[derive(Debug, Clone)]
14pub struct ApiKeysClient {
15    transport: Arc<Transport>,
16}
17
18impl ApiKeysClient {
19    pub(crate) fn new(transport: Arc<Transport>) -> Self {
20        Self { transport }
21    }
22
23    /// `GET /api-keys` — list API keys for the authenticated user's org.
24    pub async fn list(&self) -> Result<Vec<types::ApiKeyListItem>, SdkError> {
25        self.transport
26            .request_json::<(), Vec<types::ApiKeyListItem>>(RequestSpec {
27                method: Method::GET,
28                path: "/api-keys",
29                ..Default::default()
30            })
31            .await
32    }
33
34    /// `POST /api-keys` — mint a new API key.
35    pub async fn create(
36        &self,
37        body: &types::CreateApiKeyRequest,
38    ) -> Result<types::CreateApiKeyResponse, SdkError> {
39        self.transport
40            .request_json::<types::CreateApiKeyRequest, types::CreateApiKeyResponse>(RequestSpec {
41                method: Method::POST,
42                path: "/api-keys",
43                body: Some(body),
44                ..Default::default()
45            })
46            .await
47    }
48
49    /// `PATCH /api-keys/{id}` — rename or adjust scopes.
50    pub async fn update(
51        &self,
52        id: &str,
53        body: &types::UpdateApiKeyRequest,
54    ) -> Result<(), SdkError> {
55        let path = format!("/api-keys/{id}");
56        self.transport
57            .request_json::<types::UpdateApiKeyRequest, ()>(RequestSpec {
58                method: Method::PATCH,
59                path: &path,
60                body: Some(body),
61                ..Default::default()
62            })
63            .await
64    }
65
66    /// `DELETE /api-keys/{id}` — soft-revoke the key.
67    pub async fn revoke(&self, id: &str) -> Result<(), SdkError> {
68        let path = format!("/api-keys/{id}");
69        self.transport
70            .request_json::<(), ()>(RequestSpec {
71                method: Method::DELETE,
72                path: &path,
73                ..Default::default()
74            })
75            .await
76    }
77
78    /// `POST /api-keys/{id}/restore` — un-revoke a previously revoked key.
79    pub async fn restore(&self, id: &str) -> Result<(), SdkError> {
80        let path = format!("/api-keys/{id}/restore");
81        self.transport
82            .request_json::<(), ()>(RequestSpec {
83                method: Method::POST,
84                path: &path,
85                ..Default::default()
86            })
87            .await
88    }
89
90    /// `DELETE /api-keys/{id}/purge` — hard-delete a revoked key.
91    pub async fn purge(&self, id: &str) -> Result<(), SdkError> {
92        let path = format!("/api-keys/{id}/purge");
93        self.transport
94            .request_json::<(), ()>(RequestSpec {
95                method: Method::DELETE,
96                path: &path,
97                ..Default::default()
98            })
99            .await
100    }
101}