Clean up reqwest::Response to http::Response conversion

next
Jonas Platte 2021-04-29 20:58:05 +02:00
parent c28eba1e5b
commit 8bfaf09f37
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
3 changed files with 49 additions and 50 deletions

View File

@ -5,6 +5,7 @@ use ruma::api::{IncomingResponse, OutgoingRequest, SendAccessToken};
use std::{
convert::{TryFrom, TryInto},
fmt::Debug,
mem,
time::Duration,
};
@ -45,22 +46,21 @@ where
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
let url = reqwest_request.url().clone();
let mut reqwest_response = globals.reqwest_client().execute(reqwest_request).await?;
let mut response = globals.reqwest_client().execute(reqwest_request).await?;
// Because reqwest::Response -> http::Response is complicated:
let status = reqwest_response.status();
let mut http_response = http::Response::builder().status(status);
let headers = http_response.headers_mut().unwrap();
// reqwest::Response -> http::Response conversion
let status = response.status();
let mut http_response_builder = http::Response::builder()
.status(status)
.version(response.version());
mem::swap(
response.headers_mut(),
http_response_builder
.headers_mut()
.expect("http::response::Builder is usable"),
);
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| {
let body = response.bytes().await.unwrap_or_else(|e| {
warn!("server error: {}", e);
Vec::new().into()
}); // TODO: handle timeout
@ -76,7 +76,7 @@ where
}
let response = T::IncomingResponse::try_from_http_response(
http_response
http_response_builder
.body(body)
.expect("reqwest body is valid http body"),
);

View File

@ -16,7 +16,7 @@ use ruma::{
};
use sled::IVec;
use std::{convert::TryFrom, fmt::Debug};
use std::{convert::TryFrom, fmt::Debug, mem};
#[derive(Debug, Clone)]
pub struct PushData {
@ -114,24 +114,23 @@ where
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
let url = reqwest_request.url().clone();
let reqwest_response = globals.reqwest_client().execute(reqwest_request).await;
let 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();
match response {
Ok(mut response) => {
// reqwest::Response -> http::Response conversion
let status = response.status();
let mut http_response_builder = http::Response::builder()
.status(status)
.version(response.version());
mem::swap(
response.headers_mut(),
http_response_builder
.headers_mut()
.expect("http::response::Builder is usable"),
);
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| {
let body = response.bytes().await.unwrap_or_else(|e| {
warn!("server error {}", e);
Vec::new().into()
}); // TODO: handle timeout
@ -147,7 +146,7 @@ where
}
let response = T::IncomingResponse::try_from_http_response(
http_response
http_response_builder
.body(body)
.expect("reqwest body is valid http body"),
);

View File

@ -43,6 +43,7 @@ use std::{
convert::{TryFrom, TryInto},
fmt::Debug,
future::Future,
mem,
net::{IpAddr, SocketAddr},
pin::Pin,
result::Result as StdResult,
@ -219,24 +220,23 @@ 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 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();
match response {
Ok(mut response) => {
// reqwest::Response -> http::Response conversion
let status = response.status();
let mut http_response_builder = http::Response::builder()
.status(status)
.version(response.version());
mem::swap(
response.headers_mut(),
http_response_builder
.headers_mut()
.expect("http::response::Builder is usable"),
);
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| {
let body = response.bytes().await.unwrap_or_else(|e| {
warn!("server error {}", e);
Vec::new().into()
}); // TODO: handle timeout
@ -254,7 +254,7 @@ where
}
let response = T::IncomingResponse::try_from_http_response(
http_response
http_response_builder
.body(body)
.expect("reqwest body is valid http body"),
);