1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! # About
//!
//! Cscart-rs is an sdk for making api request using the cscart rest api
//!
//! This library is a work in process and so not all entities are supported for the v1
//! release
//!
//!  # Example
//! ```
//! use cscart_rs::prelude::*;
//! use cscart_rs::Client;
//! use serde_json::Value;
//! use anyhow;
//!
//! async fn get_categories() -> anyhow::Result<Value> {
//!     let client = Client::new()
//!         .host("http://my-ecommerce-site.com")
//!         .username("my-user-email@email.com")
//!         .api_key("user-api-key");
//!
//!     let categories = client
//!         .category()
//!         .get_all(GetAllOptions::default()).await;
//!
//!     categories
//! }
//! ```

mod handler;
mod request;
mod service;
mod types;
mod utils;

pub mod prelude {
    pub use crate::request::Auth as ServiceAuth;
    pub use crate::service::*;
    pub use crate::types::*;
    pub use crate::utils::test_utils;
}

use prelude::*;

/// Configure an api client to perform requests
pub struct Client {
    username: String,
    api_key: String,
    host: String,
}

impl Default for Client {
    fn default() -> Self {
        Self::new()
    }
}

impl Client {
    pub fn new() -> Self {
        Client {
            username: "".to_string(),
            api_key: "".to_string(),
            host: "".to_string(),
        }
    }

    /// The cscart api user's email address
    pub fn username(mut self, username: &str) -> Self {
        self.username = username.to_string();
        self
    }

    pub fn api_key(mut self, api_key: &str) -> Self {
        self.api_key = api_key.to_string();
        self
    }

    pub fn host(mut self, host: &str) -> Self {
        self.host = host.to_string();
        self
    }

    pub fn auth(&self) -> Service<Authenticated> {
        Service::<Unauthenticated>::with_resource(Resource::Auth)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn block(&self) -> Service<Authenticated> {
        Service::<Unauthenticated>::with_resource(Resource::Blocks)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn cart(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Cart)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn call_request(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::CallRequest)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn category(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Category)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn discussion(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Discussion)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn language(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Languages)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn langvars(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Langvars)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn order(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Order)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn page(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Pages)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn payment_method(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::PaymentMethod)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn product(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Product)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn product_feature(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::ProductFeature)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn product_option(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::ProductOption)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn settings(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Settings)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn shipment(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Shipment)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn shipment_method(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::ShipmentMethod)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn status(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Status)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn tax(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Tax)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn user(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::User)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn user_group(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::UserGroups)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    pub fn vendor(&self) -> Service<Authenticated> {
        Service::with_resource(Resource::Vendor)
            .host(self.host.as_str())
            .auth(ServiceAuth::from(&self.username, &self.api_key))
    }

    fn get_username(&self) -> &str {
        &self.username
    }

    fn get_api_key(&self) -> &str {
        &self.api_key
    }

    fn get_host(&self) -> &str {
        &self.host
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use dotenv::dotenv;
    #[test]
    fn it_creates_api_client() {
        dotenv().ok();

        let api_key = std::env::var("CSCART_API_KEY").expect("No api key found");

        let username = std::env::var("CSCART_USERNAME").expect("No username found");

        let host = std::env::var("CSCART_HOST").expect("No host found");

        let client = Client::new()
            .host(host.as_str())
            .username(username.as_str())
            .api_key(api_key.as_str());

        assert_eq!(client.get_username(), username);
        assert_eq!(client.get_api_key(), api_key);
        assert_eq!(client.get_host(), host);
    }
}