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
use ploidy_core::codegen::IntoCode;
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt, quote};
use super::{
cfg::CfgFeature,
graph::CodegenGraph,
naming::{CargoFeature, CodegenIdentUsage},
};
/// Generates the `client/mod.rs` source file.
#[derive(Clone, Copy, Debug)]
pub struct CodegenClientModule<'a> {
graph: &'a CodegenGraph<'a>,
features: &'a [&'a CargoFeature],
}
impl<'a> CodegenClientModule<'a> {
pub fn new(graph: &'a CodegenGraph<'a>, features: &'a [&'a CargoFeature]) -> Self {
Self { graph, features }
}
}
impl ToTokens for CodegenClientModule<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let mods = self.features.iter().map(|feature| {
let cfg = CfgFeature::for_resource_module(feature);
let mod_name = CodegenIdentUsage::Module(feature.as_ident());
quote! {
#cfg
pub mod #mod_name;
}
});
let client_doc = {
let info = self.graph.info();
format!("API client for {} (version {})", info.title, info.version)
};
tokens.append_all(quote! {
#[doc = #client_doc]
#[derive(Clone, Debug)]
pub struct Client {
client: ::ploidy_util::reqwest::Client,
headers: ::ploidy_util::http::HeaderMap,
base_url: ::ploidy_util::url::Url,
}
impl Client {
/// Create a new client.
pub fn new(base_url: impl AsRef<str>) -> Result<Self, crate::error::Error> {
Ok(Self::with_reqwest_client(
::ploidy_util::reqwest::Client::new(),
base_url.as_ref().parse()?,
))
}
pub fn with_reqwest_client(
client: crate::util::reqwest::Client,
base_url: crate::util::url::Url,
) -> Self {
Self {
client,
headers: ::ploidy_util::http::HeaderMap::new(),
base_url,
}
}
/// Adds a header to each request.
pub fn with_header<K, V>(mut self, name: K, value: V) -> Result<Self, crate::error::Error>
where
K: TryInto<crate::util::http::HeaderName>,
V: TryInto<crate::util::http::HeaderValue>,
K::Error: Into<crate::util::http::Error>,
V::Error: Into<crate::util::http::Error>,
{
let name = name
.try_into()
.map_err(|err| crate::error::Error::BadHeaderName(err.into()))?;
let value = value
.try_into()
.map_err(|err| crate::error::Error::BadHeaderValue(name.clone(), err.into()))?;
self.headers.insert(name, value);
Ok(Self {
client: self.client,
headers: self.headers,
base_url: self.base_url,
})
}
/// Adds a sensitive header to each request, like a password or a bearer token.
/// Sensitive headers won't appear in `Debug` output, and may be treated specially
/// by the underlying HTTP stack.
///
/// # Example
///
/// ```rust,ignore
/// use reqwest::header::AUTHORIZATION;
///
/// let client = Client::new("https://api.example.com")?
/// .with_sensitive_header(AUTHORIZATION, "Bearer decafbadcafed00d")?;
/// ```
pub fn with_sensitive_header<K, V>(self, name: K, value: V) -> Result<Self, crate::error::Error>
where
K: TryInto<crate::util::http::HeaderName>,
V: TryInto<crate::util::http::HeaderValue>,
K::Error: Into<crate::util::http::Error>,
V::Error: Into<crate::util::http::Error>,
{
let name = name
.try_into()
.map_err(|err| crate::error::Error::BadHeaderName(err.into()))?;
let mut value: ::ploidy_util::http::HeaderValue = value
.try_into()
.map_err(|err| crate::error::Error::BadHeaderValue(name.clone(), err.into()))?;
value.set_sensitive(true);
self.with_header(name, value)
}
pub fn with_user_agent<V>(self, value: V) -> Result<Self, crate::error::Error>
where
V: TryInto<crate::util::http::HeaderValue>,
V::Error: Into<crate::util::http::Error>,
{
self.with_header(::ploidy_util::http::header::USER_AGENT, value)
}
/// Returns a raw [`RequestBuilder`].
///
/// The builder sets the base URL and default headers. Use this for
/// requests that the typed client methods don't support.
///
/// [`RequestBuilder`]: crate::util::reqwest::RequestBuilder
pub fn request(
&self,
method: crate::util::reqwest::Method,
path: &str,
) -> crate::util::reqwest::RequestBuilder {
let mut url = self.base_url.clone();
let _ = url
.path_segments_mut()
.map(|mut segments| {
segments.pop_if_empty().extend(path.split('/'));
});
self.client
.request(method, url)
.headers(self.headers.clone())
}
}
#(#mods)*
});
}
}
impl IntoCode for CodegenClientModule<'_> {
type Code = (&'static str, TokenStream);
fn into_code(self) -> Self::Code {
("src/client/mod.rs", self.into_token_stream())
}
}