Skip to main content

shopify_client/admin/theme/
mod.rs

1pub mod remote;
2
3use crate::common::ServiceContext;
4
5use std::sync::Arc;
6
7use crate::{
8    admin::generated::types::theme::{
9        CreatePreviewThemeInput, ListThemesResp, Theme, ThemeDuplicateResp,
10    },
11    common::types::{APIError, RequestCallbacks},
12};
13
14pub struct ThemeService {
15    pub(crate) ctx: ServiceContext,
16}
17
18impl ThemeService {
19    pub fn new(
20        shop_url: Arc<String>,
21        version: Arc<String>,
22        access_token: Arc<String>,
23        callbacks: Arc<RequestCallbacks>,
24    ) -> Self {
25        Self::with_ctx(ServiceContext::new(
26            shop_url,
27            version,
28            access_token,
29            callbacks,
30        ))
31    }
32
33    pub fn with_ctx(ctx: ServiceContext) -> Self {
34        Self { ctx }
35    }
36
37    pub async fn list(&self) -> Result<ListThemesResp, APIError> {
38        remote::list_themes(&self.ctx, 100, None).await
39    }
40
41    pub async fn get_live(&self) -> Result<Option<Theme>, APIError> {
42        let resp = remote::list_themes(&self.ctx, 1, Some(&["MAIN"])).await?;
43        Ok(resp.themes.edges.into_iter().next().map(|edge| edge.node))
44    }
45
46    pub async fn create_preview(
47        &self,
48        input: &CreatePreviewThemeInput,
49    ) -> Result<ThemeDuplicateResp, APIError> {
50        let source_id = match &input.source_theme_id {
51            Some(id) => id.clone(),
52            None => {
53                self.get_live()
54                    .await?
55                    .ok_or_else(|| APIError::ServerError {
56                        errors: "No live (MAIN) theme found to duplicate".to_string(),
57                    })?
58                    .id
59            }
60        };
61
62        remote::duplicate_theme(&self.ctx, &source_id, input.name.as_deref()).await
63    }
64}