Skip to main content

loops_so/client/
templates.rs

1use super::{LoopsClient, Page, check};
2use crate::error::Result;
3use crate::generated::{
4    ComponentResponse, CreateComponentBody, CreateComponentRequest, CreateComponentResponse,
5    CreateThemeBody, CreateThemeRequest, CreateThemeResponse, GetComponentRequest,
6    GetComponentResponse, GetThemeRequest, GetThemeResponse, ListComponentsRequest,
7    ListComponentsRequestQuery, ListComponentsResponse, ListComponentsResponseEnum,
8    ListThemesRequest, ListThemesRequestQuery, ListThemesResponse, ListThemesResponseEnum,
9    ThemeResponse, ThemeStyles, UpdateComponentBody, UpdateComponentRequest,
10    UpdateComponentResponse, UpdateComponentResponseEnum, UpdateThemeBody, UpdateThemeRequest,
11    UpdateThemeResponse, UpdateThemeResponseEnum,
12};
13use crate::routes::Route;
14
15impl LoopsClient {
16    /// List components.
17    pub async fn list_components(&self, page: Page) -> Result<ListComponentsResponse> {
18        const ACTION: &str = "list components";
19        let query = ListComponentsRequestQuery {
20            per_page: page.per_page_param(),
21            cursor: page.cursor,
22        };
23        let raw = self
24            .send(Route::ListComponents, &[], |r| r.query(&query))
25            .await?;
26        match raw
27            .parse(ACTION, ListComponentsRequest::parse_response)
28            .await?
29        {
30            ListComponentsResponseEnum::Ok(components) => Ok(components),
31            _ => Err(raw.unexpected(ACTION)),
32        }
33    }
34
35    /// Get a component.
36    pub async fn get_component(&self, component_id: &str) -> Result<ComponentResponse> {
37        const ACTION: &str = "get component";
38        let raw = self
39            .send(Route::GetComponent, &[component_id], |r| r)
40            .await?;
41        match raw
42            .parse(ACTION, GetComponentRequest::parse_response)
43            .await?
44        {
45            GetComponentResponse::Ok(component) => Ok(component),
46            _ => Err(raw.unexpected(ACTION)),
47        }
48    }
49
50    /// Create a component from an LMX body.
51    pub async fn create_component(&self, name: &str, lmx: &str) -> Result<ComponentResponse> {
52        const ACTION: &str = "create component";
53        let body = CreateComponentBody {
54            name: name.to_owned(),
55            lmx: lmx.to_owned(),
56        };
57        check(&body)?;
58        let raw = self
59            .send(Route::CreateComponent, &[], |r| r.json(&body))
60            .await?;
61        match raw
62            .parse(ACTION, CreateComponentRequest::parse_response)
63            .await?
64        {
65            CreateComponentResponse::Created(component) => Ok(component),
66            _ => Err(raw.unexpected(ACTION)),
67        }
68    }
69
70    /// Rename a component and/or replace its LMX body. Changing the body
71    /// updates every email using the component; the response counts them in
72    /// `affected_email_count`.
73    pub async fn update_component(
74        &self,
75        component_id: &str,
76        name: Option<&str>,
77        lmx: Option<&str>,
78    ) -> Result<UpdateComponentResponse> {
79        const ACTION: &str = "update component";
80        let body = UpdateComponentBody {
81            name: name.map(str::to_owned),
82            lmx: lmx.map(str::to_owned),
83        };
84        let raw = self
85            .send(Route::UpdateComponent, &[component_id], |r| r.json(&body))
86            .await?;
87        match raw
88            .parse(ACTION, UpdateComponentRequest::parse_response)
89            .await?
90        {
91            UpdateComponentResponseEnum::Ok(component) => Ok(component),
92            _ => Err(raw.unexpected(ACTION)),
93        }
94    }
95
96    /// List themes.
97    pub async fn list_themes(&self, page: Page) -> Result<ListThemesResponse> {
98        const ACTION: &str = "list themes";
99        let query = ListThemesRequestQuery {
100            per_page: page.per_page_param(),
101            cursor: page.cursor,
102        };
103        let raw = self
104            .send(Route::ListThemes, &[], |r| r.query(&query))
105            .await?;
106        match raw.parse(ACTION, ListThemesRequest::parse_response).await? {
107            ListThemesResponseEnum::Ok(themes) => Ok(themes),
108            _ => Err(raw.unexpected(ACTION)),
109        }
110    }
111
112    /// Get a theme.
113    pub async fn get_theme(&self, theme_id: &str) -> Result<ThemeResponse> {
114        const ACTION: &str = "get theme";
115        let raw = self.send(Route::GetTheme, &[theme_id], |r| r).await?;
116        match raw.parse(ACTION, GetThemeRequest::parse_response).await? {
117            GetThemeResponse::Ok(theme) => Ok(theme),
118            _ => Err(raw.unexpected(ACTION)),
119        }
120    }
121
122    /// Create a theme. `styles` mirrors the attributes of the LMX `<Style />`
123    /// tag; unset fields keep the Loops defaults.
124    pub async fn create_theme(
125        &self,
126        name: &str,
127        styles: Option<ThemeStyles>,
128    ) -> Result<ThemeResponse> {
129        const ACTION: &str = "create theme";
130        let body = CreateThemeBody {
131            name: name.to_owned(),
132            styles,
133        };
134        check(&body)?;
135        let raw = self
136            .send(Route::CreateTheme, &[], |r| r.json(&body))
137            .await?;
138        match raw
139            .parse(ACTION, CreateThemeRequest::parse_response)
140            .await?
141        {
142            CreateThemeResponse::Created(theme) => Ok(theme),
143            _ => Err(raw.unexpected(ACTION)),
144        }
145    }
146
147    /// Rename a theme and/or change its styles. Changing styles updates every
148    /// email using the theme; the response counts them in
149    /// `affected_email_count`.
150    pub async fn update_theme(
151        &self,
152        theme_id: &str,
153        name: Option<&str>,
154        styles: Option<ThemeStyles>,
155    ) -> Result<UpdateThemeResponse> {
156        const ACTION: &str = "update theme";
157        let body = UpdateThemeBody {
158            name: name.map(str::to_owned),
159            styles,
160        };
161        let raw = self
162            .send(Route::UpdateTheme, &[theme_id], |r| r.json(&body))
163            .await?;
164        match raw
165            .parse(ACTION, UpdateThemeRequest::parse_response)
166            .await?
167        {
168            UpdateThemeResponseEnum::Ok(theme) => Ok(theme),
169            _ => Err(raw.unexpected(ACTION)),
170        }
171    }
172}