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
use reqwest::Method;
use sha2::Sha256;
use hmac::{Hmac, NewMac, Mac};
use serde::Serialize;
use crate::espocrm_types::Params;
type HmacSha256 = Hmac<Sha256>;
pub type NoGeneric = u8;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EspoApiClient {
pub(crate) url: String,
pub(crate) username: Option<String>,
pub(crate) password: Option<String>,
pub(crate) api_key: Option<String>,
pub(crate) secret_key: Option<String>,
pub(crate) url_path: String,
}
impl EspoApiClient {
pub fn new(url: &str) -> EspoApiClient {
EspoApiClient {
url: url.to_string(),
username: None,
password: None,
api_key: None,
secret_key: None,
url_path: "/api/v1/".to_string()
}
}
pub fn build(&self) -> Self {
self.clone()
}
pub fn set_url<'a>(&'a mut self, url: &'a str) -> &'a mut EspoApiClient {
let url = if url.ends_with("/") {
let mut url = url.to_string();
url.pop();
url
} else {
url.to_string()
};
self.url = url;
self
}
pub fn set_username<'a>(&'a mut self, username: &'a str) -> &'a mut EspoApiClient {
self.username = Some(username.to_string());
self
}
pub fn set_password<'a>(&'a mut self, password: &'a str) -> &'a mut EspoApiClient {
self.password = Some(password.to_string());
self
}
pub fn set_api_key<'a>(&'a mut self, api_key: &'a str) -> &'a mut EspoApiClient {
self.api_key = Some(api_key.to_string());
self
}
pub fn set_secret_key<'a>(&'a mut self, secret_key: &'a str) -> &'a mut EspoApiClient {
self.secret_key = Some(secret_key.to_string());
self
}
pub(crate) fn normalize_url(&self, action: String) -> String {
format!("{}{}{}", self.url, self.url_path, action)
}
pub async fn request<T: Serialize + Clone>(&self, method: reqwest::Method, action: String, data_get: Option<Params>, data_post: Option<T>) -> reqwest::Result<reqwest::Response> {
let mut url = self.normalize_url(action.clone());
url = if data_get.is_some() && method == Method::GET {
format!("{}?{}", url, crate::serializer::serialize(data_get.unwrap()).unwrap())
} else {
url
};
println!("{}", &url);
let client = reqwest::Client::new();
let mut request_builder = client.request(method.clone(), url);
if self.username.is_some() && self.password.is_some() {
request_builder = request_builder.basic_auth(self.username.clone().unwrap(), self.password.clone());
} else if self.api_key.is_some() && self.secret_key.is_some() {
let str = format!("{} /{}", method.clone().to_string(), action.clone());
println!("{}", &str);
let mut mac = HmacSha256::new_from_slice(self.secret_key.clone().unwrap().as_bytes()).expect("Unable to create Hmac instance. Is your key valid?");
mac.update(str.as_bytes());
let mac_result = mac.finalize().into_bytes();
let auth_part = format!("{}{}{}",
base64::encode(self.api_key.clone().unwrap().as_bytes()),
"6",
base64::encode(mac_result));
println!("auth: {}", &auth_part);
request_builder = request_builder.header("X-Hmac-Authorization", auth_part);
} else if self.api_key.is_some() {
request_builder = request_builder.header("X-Api-Key", self.api_key.clone().unwrap());
}
if data_post.is_some() {
if method != Method::GET {
request_builder = request_builder.json(&data_post.clone().unwrap());
request_builder = request_builder.header("Content-Type", "application/json");
}
}
let response = request_builder.send();
response.await
}
}