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
//! # 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::Client;
//! use serde_json::Value;
//! use std::error::Error;
//!
//! async fn get_categories() -> Result<Value , Box<dyn Error>> {
//!     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().await;
//!
//!     categories
//! }
//! ```

mod request;
mod crud;
mod service;

use dotenv::dotenv;

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

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 category (&self) -> service::Service {
        service::Service::new()
            .host(self.host.as_str())
            .api_key(self.api_key.as_str())
            .username(self.username.as_str())
            .path("/api/2.0/categories")
            .build()
    }

    pub fn product (&self) -> service::Service {
        service::Service::new()
            .host(self.host.as_str())
            .api_key(self.api_key.as_str())
            .username(self.username.as_str())
            .path("/api/2.0/products")
            .build()
    }

    pub fn user (&self) -> service::Service {
        service::Service::new()
            .host(self.host.as_str())
            .api_key(self.api_key.as_str())
            .username(self.username.as_str())
            .path("/api/2.0/users")
            .build()
    }

    pub fn cart (&self) -> service::Service {
        service::Service::new()
            .host(self.host.as_str())
            .api_key(self.api_key.as_str())
            .username(self.username.as_str())
            .path("/api/2.0/carts")
            .build()
    }

    pub fn vendor (&self) -> service::Service {
        service::Service::new()
            .host(self.host.as_str())
            .api_key(self.api_key.as_str())
            .username(self.username.as_str())
            .path("/api/2.0/vendors")
            .build()
    }

    pub fn payment_method (&self) -> service::Service {
        service::Service::new()
            .host(self.host.as_str())
            .api_key(self.api_key.as_str())
            .username(self.username.as_str())
            .path("/api/2.0/payments")
            .build()
    }

    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 {
    #[test]
    fn it_creates_api_client() {
        use super::*;
        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);
    }
}