Skip to main content

kontext_dev_sdk/management/
applications.rs

1//! Applications management resource.
2
3use std::sync::Arc;
4
5use kontext_dev_sdk_core::types::{
6    ApplicationIntegrationsResponse, ApplicationOAuthResponse, ApplicationResponse,
7    CreateApplicationRequest, CreateApplicationResponse, ListApplicationsResponse,
8    PaginationParams, RotateApplicationSecretResponse, UpdateApplicationOAuthRequest,
9    UpdateApplicationRequest,
10};
11
12use crate::http_client::{HttpClient, HttpError, RequestOptions, RequestParams};
13
14/// TS-compatible name.
15pub type ApplicationsResource = ApplicationsApi;
16
17#[derive(Clone)]
18pub struct ApplicationsApi {
19    http: Arc<HttpClient>,
20}
21
22impl ApplicationsApi {
23    pub fn new(http: Arc<HttpClient>) -> Self {
24        Self { http }
25    }
26
27    pub async fn create(
28        &self,
29        data: CreateApplicationRequest,
30    ) -> Result<CreateApplicationResponse, HttpError> {
31        self.http.post("/applications", Some(data), None).await
32    }
33
34    pub async fn list(
35        &self,
36        params: Option<PaginationParams>,
37    ) -> Result<ListApplicationsResponse, HttpError> {
38        let options = params.map(|p| {
39            let mut request_params = RequestParams::new();
40            if let Some(limit) = p.limit {
41                request_params.insert("limit".to_string(), limit.to_string());
42            }
43            if let Some(cursor) = p.cursor {
44                request_params.insert("cursor".to_string(), cursor);
45            }
46            RequestOptions {
47                params: Some(request_params),
48                headers: None,
49            }
50        });
51        self.http.get("/applications", options).await
52    }
53
54    pub async fn get(&self, id: &str) -> Result<ApplicationResponse, HttpError> {
55        self.http.get(&format!("/applications/{id}"), None).await
56    }
57
58    pub async fn update(
59        &self,
60        id: &str,
61        data: UpdateApplicationRequest,
62    ) -> Result<ApplicationResponse, HttpError> {
63        self.http
64            .patch(&format!("/applications/{id}"), Some(data), None)
65            .await
66    }
67
68    pub async fn archive(&self, id: &str) -> Result<(), HttpError> {
69        self.http
70            .delete_no_content(&format!("/applications/{id}"), None)
71            .await
72    }
73
74    pub async fn get_oauth(&self, id: &str) -> Result<ApplicationOAuthResponse, HttpError> {
75        self.http
76            .get(&format!("/applications/{id}/oauth"), None)
77            .await
78    }
79
80    pub async fn update_oauth(
81        &self,
82        id: &str,
83        data: UpdateApplicationOAuthRequest,
84    ) -> Result<ApplicationOAuthResponse, HttpError> {
85        self.http
86            .patch(&format!("/applications/{id}/oauth"), Some(data), None)
87            .await
88    }
89
90    pub async fn rotate_secret(
91        &self,
92        id: &str,
93    ) -> Result<RotateApplicationSecretResponse, HttpError> {
94        self.http
95            .post::<RotateApplicationSecretResponse, ()>(
96                &format!("/applications/{id}/rotate-secret"),
97                None,
98                None,
99            )
100            .await
101    }
102
103    pub async fn list_integrations(
104        &self,
105        id: &str,
106    ) -> Result<ApplicationIntegrationsResponse, HttpError> {
107        self.http
108            .get(&format!("/applications/{id}/integrations"), None)
109            .await
110    }
111
112    pub async fn replace_integrations(
113        &self,
114        id: &str,
115        integration_ids: Vec<String>,
116    ) -> Result<ApplicationIntegrationsResponse, HttpError> {
117        #[derive(serde::Serialize)]
118        #[serde(rename_all = "camelCase")]
119        struct Body {
120            integration_ids: Vec<String>,
121        }
122
123        self.http
124            .put(
125                &format!("/applications/{id}/integrations"),
126                Some(Body { integration_ids }),
127                None,
128            )
129            .await
130    }
131
132    pub async fn attach_integration(
133        &self,
134        id: &str,
135        integration_id: &str,
136    ) -> Result<(), HttpError> {
137        self.http
138            .post_no_content::<()>(
139                &format!("/applications/{id}/integrations/{integration_id}"),
140                None,
141                None,
142            )
143            .await
144    }
145
146    pub async fn detach_integration(
147        &self,
148        id: &str,
149        integration_id: &str,
150    ) -> Result<(), HttpError> {
151        self.http
152            .delete_no_content(
153                &format!("/applications/{id}/integrations/{integration_id}"),
154                None,
155            )
156            .await
157    }
158}