From e72fd44bb5b61303b42763be61c7c9a218b6e718 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 23 Apr 2021 18:38:20 +0200 Subject: [PATCH] Refactor send_request for appservices --- src/appservice_server.rs | 79 +++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/src/appservice_server.rs b/src/appservice_server.rs index 1b72c764..9220c2d1 100644 --- a/src/appservice_server.rs +++ b/src/appservice_server.rs @@ -49,51 +49,46 @@ where *reqwest_request.timeout_mut() = Some(Duration::from_secs(30)); let url = reqwest_request.url().clone(); - let reqwest_response = globals.reqwest_client().execute(reqwest_request).await; + let mut reqwest_response = globals.reqwest_client().execute(reqwest_request).await?; // Because reqwest::Response -> http::Response is complicated: - match reqwest_response { - Ok(mut reqwest_response) => { - let status = reqwest_response.status(); - let mut http_response = http::Response::builder().status(status); - let headers = http_response.headers_mut().unwrap(); + let status = reqwest_response.status(); + let mut http_response = http::Response::builder().status(status); + let headers = http_response.headers_mut().unwrap(); - for (k, v) in reqwest_response.headers_mut().drain() { - if let Some(key) = k { - headers.insert(key, v); - } - } - - let status = reqwest_response.status(); - - let body = reqwest_response.bytes().await.unwrap_or_else(|e| { - warn!("server error: {}", e); - Vec::new().into() - }); // TODO: handle timeout - - if status != 200 { - warn!( - "Appservice returned bad response {} {}\n{}\n{:?}", - destination, - status, - url, - utils::string_from_bytes(&body) - ); - } - - let response = T::IncomingResponse::try_from_http_response( - http_response - .body(body) - .expect("reqwest body is valid http body"), - ); - response.map_err(|_| { - warn!( - "Appservice returned invalid response bytes {}\n{}", - destination, url - ); - Error::BadServerResponse("Server returned bad response.") - }) + for (k, v) in reqwest_response.headers_mut().drain() { + if let Some(key) = k { + headers.insert(key, v); } - Err(e) => Err(e.into()), } + + let status = reqwest_response.status(); + + let body = reqwest_response.bytes().await.unwrap_or_else(|e| { + warn!("server error: {}", e); + Vec::new().into() + }); // TODO: handle timeout + + if status != 200 { + warn!( + "Appservice returned bad response {} {}\n{}\n{:?}", + destination, + status, + url, + utils::string_from_bytes(&body) + ); + } + + let response = T::IncomingResponse::try_from_http_response( + http_response + .body(body) + .expect("reqwest body is valid http body"), + ); + response.map_err(|_| { + warn!( + "Appservice returned invalid response bytes {}\n{}", + destination, url + ); + Error::BadServerResponse("Server returned bad response.") + }) }