kontext_dev_sdk/management/
service_accounts.rs1use std::sync::Arc;
4
5use kontext_dev_sdk_core::types::{
6 CreateServiceAccountRequest, CreateServiceAccountResponse, ListServiceAccountsResponse,
7 PaginationParams, RotateServiceAccountSecretResponse, ServiceAccountResponse,
8};
9
10use crate::http_client::{HttpClient, HttpError, RequestOptions, RequestParams};
11
12pub type ServiceAccountsResource = ServiceAccountsApi;
14
15#[derive(Clone)]
16pub struct ServiceAccountsApi {
17 http: Arc<HttpClient>,
18}
19
20impl ServiceAccountsApi {
21 pub fn new(http: Arc<HttpClient>) -> Self {
22 Self { http }
23 }
24
25 pub async fn create(
26 &self,
27 data: CreateServiceAccountRequest,
28 ) -> Result<CreateServiceAccountResponse, HttpError> {
29 self.http.post("/service-accounts", Some(data), None).await
30 }
31
32 pub async fn list(
33 &self,
34 params: Option<PaginationParams>,
35 ) -> Result<ListServiceAccountsResponse, HttpError> {
36 let options = params.map(|p| {
37 let mut request_params = RequestParams::new();
38 if let Some(limit) = p.limit {
39 request_params.insert("limit".to_string(), limit.to_string());
40 }
41 if let Some(cursor) = p.cursor {
42 request_params.insert("cursor".to_string(), cursor);
43 }
44 RequestOptions {
45 params: Some(request_params),
46 headers: None,
47 }
48 });
49 self.http.get("/service-accounts", options).await
50 }
51
52 pub async fn get(&self, id: &str) -> Result<ServiceAccountResponse, HttpError> {
53 self.http
54 .get(&format!("/service-accounts/{id}"), None)
55 .await
56 }
57
58 pub async fn rotate_secret(
59 &self,
60 id: &str,
61 ) -> Result<RotateServiceAccountSecretResponse, HttpError> {
62 self.http
63 .post::<RotateServiceAccountSecretResponse, ()>(
64 &format!("/service-accounts/{id}/rotate-secret"),
65 None,
66 None,
67 )
68 .await
69 }
70
71 pub async fn revoke(&self, id: &str) -> Result<(), HttpError> {
72 self.http
73 .delete_no_content(&format!("/service-accounts/{id}"), None)
74 .await
75 }
76}