Skip to main content

context69_sdk/client/
sources.rs

1use context69_contracts::{
2    SourceConfigInput, SourceConnectionResponse, SourceStatus, SyncOutcome,
3    UpsertSourceConnectionRequest,
4};
5use reqwest::Method;
6
7use crate::{Context69Client, Error};
8
9pub struct SourcesApi<'a> {
10    client: &'a Context69Client,
11}
12
13impl<'a> SourcesApi<'a> {
14    pub(crate) fn new(client: &'a Context69Client) -> Self {
15        Self { client }
16    }
17
18    pub async fn list_sources(&self) -> Result<Vec<SourceStatus>, Error> {
19        self.client
20            .execute_json(
21                self.client
22                    .authorized_request(Method::GET, "/v1/sources")
23                    .await?,
24            )
25            .await
26    }
27
28    pub async fn create_source(&self, request: &SourceConfigInput) -> Result<SourceStatus, Error> {
29        self.client
30            .execute_json(
31                self.client
32                    .authorized_request(Method::POST, "/v1/sources")
33                    .await?
34                    .json(request),
35            )
36            .await
37    }
38
39    pub async fn update_source(
40        &self,
41        source_key: &str,
42        request: &SourceConfigInput,
43    ) -> Result<SourceStatus, Error> {
44        let path = format!("/v1/sources/{source_key}");
45        self.client
46            .execute_json(
47                self.client
48                    .authorized_request(Method::PUT, &path)
49                    .await?
50                    .json(request),
51            )
52            .await
53    }
54
55    pub async fn delete_source(&self, source_key: &str) -> Result<(), Error> {
56        let path = format!("/v1/sources/{source_key}");
57        self.client
58            .execute_empty(
59                self.client
60                    .authorized_request(Method::DELETE, &path)
61                    .await?,
62            )
63            .await
64    }
65
66    pub async fn sync_source(&self, source_key: &str) -> Result<SyncOutcome, Error> {
67        let path = format!("/v1/sources/{source_key}/sync");
68        self.client
69            .execute_json(self.client.authorized_request(Method::POST, &path).await?)
70            .await
71    }
72
73    pub async fn list_source_connections(&self) -> Result<Vec<SourceConnectionResponse>, Error> {
74        self.client
75            .execute_json(
76                self.client
77                    .authorized_request(Method::GET, "/v1/source-connections")
78                    .await?,
79            )
80            .await
81    }
82
83    pub async fn create_source_connection(
84        &self,
85        request: &UpsertSourceConnectionRequest,
86    ) -> Result<SourceConnectionResponse, Error> {
87        self.client
88            .execute_json(
89                self.client
90                    .authorized_request(Method::POST, "/v1/source-connections")
91                    .await?
92                    .json(request),
93            )
94            .await
95    }
96
97    pub async fn update_source_connection(
98        &self,
99        request: &UpsertSourceConnectionRequest,
100    ) -> Result<SourceConnectionResponse, Error> {
101        self.client
102            .execute_json(
103                self.client
104                    .authorized_request(Method::PUT, "/v1/source-connections")
105                    .await?
106                    .json(request),
107            )
108            .await
109    }
110
111    pub async fn delete_source_connection(&self, name: &str) -> Result<(), Error> {
112        let path = format!("/v1/source-connections/{name}");
113        self.client
114            .execute_empty(
115                self.client
116                    .authorized_request(Method::DELETE, &path)
117                    .await?,
118            )
119            .await
120    }
121
122    pub async fn list_project_sources(
123        &self,
124        group_key: &str,
125        project_key: &str,
126    ) -> Result<Vec<SourceStatus>, Error> {
127        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources");
128        self.client
129            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
130            .await
131    }
132
133    pub async fn create_project_source(
134        &self,
135        group_key: &str,
136        project_key: &str,
137        request: &SourceConfigInput,
138    ) -> Result<SourceStatus, Error> {
139        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources");
140        self.client
141            .execute_json(
142                self.client
143                    .authorized_request(Method::POST, &path)
144                    .await?
145                    .json(request),
146            )
147            .await
148    }
149
150    pub async fn update_project_source(
151        &self,
152        group_key: &str,
153        project_key: &str,
154        source_key: &str,
155        request: &SourceConfigInput,
156    ) -> Result<SourceStatus, Error> {
157        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources/{source_key}");
158        self.client
159            .execute_json(
160                self.client
161                    .authorized_request(Method::PUT, &path)
162                    .await?
163                    .json(request),
164            )
165            .await
166    }
167
168    pub async fn delete_project_source(
169        &self,
170        group_key: &str,
171        project_key: &str,
172        source_key: &str,
173    ) -> Result<(), Error> {
174        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources/{source_key}");
175        self.client
176            .execute_empty(
177                self.client
178                    .authorized_request(Method::DELETE, &path)
179                    .await?,
180            )
181            .await
182    }
183
184    pub async fn sync_project_source(
185        &self,
186        group_key: &str,
187        project_key: &str,
188        source_key: &str,
189    ) -> Result<SyncOutcome, Error> {
190        let path =
191            format!("/v1/groups/{group_key}/projects/{project_key}/sources/{source_key}/sync");
192        self.client
193            .execute_json(self.client.authorized_request(Method::POST, &path).await?)
194            .await
195    }
196}