Skip to main content

context69_sdk/client/settings/
providers.rs

1use context69_contracts::{ProviderAccountResponse, UpsertProviderAccountRequest};
2use reqwest::Method;
3
4use super::Context69Client;
5use crate::{Error, client::transport::encode_path_component};
6
7pub struct ProviderAccountsApi<'a> {
8    client: &'a Context69Client,
9}
10
11impl<'a> ProviderAccountsApi<'a> {
12    pub(super) fn new(client: &'a Context69Client) -> Self {
13        Self { client }
14    }
15
16    pub async fn list(&self) -> Result<Vec<ProviderAccountResponse>, Error> {
17        self.client
18            .execute_json(
19                self.client
20                    .authorized_request(Method::GET, "/v1/settings/provider-accounts")
21                    .await?,
22            )
23            .await
24    }
25
26    pub async fn create(
27        &self,
28        request: &UpsertProviderAccountRequest,
29    ) -> Result<ProviderAccountResponse, Error> {
30        self.save(Method::POST, request).await
31    }
32
33    pub async fn update(
34        &self,
35        request: &UpsertProviderAccountRequest,
36    ) -> Result<ProviderAccountResponse, Error> {
37        self.save(Method::PUT, request).await
38    }
39
40    async fn save(
41        &self,
42        method: Method,
43        request: &UpsertProviderAccountRequest,
44    ) -> Result<ProviderAccountResponse, Error> {
45        self.client
46            .execute_json(
47                self.client
48                    .authorized_request(method, "/v1/settings/provider-accounts")
49                    .await?
50                    .json(request),
51            )
52            .await
53    }
54}
55
56pub struct ProviderAccountApi<'a> {
57    client: &'a Context69Client,
58    account_key: String,
59}
60
61impl<'a> ProviderAccountApi<'a> {
62    pub(super) fn new(client: &'a Context69Client, account_key: String) -> Self {
63        Self {
64            client,
65            account_key,
66        }
67    }
68
69    pub async fn delete(&self) -> Result<(), Error> {
70        let path = format!(
71            "/v1/settings/provider-accounts/{}",
72            encode_path_component(&self.account_key)
73        );
74        self.client
75            .execute_empty(
76                self.client
77                    .authorized_request(Method::DELETE, &path)
78                    .await?,
79            )
80            .await
81    }
82}