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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::{collections::HashMap, time::Duration};

use hmac::{Hmac, Mac};
use md5::{Digest, Md5};
use reqwest::{
    header::{HeaderMap, HeaderValue},
    ClientBuilder, Response,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sha1::Sha1;
use time::{macros::format_description, OffsetDateTime};
use url::Url;
use uuid::Uuid;

use crate::client::error::{Error, Result};

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct ROAServiceError {
    /// Error code
    pub code: String,
    /// Error message
    pub message: String,
    /// Request id
    #[serde(default)]
    pub request_id: String,
    /// Recommend
    #[serde(default)]
    pub recommend: String,
}

/// Default const header.
const AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
const DEFAULT_HEADER: &[(&str, &str)] = &[
    ("accept", "application/json"),
    ("x-acs-signature-method", "HMAC-SHA1"),
    ("user-agent", AGENT),
    ("x-sdk-client", AGENT),
];

type HamcSha1 = Hmac<Sha1>;

/// Config for request.
#[derive(Clone, Debug, Default)]
struct Request {
    method: String,
    uri: String,
    body: Option<String>,
    query: Vec<(String, String)>,
    headers: HeaderMap,
    project: Option<String>,
    version: String,
    timeout: Option<Duration>,
}

#[derive(Clone, Debug)]
pub struct ROAClient {
    /// The access key id of aliyun developer account.
    access_key_id: String,
    /// The access key secret of aliyun developer account.
    access_key_secret: String,
    /// The api endpoint of aliyun api service (need start with http:// or https://).
    endpoint: String,
    /// The config of http request.
    request: Request,
}

impl ROAClient {
    /// Create a api client.
    pub fn new(
        access_key_id: impl Into<String>,
        access_key_secret: impl Into<String>,
        endpoint: impl Into<String>,
    ) -> Self {
        ROAClient {
            access_key_id: access_key_id.into(),
            access_key_secret: access_key_secret.into(),
            endpoint: endpoint.into(),
            request: Default::default(),
        }
    }

    /// Create a request with the `method` and `uri`.
    ///
    /// Returns a `Self` for send request.
    pub fn request(mut self, method: impl Into<String>, uri: impl Into<String>) -> Self {
        self.request.method = method.into();
        self.request.uri = uri.into();

        self
    }

    /// Create a `GET` request with the `uri`.
    ///
    /// Returns a `Self` for send request.
    pub fn get(self, uri: impl Into<String>) -> Self {
        self.request("GET".to_string(), uri.into())
    }

    /// Create a `POST` request with the `uri`.
    ///
    /// Returns a `Self` for send request.
    pub fn post(self, uri: impl Into<String>) -> Self {
        self.request("POST".to_string(), uri.into())
    }

    /// Set queries for request.
    ///
    /// Returns a `Self` for send request.
    pub fn query<I, T>(mut self, queries: I) -> Self
    where
        I: IntoIterator<Item = (T, T)>,
        T: Into<String>,
    {
        self.request.query = queries
            .into_iter()
            .map(|v| (v.0.into(), v.1.into()))
            .collect();

        self
    }

    /// Set version for request.
    ///
    /// Returns a `Self` for send request.
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.request.version = version.into();

        self
    }

    /// Set body for request.
    ///
    /// Returns a `Self` for send request.
    pub fn body(mut self, body: impl Into<String>) -> Result<Self> {
        // compute body length and md5.
        let body = body.into();
        let mut hasher = Md5::new();
        hasher.update(body.as_bytes());
        let md5_result = hasher.finalize();

        // update headers.
        self.request
            .headers
            .insert("content-length", body.len().to_string().parse()?);
        self.request
            .headers
            .insert("content-md5", base64::encode(md5_result).parse()?);

        // store body string.
        self.request.body = Some(body);

        Ok(self)
    }

    /// Set header for request.
    ///
    /// Returns a `Self` for send request.
    pub fn header(mut self, headers: impl Into<HashMap<String, String>>) -> Result<Self> {
        self.request.headers = (&headers.into())
            .try_into()
            .map_err(|e| Error::InvalidRequest(format!("Cannot parse header: {}", e)))?;
        Ok(self)
    }

    /// Set project for request.
    ///
    /// Returns a `Self` for send request.
    pub fn project(mut self, project: impl Into<String>) -> Self {
        self.request.project = Some(project.into());

        self
    }

    /// Set a timeout for connect, read and write operations of a `Client`.
    ///
    /// Default is no timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.request.timeout = Some(timeout);

        self
    }

    /// Send a request to service.
    /// Try to deserialize the response body as JSON.
    pub async fn json<T: DeserializeOwned>(self) -> Result<T> {
        Ok(self.send().await?.json::<T>().await?)
    }

    /// Send a request to service.
    /// Try to deserialize the response body as TEXT.
    pub async fn text(self) -> Result<String> {
        Ok(self.send().await?.text().await?)
    }

    /// Send a request to service.
    /// Return client Response.
    pub async fn send(mut self) -> Result<Response> {
        // add const header
        for (k, v) in DEFAULT_HEADER.iter() {
            self.request.headers.insert(*k, v.parse()?);
        }

        // add host header.
        let endpoint = Url::parse(&self.endpoint)
            .map_err(|e| Error::InvalidRequest(format!("Invalid endpoint: {e}")))?;
        let host = endpoint
            .host_str()
            .ok_or_else(|| Error::InvalidRequest(format!("Invalid endpoint: {endpoint}")))?;
        self.request.headers.insert("host", host.parse()?);

        // add date header.
        // RFC 1123: %a, %d %b %Y %H:%M:%S GMT
        let format = format_description!(
            "[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] GMT"
        );
        let ts = OffsetDateTime::now_utc()
            .format(&format)
            .map_err(|e| Error::InvalidRequest(format!("Invalid RFC 1123 Date: {}", e)))?;
        self.request.headers.insert("date", ts.parse()?);

        // add nonce header.
        let nonce = Uuid::new_v4().to_string();
        self.request
            .headers
            .insert("x-acs-signature-nonce", nonce.parse()?);

        // compute `Authorization` field.
        // Authorization = "acs <AccessKeyId>:<Signature>"
        let authorization = format!("acs {}:{}", self.access_key_id, self.signature()?);
        self.request
            .headers
            .insert("Authorization", authorization.parse()?);

        // build http client.
        let final_url = format!("{}{}", self.endpoint, self.request.uri);
        let mut http_client_builder = ClientBuilder::new();
        if let Some(timeout) = self.request.timeout {
            http_client_builder = http_client_builder.timeout(timeout);
        }
        let mut http_client = http_client_builder.build()?.request(
            self.request
                .method
                .parse()
                .map_err(|e| Error::InvalidRequest(format!("Invalid HTTP method: {}", e)))?,
            &final_url,
        );

        // set body.
        if let Some(body) = self.request.body {
            http_client = http_client.body(body);
        }

        // set query.
        if !self.request.query.is_empty() {
            http_client = http_client.query(&self.request.query);
        }

        // send request.
        let response = http_client.headers(self.request.headers).send().await?;

        // check HTTP StatusCode.
        if !response.status().is_success() {
            let result = response.json::<ROAServiceError>().await?;
            return Err(Error::InvalidResponse {
                request_id: result.request_id,
                error_code: result.code,
                error_message: result.message,
            });
        }

        // return response.
        Ok(response)
    }

    /// Compute canonicalized headers.
    fn canonicalized_headers(&self) -> String {
        let mut headers: Vec<(String, String)> = self
            .request
            .headers
            .iter()
            .filter_map(|(k, v)| {
                let k = k.as_str().to_lowercase();
                if k.starts_with("x-acs-") {
                    Some((k, v.to_str().unwrap().to_string()))
                } else {
                    None
                }
            })
            .collect();
        headers.sort_by(|a, b| a.0.cmp(&b.0));

        let headers: Vec<String> = headers
            .iter()
            .map(|(k, v)| format!("{}:{}", k, v))
            .collect();

        headers.join("\n")
    }

    /// Compute canonicalized resource.
    fn canonicalized_resource(&self) -> String {
        if !self.request.query.is_empty() {
            let mut params = self.request.query.clone();
            params.sort_by_key(|item| item.0.clone());
            let params: Vec<String> = params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
            let sorted_query_string = params.join("&");
            format!("{}?{}", self.request.uri, sorted_query_string)
        } else {
            self.request.uri.clone()
        }
    }

    /// Compute signature for request.
    fn signature(&self) -> Result<String> {
        // build body.
        let canonicalized_headers = self.canonicalized_headers();
        let canonicalized_resource = self.canonicalized_resource();
        let body = format!(
            "{}\n{}\n{}\n{}\n{}\n{}\n{}",
            self.request.method.to_uppercase(),
            self.request.headers["accept"].to_str().unwrap(),
            self.request
                .headers
                .get("content-md5")
                .unwrap_or(&HeaderValue::from_static(""))
                .to_str()
                .unwrap(),
            self.request
                .headers
                .get("content-type")
                .unwrap_or(&HeaderValue::from_static(""))
                .to_str()
                .unwrap(),
            self.request.headers["date"].to_str().unwrap(),
            canonicalized_headers,
            canonicalized_resource
        );

        // sign body.
        let mut mac = HamcSha1::new_from_slice(self.access_key_secret.as_bytes())
            .map_err(|e| Error::InvalidRequest(format!("Invalid HMAC-SHA1 secret key: {}", e)))?;
        mac.update(body.as_bytes());
        let result = mac.finalize();
        let code = result.into_bytes();

        Ok(base64::encode(code))
    }
}

