Skip to main content

context69_sdk/client/sources/
mod.rs

1use context69_contracts::{
2    SourceConfigInput, SourceConnectionResponse, SourceStatus, SyncOutcome,
3    UpsertSourceConnectionRequest,
4};
5use reqwest::Method;
6
7use super::Context69Client;
8use crate::{Error, client::transport::encode_path_component};
9
10pub struct SourcesApi<'a> {
11    client: &'a Context69Client,
12}
13
14impl<'a> SourcesApi<'a> {
15    pub(crate) fn new(client: &'a Context69Client) -> Self {
16        Self { client }
17    }
18
19    pub async fn list(&self) -> Result<Vec<SourceStatus>, Error> {
20        self.client
21            .execute_json(
22                self.client
23                    .authorized_request(Method::GET, "/v1/sources")
24                    .await?,
25            )
26            .await
27    }
28
29    pub async fn create(&self, request: &SourceConfigInput) -> Result<SourceStatus, Error> {
30        self.client
31            .execute_json(
32                self.client
33                    .authorized_request(Method::POST, "/v1/sources")
34                    .await?
35                    .json(request),
36            )
37            .await
38    }
39}
40
41pub struct SourceApi<'a> {
42    client: &'a Context69Client,
43    source_key: String,
44}
45
46impl<'a> SourceApi<'a> {
47    pub(crate) fn new(client: &'a Context69Client, source_key: String) -> Self {
48        Self { client, source_key }
49    }
50
51    fn path(&self, suffix: &str) -> String {
52        format!(
53            "/v1/sources/{}{}",
54            encode_path_component(&self.source_key),
55            suffix
56        )
57    }
58
59    pub async fn update(&self, request: &SourceConfigInput) -> Result<SourceStatus, Error> {
60        let path = self.path("");
61        self.client
62            .execute_json(
63                self.client
64                    .authorized_request(Method::PUT, &path)
65                    .await?
66                    .json(request),
67            )
68            .await
69    }
70
71    pub async fn sync(&self) -> Result<SyncOutcome, Error> {
72        let path = self.path("/sync");
73        self.client
74            .execute_json(self.client.authorized_request(Method::POST, &path).await?)
75            .await
76    }
77
78    pub async fn delete(&self) -> Result<(), Error> {
79        let path = self.path("");
80        self.client
81            .execute_empty(
82                self.client
83                    .authorized_request(Method::DELETE, &path)
84                    .await?,
85            )
86            .await
87    }
88}
89
90pub struct SourceConnectionsApi<'a> {
91    client: &'a Context69Client,
92}
93
94impl<'a> SourceConnectionsApi<'a> {
95    pub(crate) fn new(client: &'a Context69Client) -> Self {
96        Self { client }
97    }
98
99    pub async fn list(&self) -> Result<Vec<SourceConnectionResponse>, Error> {
100        self.client
101            .execute_json(
102                self.client
103                    .authorized_request(Method::GET, "/v1/source-connections")
104                    .await?,
105            )
106            .await
107    }
108
109    pub async fn create(
110        &self,
111        request: &UpsertSourceConnectionRequest,
112    ) -> Result<SourceConnectionResponse, Error> {
113        self.save(Method::POST, request).await
114    }
115
116    pub async fn update(
117        &self,
118        request: &UpsertSourceConnectionRequest,
119    ) -> Result<SourceConnectionResponse, Error> {
120        self.save(Method::PUT, request).await
121    }
122
123    async fn save(
124        &self,
125        method: Method,
126        request: &UpsertSourceConnectionRequest,
127    ) -> Result<SourceConnectionResponse, Error> {
128        self.client
129            .execute_json(
130                self.client
131                    .authorized_request(method, "/v1/source-connections")
132                    .await?
133                    .json(request),
134            )
135            .await
136    }
137}
138
139pub struct SourceConnectionApi<'a> {
140    client: &'a Context69Client,
141    name: String,
142}
143
144impl<'a> SourceConnectionApi<'a> {
145    pub(crate) fn new(client: &'a Context69Client, name: String) -> Self {
146        Self { client, name }
147    }
148
149    pub async fn delete(&self) -> Result<(), Error> {
150        let path = format!(
151            "/v1/source-connections/{}",
152            encode_path_component(&self.name)
153        );
154        self.client
155            .execute_empty(
156                self.client
157                    .authorized_request(Method::DELETE, &path)
158                    .await?,
159            )
160            .await
161    }
162}