dnspod_lib/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use std::collections::HashMap;
4
5use chrono::Utc;
6use data_types::*;
7use header_params::HeaderParams;
8use signature::calculate_authorization;
9
10pub mod action;
11pub mod consts;
12pub mod data_types;
13pub mod error_code;
14pub mod header_params;
15pub mod response;
16pub mod signature;
17mod macros;
18pub mod utils;
19
20// re-export serde and serde_json
21// #[allow(unused_imports)]
22// #[macro_use]
23pub use serde;
24pub use serde_json;
25
26pub mod prelude {
27    pub use super::action::*;
28    pub use super::ExtractCommonParams;
29}
30
31pub trait ExtractCommonParams {
32    fn action(&self) -> &'static str;
33    fn body(&self) -> Vec<u8>;
34    fn url(&self) -> &'static str { consts::DNSPOD_URL }
35    fn version(&self) -> Version { Default::default() }
36    fn region(&self) -> Option<Region> { None }
37
38    fn headers(
39        &self,
40        secret_id: &impl AsRef<str>,
41        secret_key: &impl AsRef<str>,
42    ) -> HashMap<String, String> {
43        let datetime = Utc::now();
44        // let timestamp = datetime.timestamp() as u64;
45        // let date = datetime.date_naive().to_string();
46
47        let mut hp = HeaderParams {
48            action: self.action(),
49            version: self.version(),
50            region: self.region(),
51            datetime,
52            authorization: "".into(),
53            language: Language::EnUS,
54            content_type: ContentType::JSON,
55        };
56
57        let body = self.body();
58        hp.authorization =
59            calculate_authorization(&body, &hp, secret_id.as_ref(), secret_key.as_ref());
60
61        hp.into()
62    }
63}