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
pub mod apiclient;
pub mod async_api;
pub mod auth;
pub mod endpoint;
pub mod mock;
mod reqwest_adaptors;
pub mod response;
use crate::framework::{apiclient::ApiClient, auth::AuthClient, response::map_api_response};
use failure::Fallible;
use reqwest_adaptors::match_reqwest_method;
use serde::Serialize;
use std::time::Duration;
#[derive(Serialize, Clone, Debug)]
pub enum OrderDirection {
#[serde(rename = "asc")]
Ascending,
#[serde(rename = "desc")]
Descending,
}
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum SearchMatch {
All,
Any,
}
#[derive(Debug)]
pub enum Environment {
Production,
Custom(url::Url),
}
impl<'a> From<&'a Environment> for url::Url {
fn from(environment: &Environment) -> Self {
match environment {
Environment::Production => {
url::Url::parse("https://api.cloudflare.com/client/v4/").unwrap()
}
Environment::Custom(url) => url.clone(),
}
}
}
pub struct HttpApiClient {
environment: Environment,
credentials: auth::Credentials,
http_client: reqwest::blocking::Client,
}
pub struct HttpApiClientConfig {
pub http_timeout: Duration,
pub default_headers: http::HeaderMap,
}
impl Default for HttpApiClientConfig {
fn default() -> Self {
HttpApiClientConfig {
http_timeout: Duration::from_secs(30),
default_headers: http::HeaderMap::default(),
}
}
}
impl HttpApiClient {
pub fn new(
credentials: auth::Credentials,
config: HttpApiClientConfig,
environment: Environment,
) -> Fallible<HttpApiClient> {
let http_client = reqwest::blocking::Client::builder()
.timeout(config.http_timeout)
.default_headers(config.default_headers)
.build()?;
Ok(HttpApiClient {
environment,
credentials,
http_client,
})
}
}
impl<'a> ApiClient for HttpApiClient {
fn request<ResultType, QueryType, BodyType>(
&self,
endpoint: &dyn endpoint::Endpoint<ResultType, QueryType, BodyType>,
) -> response::ApiResponse<ResultType>
where
ResultType: response::ApiResult,
QueryType: Serialize,
BodyType: Serialize,
{
let mut request = self
.http_client
.request(
match_reqwest_method(endpoint.method()),
endpoint.url(&self.environment),
)
.query(&endpoint.query());
if let Some(body) = endpoint.body() {
request = request.body(serde_json::to_string(&body).unwrap());
request = request.header(reqwest::header::CONTENT_TYPE, endpoint.content_type());
}
request = request.auth(&self.credentials);
let response = request.send()?;
map_api_response(response)
}
}