Skip to main content

context69_sdk/client/library/
texts.rs

1use context69_contracts::{CreateTextRequest, LibraryUploadResponse, UpsertLibraryTextRequest};
2use reqwest::Method;
3
4use super::Context69Client;
5use crate::Error;
6
7pub struct LibraryTextsApi<'a> {
8    client: &'a Context69Client,
9    base_path: String,
10}
11
12impl<'a> LibraryTextsApi<'a> {
13    pub(super) fn new(client: &'a Context69Client, base_path: String) -> Self {
14        Self { client, base_path }
15    }
16
17    pub async fn create(
18        &self,
19        request: &CreateTextRequest,
20    ) -> Result<LibraryUploadResponse, Error> {
21        create_text(self.client, &self.base_path, request).await
22    }
23}
24
25pub struct GroupLibraryTextsApi<'a> {
26    client: &'a Context69Client,
27    base_path: String,
28}
29
30impl<'a> GroupLibraryTextsApi<'a> {
31    pub(super) fn new(client: &'a Context69Client, base_path: String) -> Self {
32        Self { client, base_path }
33    }
34
35    pub async fn create(
36        &self,
37        request: &CreateTextRequest,
38    ) -> Result<LibraryUploadResponse, Error> {
39        create_text(self.client, &self.base_path, request).await
40    }
41
42    pub async fn upsert(
43        &self,
44        request: &UpsertLibraryTextRequest,
45    ) -> Result<LibraryUploadResponse, Error> {
46        let path = format!("{}/texts", self.base_path);
47        self.client
48            .execute_json(
49                self.client
50                    .authorized_request(Method::PUT, &path)
51                    .await?
52                    .json(request),
53            )
54            .await
55    }
56}
57
58async fn create_text(
59    client: &Context69Client,
60    base_path: &str,
61    request: &CreateTextRequest,
62) -> Result<LibraryUploadResponse, Error> {
63    let path = format!("{base_path}/texts");
64    client
65        .execute_json(
66            client
67                .authorized_request(Method::POST, &path)
68                .await?
69                .json(request),
70        )
71        .await
72}