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
9impl Context69Client {
10    pub async fn list_sources(&self) -> Result<Vec<SourceStatus>, Error> {
11        self.execute_json(self.authorized_request(Method::GET, "/v1/sources").await?)
12            .await
13    }
14
15    pub async fn create_source(&self, request: &SourceConfigInput) -> Result<SourceStatus, Error> {
16        self.execute_json(
17            self.authorized_request(Method::POST, "/v1/sources")
18                .await?
19                .json(request),
20        )
21        .await
22    }
23
24    pub async fn update_source(
25        &self,
26        source_key: &str,
27        request: &SourceConfigInput,
28    ) -> Result<SourceStatus, Error> {
29        let path = format!("/v1/sources/{source_key}");
30        self.execute_json(
31            self.authorized_request(Method::PUT, &path)
32                .await?
33                .json(request),
34        )
35        .await
36    }
37
38    pub async fn delete_source(&self, source_key: &str) -> Result<(), Error> {
39        let path = format!("/v1/sources/{source_key}");
40        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
41            .await
42    }
43
44    pub async fn sync_source(&self, source_key: &str) -> Result<SyncOutcome, Error> {
45        let path = format!("/v1/sources/{source_key}/sync");
46        self.execute_json(self.authorized_request(Method::POST, &path).await?)
47            .await
48    }
49
50    pub async fn list_source_connections(&self) -> Result<Vec<SourceConnectionResponse>, Error> {
51        self.execute_json(
52            self.authorized_request(Method::GET, "/v1/source-connections")
53                .await?,
54        )
55        .await
56    }
57
58    pub async fn create_source_connection(
59        &self,
60        request: &UpsertSourceConnectionRequest,
61    ) -> Result<SourceConnectionResponse, Error> {
62        self.execute_json(
63            self.authorized_request(Method::POST, "/v1/source-connections")
64                .await?
65                .json(request),
66        )
67        .await
68    }
69
70    pub async fn update_source_connection(
71        &self,
72        request: &UpsertSourceConnectionRequest,
73    ) -> Result<SourceConnectionResponse, Error> {
74        self.execute_json(
75            self.authorized_request(Method::PUT, "/v1/source-connections")
76                .await?
77                .json(request),
78        )
79        .await
80    }
81
82    pub async fn delete_source_connection(&self, name: &str) -> Result<(), Error> {
83        let path = format!("/v1/source-connections/{name}");
84        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
85            .await
86    }
87
88    pub async fn list_project_sources(
89        &self,
90        group_key: &str,
91        project_key: &str,
92    ) -> Result<Vec<SourceStatus>, Error> {
93        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources");
94        self.execute_json(self.authorized_request(Method::GET, &path).await?)
95            .await
96    }
97
98    pub async fn create_project_source(
99        &self,
100        group_key: &str,
101        project_key: &str,
102        request: &SourceConfigInput,
103    ) -> Result<SourceStatus, Error> {
104        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources");
105        self.execute_json(
106            self.authorized_request(Method::POST, &path)
107                .await?
108                .json(request),
109        )
110        .await
111    }
112
113    pub async fn update_project_source(
114        &self,
115        group_key: &str,
116        project_key: &str,
117        source_key: &str,
118        request: &SourceConfigInput,
119    ) -> Result<SourceStatus, Error> {
120        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources/{source_key}");
121        self.execute_json(
122            self.authorized_request(Method::PUT, &path)
123                .await?
124                .json(request),
125        )
126        .await
127    }
128
129    pub async fn delete_project_source(
130        &self,
131        group_key: &str,
132        project_key: &str,
133        source_key: &str,
134    ) -> Result<(), Error> {
135        let path = format!("/v1/groups/{group_key}/projects/{project_key}/sources/{source_key}");
136        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
137            .await
138    }
139
140    pub async fn sync_project_source(
141        &self,
142        group_key: &str,
143        project_key: &str,
144        source_key: &str,
145    ) -> Result<SyncOutcome, Error> {
146        let path =
147            format!("/v1/groups/{group_key}/projects/{project_key}/sources/{source_key}/sync");
148        self.execute_json(self.authorized_request(Method::POST, &path).await?)
149            .await
150    }
151}