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
//! Shared HTTP transport. All resource modules call into [`HttpInner::execute`]
//! or [`HttpInner::execute_with_body`].
//!
//! Centralising the transport here means the (de)serialisation, error body
//! extraction, retry loop, `Retry-After` handling, and tracing instrumentation
//! all live in one place — the generated resource modules stay tiny.
use std::{sync::Arc, time::Duration};
use reqwest::{Client, Method, Response, StatusCode};
use serde::{de::DeserializeOwned, Serialize};
use url::Url;
use crate::{
error::{ApiError, ErrorEnvelope, Result},
retry::{is_idempotent_method, RetryConfig},
};
/// Internal, refcounted shared state used by every resource client.
///
/// `HttpInner` is cheap to clone (it's an [`Arc`] under the hood — we expose it
/// directly because the indirection is intentional and lets callers freely move
/// `ApiClient` across tasks).
#[derive(Debug, Clone)]
pub struct HttpInner {
pub(crate) base_url: Arc<Url>,
pub(crate) client: Client,
pub(crate) retry: RetryConfig,
}
impl HttpInner {
pub(crate) fn new(base_url: Url, client: Client, retry: RetryConfig) -> Self {
Self {
base_url: Arc::new(base_url),
client,
retry,
}
}
/// Build a fully-qualified request URL by joining `path` onto the base.
/// The leading `/` of `path` is significant — paths in the OpenAPI spec
/// always start with `/` and we preserve that.
pub(crate) fn url(&self, path: &str) -> Result<Url> {
// Url::join treats absolute paths correctly; we just need to make sure
// base_url ends with `/` so segments don't get dropped. We normalise it
// once at construction time in `ClientBuilder::build`, so this is safe.
let joined = self
.base_url
.join(path.trim_start_matches('/'))
.map_err(ApiError::from)?;
Ok(joined)
}
/// Execute a request that has no body and decode the response into `T`.
/// `T` may be `serde_json::Value` for endpoints where the spec did not pin
/// a schema.
pub async fn execute<T: DeserializeOwned>(&self, method: Method, url: Url) -> Result<T> {
self.execute_inner::<T, ()>(method, url, None).await
}
/// Execute a request whose body is a `Serialize`-able value.
pub async fn execute_with_body<T, B>(&self, method: Method, url: Url, body: &B) -> Result<T>
where
T: DeserializeOwned,
B: Serialize + ?Sized,
{
self.execute_inner::<T, B>(method, url, Some(body)).await
}
async fn execute_inner<T, B>(&self, method: Method, url: Url, body: Option<&B>) -> Result<T>
where
T: DeserializeOwned,
B: Serialize + ?Sized,
{
let max_attempts = if is_idempotent_method(&method) {
self.retry.max_attempts.max(1)
} else {
1
};
let mut last_err: Option<ApiError> = None;
for attempt in 1..=max_attempts {
#[cfg(feature = "tracing")]
let started = std::time::Instant::now();
let mut req = self.client.request(method.clone(), url.clone());
if let Some(b) = body {
req = req.json(b);
}
let send_result = req.send().await;
let response = match send_result {
Ok(r) => r,
Err(e) => {
let err = ApiError::from(e);
if attempt < max_attempts && err.is_retryable() {
let delay = self.retry.backoff_for(attempt);
#[cfg(feature = "tracing")]
tracing::warn!(
attempt,
next_delay_ms = delay.as_millis() as u64,
error = %err,
"transport error, retrying"
);
tokio::time::sleep(delay).await;
last_err = Some(err);
continue;
}
return Err(err);
}
};
let status = response.status();
#[cfg(feature = "tracing")]
tracing::debug!(
method = %method,
path = %url.path(),
status = status.as_u16(),
elapsed_ms = started.elapsed().as_millis() as u64,
"cnb api call"
);
if status.is_success() {
return decode_response::<T>(response, status).await;
}
// Non-success: try to extract a structured body for the error.
let retry_after = parse_retry_after(&response);
let api_err = build_http_error(response, status).await;
// Honour Retry-After even before checking is_retryable, because the
// server explicitly told us to wait.
if attempt < max_attempts && api_err.is_retryable() {
let delay = retry_after
.unwrap_or_else(|| self.retry.backoff_for(attempt))
.min(self.retry.max_delay.max(Duration::from_secs(60)));
#[cfg(feature = "tracing")]
tracing::warn!(
attempt,
status = status.as_u16(),
next_delay_ms = delay.as_millis() as u64,
"retryable HTTP error, backing off"
);
tokio::time::sleep(delay).await;
last_err = Some(api_err);
continue;
}
return Err(api_err);
}
// The loop only exits via `return` or by exhausting all attempts; in
// the latter case `last_err` is always populated.
Err(last_err.unwrap_or_else(|| ApiError::EnvVar("retry loop exited unexpectedly".into())))
}
}
/// Decode the success body. We treat empty 2xx bodies (e.g. 204) as
/// `serde_json::Value::Null` for `Value` callers, and as a JSON parse error for
/// strongly-typed callers (which is the correct behaviour — the caller asked
/// for a typed payload that the server did not provide).
async fn decode_response<T: DeserializeOwned>(response: Response, status: StatusCode) -> Result<T> {
let bytes = response.bytes().await?;
if bytes.is_empty() {
// Try to deserialise from `null` — works for `Option<_>` and `Value`,
// fails for everything else with a clear JSON error.
return serde_json::from_slice::<T>(b"null").map_err(|e| {
// Wrap into a more descriptive error: the server told us 2xx but
// gave us nothing.
let _ = status;
ApiError::from(e)
});
}
serde_json::from_slice::<T>(&bytes).map_err(ApiError::from)
}
/// Build an [`ApiError::Http`] from a non-2xx response. Always preserves the
/// raw body, even when JSON parsing fails.
async fn build_http_error(response: Response, status: StatusCode) -> ApiError {
// Preserve `X-Request-Id` if present in headers — useful even when body is
// empty or non-JSON.
let header_request_id = response
.headers()
.get("x-request-id")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let body: String = response.text().await.unwrap_or_default();
let envelope = if body.is_empty() {
ErrorEnvelope::default()
} else {
ErrorEnvelope::parse(&body)
};
let message = envelope
.message
.clone()
.unwrap_or_else(|| status.canonical_reason().unwrap_or("error").to_string());
let request_id = envelope.request_id.clone().or(header_request_id);
ApiError::Http {
status: status.as_u16(),
body,
code: envelope.code,
message,
request_id,
}
}
/// Parse a `Retry-After` header. Supports the `delta-seconds` form only —
/// the HTTP-date form is uncommon for CNB and would require a full date parser.
fn parse_retry_after(response: &Response) -> Option<Duration> {
response
.headers()
.get(reqwest::header::RETRY_AFTER)
.and_then(|v| v.to_str().ok())
.and_then(|s| s.trim().parse::<u64>().ok())
.map(Duration::from_secs)
}