1mod handler;
31mod request;
32mod service;
33mod types;
34mod utils;
35
36pub mod prelude {
37 pub use crate::methods::*;
38 pub use crate::request::Auth as ServiceAuth;
39 pub use crate::service::state::*;
40 pub use crate::service::*;
41 pub use crate::types::*;
42 pub use crate::utils::test_utils;
43}
44
45use auth_service::AuthService;
46use block_service::BlockService;
47use cart_service::CartService;
48use category_service::CategoryService;
49pub use category_service::{
50 CreateCategoryResponse, GetAllCategoryResponse, GetAllProductsResponse, UpdateCategoryResponse,
51};
52use config::ServiceConfig;
53use order_service::OrderService;
54use prelude::*;
55use product_service::ProductService;
56use user_service::UserService;
57use vendor_service::VendorService;
58
59pub struct Client {
61 username: String,
62 api_key: String,
63 host: String,
64}
65
66impl Default for Client {
67 fn default() -> Self {
68 Self::new()
69 }
70}
71
72impl Client {
73 pub fn new() -> Self {
74 Client {
75 username: "".to_string(),
76 api_key: "".to_string(),
77 host: "".to_string(),
78 }
79 }
80
81 pub fn username(mut self, username: &str) -> Self {
83 self.username = username.to_string();
84 self
85 }
86
87 pub fn api_key(mut self, api_key: &str) -> Self {
88 self.api_key = api_key.to_string();
89 self
90 }
91
92 pub fn host(mut self, host: &str) -> Self {
93 self.host = host.to_string();
94 self
95 }
96
97 pub fn auth(&self) -> AuthService {
98 let config = ServiceConfig::with_resource(Resource::Auth)
99 .host(self.host.as_str())
100 .auth(ServiceAuth::from(&self.username, &self.api_key));
101 AuthService::with_config(config)
102 }
103
104 pub fn block(&self) -> BlockService {
105 let config = ServiceConfig::with_resource(Resource::Blocks)
106 .host(self.host.as_str())
107 .auth(ServiceAuth::from(&self.username, &self.api_key));
108 BlockService::with_config(config)
109 }
110
111 pub fn cart(&self) -> CartService {
112 let config = ServiceConfig::with_resource(Resource::Cart)
113 .host(self.host.as_str())
114 .auth(ServiceAuth::from(&self.username, &self.api_key));
115 CartService::with_config(config)
116 }
117
118 pub fn call_request(&self) -> Service<Authenticated> {
119 Service::with_resource(Resource::CallRequest)
120 .host(self.host.as_str())
121 .auth(ServiceAuth::from(&self.username, &self.api_key))
122 }
123
124 pub fn category(&self) -> CategoryService {
125 let config = ServiceConfig::with_resource(Resource::Category)
126 .host(self.host.as_str())
127 .auth(ServiceAuth::from(&self.username, &self.api_key));
128 CategoryService::with_config(config)
129 }
130
131 pub fn discussion(&self) -> Service<Authenticated> {
132 Service::with_resource(Resource::Discussion)
133 .host(self.host.as_str())
134 .auth(ServiceAuth::from(&self.username, &self.api_key))
135 }
136
137 pub fn language(&self) -> Service<Authenticated> {
138 Service::with_resource(Resource::Languages)
139 .host(self.host.as_str())
140 .auth(ServiceAuth::from(&self.username, &self.api_key))
141 }
142
143 pub fn langvars(&self) -> Service<Authenticated> {
144 Service::with_resource(Resource::Langvars)
145 .host(self.host.as_str())
146 .auth(ServiceAuth::from(&self.username, &self.api_key))
147 }
148
149 pub fn order(&self) -> OrderService {
150 let config = ServiceConfig::with_resource(Resource::Order)
151 .host(self.host.as_str())
152 .auth(ServiceAuth::from(&self.username, &self.api_key));
153 OrderService::with_config(config)
154 }
155
156 pub fn page(&self) -> Service<Authenticated> {
157 Service::with_resource(Resource::Pages)
158 .host(self.host.as_str())
159 .auth(ServiceAuth::from(&self.username, &self.api_key))
160 }
161
162 pub fn payment_method(&self) -> Service<Authenticated> {
163 Service::with_resource(Resource::PaymentMethod)
164 .host(self.host.as_str())
165 .auth(ServiceAuth::from(&self.username, &self.api_key))
166 }
167
168 pub fn product(&self) -> ProductService {
169 let config = ServiceConfig::with_resource(Resource::Product)
170 .host(self.host.as_str())
171 .auth(ServiceAuth::from(&self.username, &self.api_key));
172 ProductService::with_config(config)
173 }
174
175 pub fn product_feature(&self) -> Service<Authenticated> {
176 Service::with_resource(Resource::ProductFeature)
177 .host(self.host.as_str())
178 .auth(ServiceAuth::from(&self.username, &self.api_key))
179 }
180
181 pub fn product_option(&self) -> Service<Authenticated> {
182 Service::with_resource(Resource::ProductOption)
183 .host(self.host.as_str())
184 .auth(ServiceAuth::from(&self.username, &self.api_key))
185 }
186
187 pub fn settings(&self) -> Service<Authenticated> {
188 Service::with_resource(Resource::Settings)
189 .host(self.host.as_str())
190 .auth(ServiceAuth::from(&self.username, &self.api_key))
191 }
192
193 pub fn shipment(&self) -> Service<Authenticated> {
194 Service::with_resource(Resource::Shipment)
195 .host(self.host.as_str())
196 .auth(ServiceAuth::from(&self.username, &self.api_key))
197 }
198
199 pub fn shipment_method(&self) -> Service<Authenticated> {
200 Service::with_resource(Resource::ShipmentMethod)
201 .host(self.host.as_str())
202 .auth(ServiceAuth::from(&self.username, &self.api_key))
203 }
204
205 pub fn status(&self) -> Service<Authenticated> {
206 Service::with_resource(Resource::Status)
207 .host(self.host.as_str())
208 .auth(ServiceAuth::from(&self.username, &self.api_key))
209 }
210
211 pub fn tax(&self) -> Service<Authenticated> {
212 Service::with_resource(Resource::Tax)
213 .host(self.host.as_str())
214 .auth(ServiceAuth::from(&self.username, &self.api_key))
215 }
216
217 pub fn user(&self) -> UserService {
218 let config = ServiceConfig::with_resource(Resource::User)
219 .host(self.host.as_str())
220 .auth(ServiceAuth::from(&self.username, &self.api_key));
221 UserService::with_config(config)
222 }
223
224 pub fn user_group(&self) -> Service<Authenticated> {
225 Service::with_resource(Resource::UserGroups)
226 .host(self.host.as_str())
227 .auth(ServiceAuth::from(&self.username, &self.api_key))
228 }
229
230 pub fn vendor(&self) -> VendorService {
231 let config = ServiceConfig::with_resource(Resource::Vendor)
232 .host(self.host.as_str())
233 .auth(ServiceAuth::from(&self.username, &self.api_key));
234 VendorService::with_config(config)
235 }
236
237 fn get_username(&self) -> &str {
238 &self.username
239 }
240
241 fn get_api_key(&self) -> &str {
242 &self.api_key
243 }
244
245 fn get_host(&self) -> &str {
246 &self.host
247 }
248}
249
250#[cfg(test)]
251mod tests {
252 use super::*;
253 use dotenv::dotenv;
254 #[test]
255 fn it_creates_api_client() {
256 dotenv().ok();
257
258 let api_key = std::env::var("CSCART_API_KEY").expect("No api key found");
259
260 let username = std::env::var("CSCART_USERNAME").expect("No username found");
261
262 let host = std::env::var("CSCART_HOST").expect("No host found");
263
264 let client = Client::new()
265 .host(host.as_str())
266 .username(username.as_str())
267 .api_key(api_key.as_str());
268
269 assert_eq!(client.get_username(), username);
270 assert_eq!(client.get_api_key(), api_key);
271 assert_eq!(client.get_host(), host);
272 }
273}