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::JsonValue;
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<JsonValue>, SdkError> {
25        self.transport
26            .request_json::<(), Vec<JsonValue>>(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(&self, body: &JsonValue) -> Result<JsonValue, SdkError> {
36        self.transport
37            .request_json::<JsonValue, JsonValue>(RequestSpec {
38                method: Method::POST,
39                path: "/api-keys",
40                body: Some(body),
41                ..Default::default()
42            })
43            .await
44    }
45
46    /// `PATCH /api-keys/{id}` — rename or adjust scopes.
47    pub async fn update(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
48        let path = format!("/api-keys/{id}");
49        self.transport
50            .request_json::<JsonValue, JsonValue>(RequestSpec {
51                method: Method::PATCH,
52                path: &path,
53                body: Some(body),
54                ..Default::default()
55            })
56            .await
57    }
58
59    /// `DELETE /api-keys/{id}` — soft-revoke the key.
60    pub async fn revoke(&self, id: &str) -> Result<(), SdkError> {
61        let path = format!("/api-keys/{id}");
62        self.transport
63            .request_json::<(), ()>(RequestSpec {
64                method: Method::DELETE,
65                path: &path,
66                ..Default::default()
67            })
68            .await
69    }
70
71    /// `POST /api-keys/{id}/restore` — un-revoke a previously revoked key.
72    pub async fn restore(&self, id: &str) -> Result<JsonValue, SdkError> {
73        let path = format!("/api-keys/{id}/restore");
74        self.transport
75            .request_json::<(), JsonValue>(RequestSpec {
76                method: Method::POST,
77                path: &path,
78                ..Default::default()
79            })
80            .await
81    }
82
83    /// `DELETE /api-keys/{id}/purge` — hard-delete a revoked key.
84    pub async fn purge(&self, id: &str) -> Result<(), SdkError> {
85        let path = format!("/api-keys/{id}/purge");
86        self.transport
87            .request_json::<(), ()>(RequestSpec {
88                method: Method::DELETE,
89                path: &path,
90                ..Default::default()
91            })
92            .await
93    }
94}