#[cfg(test)]
mod tests {
    use std::env;

    use serde_json::json;

    use super::*;

    #[tokio::test]
    async fn roa_client_invalid_access_key_id_test() -> Result<()> {
        // create roa style api client.
        let aliyun_openapi_client = ROAClient::new(
            env::var("ACCESS_KEY_ID").unwrap(),
            env::var("ACCESS_KEY_SECRET").unwrap(),
            "https://ros.aliyuncs.com",
        );

        // call `DescribeRegions` with empty queries.
        let response = aliyun_openapi_client
            .version("2015-09-01")
            .get("/regions")
            .text()
            .await?;

        assert!(response.contains("Regions"));

        Ok(())
    }

    #[tokio::test]
    async fn roa_client_get_with_timeout() -> Result<()> {
        // create roa style api client.
        let aliyun_openapi_client = ROAClient::new(
            env::var("ACCESS_KEY_ID").unwrap(),
            env::var("ACCESS_KEY_SECRET").unwrap(),
            "https://ros.aliyuncs.com",
        );

        // call `DescribeRegions` with empty queries.
        let response = aliyun_openapi_client
            .version("2015-09-01")
            .get("/regions")
            .timeout(Duration::from_millis(1))
            .text()
            .await;

        assert!(response.is_err());

        Ok(())
    }

    #[tokio::test]
    async fn roa_client_get_with_query_test() -> Result<()> {
        // create roa style api client.
        let aliyun_openapi_client = ROAClient::new(
            env::var("ACCESS_KEY_ID").unwrap(),
            env::var("ACCESS_KEY_SECRET").unwrap(),
            "http://mt.aliyuncs.com",
        );

        // create params.
        let mut params = HashMap::new();
        params.insert("SourceText", "你好");
        params.insert("SourceLanguage", "zh");
        params.insert("TargetLanguage", "en");
        params.insert("FormatType", "text");
        params.insert("Scene", "general");

        let response = aliyun_openapi_client
            .version("2018-04-08")
            .post("/api/translate/web/general")
            .header([("Content-Type".to_string(), "application/json".to_string())])?
            .body(json!(params).to_string())?
            .text()
            .await?;

        assert!(response.contains("Hello"));

        Ok(())
    }
}