Skip to main content

context69_sdk/client/groups/
translations.rs

1use context69_contracts::{
2    GroupTranslationSettingsResponse, RebuildDocumentTranslationsRequest, TranslationJobResponse,
3    TranslationJobsResponse, UpdateGroupTranslationSettingsRequest,
4};
5use reqwest::Method;
6use uuid::Uuid;
7
8use super::super::Context69Client;
9use crate::{Error, client::transport::group_path};
10
11pub struct GroupTranslationApi<'a> {
12    client: &'a Context69Client,
13    group_path: String,
14}
15
16impl<'a> GroupTranslationApi<'a> {
17    pub(crate) fn new(client: &'a Context69Client, group_path: String) -> Self {
18        Self { client, group_path }
19    }
20
21    pub async fn settings(&self) -> Result<GroupTranslationSettingsResponse, Error> {
22        let path = group_path(&self.group_path, "/translation-settings");
23        self.client
24            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
25            .await
26    }
27
28    pub async fn update_settings(
29        &self,
30        request: &UpdateGroupTranslationSettingsRequest,
31    ) -> Result<GroupTranslationSettingsResponse, Error> {
32        let path = group_path(&self.group_path, "/translation-settings");
33        self.client
34            .execute_json(
35                self.client
36                    .authorized_request(Method::PUT, &path)
37                    .await?
38                    .json(request),
39            )
40            .await
41    }
42
43    pub async fn jobs(&self, document_id: i64) -> Result<TranslationJobsResponse, Error> {
44        let path = group_path(
45            &self.group_path,
46            &format!("/documents/{document_id}/translations"),
47        );
48        self.client
49            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
50            .await
51    }
52
53    pub async fn rebuild(
54        &self,
55        document_id: i64,
56        request: &RebuildDocumentTranslationsRequest,
57    ) -> Result<TranslationJobsResponse, Error> {
58        let path = group_path(
59            &self.group_path,
60            &format!("/documents/{document_id}/translations/rebuild"),
61        );
62        self.client
63            .execute_json(
64                self.client
65                    .authorized_request(Method::POST, &path)
66                    .await?
67                    .json(request),
68            )
69            .await
70    }
71}
72
73pub struct TranslationJobApi<'a> {
74    client: &'a Context69Client,
75    group_path: String,
76    job_id: Uuid,
77}
78
79impl<'a> TranslationJobApi<'a> {
80    pub(crate) fn new(client: &'a Context69Client, group_path: String, job_id: Uuid) -> Self {
81        Self {
82            client,
83            group_path,
84            job_id,
85        }
86    }
87
88    pub async fn get(&self) -> Result<TranslationJobResponse, Error> {
89        self.request(Method::GET, "").await
90    }
91
92    pub async fn retry(&self) -> Result<TranslationJobResponse, Error> {
93        self.request(Method::POST, "/retry").await
94    }
95
96    async fn request(&self, method: Method, suffix: &str) -> Result<TranslationJobResponse, Error> {
97        let path = group_path(
98            &self.group_path,
99            &format!("/translation-jobs/{}{suffix}", self.job_id),
100        );
101        self.client
102            .execute_json(self.client.authorized_request(method, &path).await?)
103            .await
104    }
105}