cscart_rs/service/
mod.rs

1pub mod auth_service;
2pub mod block_service;
3pub mod cart_service;
4pub mod category_service;
5pub mod config;
6pub mod order_service;
7pub mod product_service;
8pub mod user_service;
9pub mod vendor_service;
10#[macro_use]
11pub mod methods;
12pub mod state;
13
14use crate::handler::HandlerBuilder;
15use crate::handler::{self};
16use crate::prelude::*;
17use serde_json::Value;
18use state::*;
19use std::marker::PhantomData;
20
21pub struct Service<S: ServiceState = Unauthenticated> {
22    pub _marker: PhantomData<S>,
23    pub(crate) host: Option<String>,
24    pub(crate) auth: Option<ServiceAuth>,
25    pub(crate) resource: Option<Resource>, // sub-entity e.g /api/2.0/categories/<id>/products//  Break the path into path , entity and sub_entity methods
26    pub(crate) params: Option<Vec<(String, String)>>,
27}
28
29impl Service<Unauthenticated> {
30    pub fn with_resource(resource: Resource) -> Service<Unauthenticated> {
31        Service::<Unauthenticated> {
32            _marker: PhantomData,
33            host: None,
34            auth: None,
35            resource: Some(resource),
36            params: None,
37        }
38    }
39}
40
41impl Service<Unauthenticated> {
42    pub fn host(self, host: &str) -> Service<OnlyHost> {
43        Service::<OnlyHost> {
44            _marker: PhantomData,
45            host: Some(host.into()),
46            auth: None,
47            resource: self.resource,
48            params: None,
49        }
50    }
51
52    pub fn auth(self, auth: ServiceAuth) -> Service<OnlyAuth> {
53        Service::<OnlyAuth> {
54            _marker: PhantomData,
55            host: None,
56            auth: Some(auth),
57            resource: self.resource,
58            params: None,
59        }
60    }
61}
62
63impl Service<OnlyHost> {
64    pub fn auth(self, auth: ServiceAuth) -> Service<Authenticated> {
65        Service::<Authenticated> {
66            _marker: PhantomData,
67            host: self.host,
68            auth: Some(auth),
69            resource: self.resource,
70            params: None,
71        }
72    }
73}
74
75impl Service<OnlyAuth> {
76    pub fn host(self, host: &str) -> Service<Authenticated> {
77        Service::<Authenticated> {
78            _marker: PhantomData,
79            host: Some(host.into()),
80            auth: self.auth,
81            resource: self.resource,
82            params: None,
83        }
84    }
85}
86
87impl Service<Authenticated> {
88    fn set_handler_credentials(&self) -> HandlerBuilder {
89        handler::Handler::new()
90            .host(self.host.as_ref().unwrap().as_str())
91            .username(self.auth.as_ref().unwrap().username())
92            .api_key(self.auth.as_ref().unwrap().api_key())
93    }
94
95    pub async fn create(&self, data: Value) -> anyhow::Result<Value> {
96        let handler = self
97            .set_handler_credentials()
98            .path(self.resource.as_ref().unwrap().path())
99            .build();
100        let rsp = handler.create(data).await?;
101        Ok(rsp)
102    }
103
104    pub async fn get_all(&self, options: GetAllOptions) -> anyhow::Result<Value> {
105        // This method sometimes requires mandatory params to be provided (depending on resource e.g User requires user_type)
106        let handler = self
107            .set_handler_credentials()
108            .path(self.resource.as_ref().unwrap().path())
109            .set_query_params(options.params())
110            .build();
111        let rsp = handler.read().await?;
112        Ok(rsp)
113    }
114
115    pub async fn get_by_id(&mut self, id: &str) -> anyhow::Result<Value> {
116        let handler = self
117            .set_handler_credentials()
118            .path(format!("{}/{}", self.resource.as_ref().unwrap().path(), id))
119            .build();
120
121        let rsp = handler.read().await?;
122        Ok(rsp)
123    }
124
125    pub async fn update_by_id(&self, id: &str, data: Value) -> anyhow::Result<Value> {
126        let handler = self
127            .set_handler_credentials()
128            .path(format!("{}/{}", self.resource.as_ref().unwrap().path(), id))
129            .build();
130
131        let rsp = handler.update(data).await?;
132        Ok(rsp)
133    }
134
135    pub async fn delete_by_id(&self, id: &str) -> anyhow::Result<Value> {
136        let handler = self
137            .set_handler_credentials()
138            .path(format!("{}/{}", self.resource.as_ref().unwrap().path(), id))
139            .build();
140
141        let rsp = handler.delete().await?;
142        Ok(rsp)
143    }
144
145    pub async fn get_all_entity(&mut self, id: &str, entity: &str) -> anyhow::Result<Value> {
146        let handler = self
147            .set_handler_credentials()
148            .path(format!(
149                "{}/{}/{}",
150                self.resource.as_ref().unwrap().path(),
151                id,
152                entity
153            ))
154            .build();
155
156        let rsp = handler.read().await?;
157        Ok(rsp)
158    }
159}