Skip to content

Commit 814544b

Browse files
committed
Extract send_and_parse helper method to deduplicate HTTP request handling logic in FacilitatorClient
* Extract common request sending and JSON parsing logic into new send_and_parse() method in r402-http/src/server/facilitator_client.rs * Refactor post_json() to build request and delegate to send_and_parse() instead of duplicating header/timeout/error handling * Refactor get_json() to build request and delegate to send_and_parse() instead of duplicating header/timeout/error handling * Update get
1 parent 5b34c21 commit 814544b

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

r402-http/src/server/facilitator_client.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -403,43 +403,12 @@ impl FacilitatorClient {
403403
T: serde::Serialize + Sync + ?Sized,
404404
R: serde::de::DeserializeOwned,
405405
{
406-
let mut req = self.client.post(url.clone()).json(payload);
407-
for (key, value) in &self.headers {
408-
req = req.header(key, value);
409-
}
410-
if let Some(timeout) = self.timeout {
411-
req = req.timeout(timeout);
412-
}
413-
let http_response = req
414-
.send()
415-
.await
416-
.map_err(|e| FacilitatorClientError::Http { context, source: e })?;
417-
418-
let result = if http_response.status() == StatusCode::OK {
419-
http_response
420-
.json::<R>()
421-
.await
422-
.map_err(|e| FacilitatorClientError::JsonDeserialization { context, source: e })
423-
} else {
424-
let status = http_response.status();
425-
let body = http_response
426-
.text()
427-
.await
428-
.map_err(|e| FacilitatorClientError::ResponseBodyRead { context, source: e })?;
429-
Err(FacilitatorClientError::HttpStatus {
430-
context,
431-
status,
432-
body,
433-
})
434-
};
435-
436-
record_result_on_span(&result);
437-
438-
result
406+
let req = self.client.post(url.clone()).json(payload);
407+
self.send_and_parse(req, context).await
439408
}
440409

441-
/// Generic GET helper that handles JSON serialization, error mapping,
442-
/// timeout application, and telemetry integration.
410+
/// Generic GET helper that handles error mapping, timeout application,
411+
/// and telemetry integration.
443412
///
444413
/// `context` is a human-readable identifier used in tracing and error messages (e.g. `"GET /supported"`).
445414
async fn get_json<R>(
@@ -450,7 +419,19 @@ impl FacilitatorClient {
450419
where
451420
R: serde::de::DeserializeOwned,
452421
{
453-
let mut req = self.client.get(url.clone());
422+
let req = self.client.get(url.clone());
423+
self.send_and_parse(req, context).await
424+
}
425+
426+
/// Applies headers, timeout, sends the request, and parses the JSON response.
427+
async fn send_and_parse<R>(
428+
&self,
429+
mut req: reqwest::RequestBuilder,
430+
context: &'static str,
431+
) -> Result<R, FacilitatorClientError>
432+
where
433+
R: serde::de::DeserializeOwned,
434+
{
454435
for (key, value) in &self.headers {
455436
req = req.header(key, value);
456437
}

0 commit comments

Comments
 (0)