Clean up reqwest::Response to http::Response conversion
This commit is contained in:
parent
c28eba1e5b
commit
8bfaf09f37
3 changed files with 49 additions and 50 deletions
|
@ -5,6 +5,7 @@ use ruma::api::{IncomingResponse, OutgoingRequest, SendAccessToken};
|
||||||
use std::{
|
use std::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
|
mem,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -45,22 +46,21 @@ where
|
||||||
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
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:
|
// reqwest::Response -> http::Response conversion
|
||||||
let status = reqwest_response.status();
|
let status = response.status();
|
||||||
let mut http_response = http::Response::builder().status(status);
|
let mut http_response_builder = http::Response::builder()
|
||||||
let headers = http_response.headers_mut().unwrap();
|
.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() {
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
||||||
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);
|
warn!("server error: {}", e);
|
||||||
Vec::new().into()
|
Vec::new().into()
|
||||||
}); // TODO: handle timeout
|
}); // TODO: handle timeout
|
||||||
|
@ -76,7 +76,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = T::IncomingResponse::try_from_http_response(
|
let response = T::IncomingResponse::try_from_http_response(
|
||||||
http_response
|
http_response_builder
|
||||||
.body(body)
|
.body(body)
|
||||||
.expect("reqwest body is valid http body"),
|
.expect("reqwest body is valid http body"),
|
||||||
);
|
);
|
||||||
|
|
|
@ -16,7 +16,7 @@ use ruma::{
|
||||||
};
|
};
|
||||||
use sled::IVec;
|
use sled::IVec;
|
||||||
|
|
||||||
use std::{convert::TryFrom, fmt::Debug};
|
use std::{convert::TryFrom, fmt::Debug, mem};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PushData {
|
pub struct PushData {
|
||||||
|
@ -114,24 +114,23 @@ where
|
||||||
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
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 response {
|
||||||
match reqwest_response {
|
Ok(mut response) => {
|
||||||
Ok(mut reqwest_response) => {
|
// reqwest::Response -> http::Response conversion
|
||||||
let status = reqwest_response.status();
|
let status = response.status();
|
||||||
let mut http_response = http::Response::builder().status(status);
|
let mut http_response_builder = http::Response::builder()
|
||||||
let headers = http_response.headers_mut().unwrap();
|
.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() {
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
||||||
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);
|
warn!("server error {}", e);
|
||||||
Vec::new().into()
|
Vec::new().into()
|
||||||
}); // TODO: handle timeout
|
}); // TODO: handle timeout
|
||||||
|
@ -147,7 +146,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = T::IncomingResponse::try_from_http_response(
|
let response = T::IncomingResponse::try_from_http_response(
|
||||||
http_response
|
http_response_builder
|
||||||
.body(body)
|
.body(body)
|
||||||
.expect("reqwest body is valid http body"),
|
.expect("reqwest body is valid http body"),
|
||||||
);
|
);
|
||||||
|
|
|
@ -43,6 +43,7 @@ use std::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
future::Future,
|
future::Future,
|
||||||
|
mem,
|
||||||
net::{IpAddr, SocketAddr},
|
net::{IpAddr, SocketAddr},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
result::Result as StdResult,
|
result::Result as StdResult,
|
||||||
|
@ -219,24 +220,23 @@ where
|
||||||
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
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 response {
|
||||||
match reqwest_response {
|
Ok(mut response) => {
|
||||||
Ok(mut reqwest_response) => {
|
// reqwest::Response -> http::Response conversion
|
||||||
let status = reqwest_response.status();
|
let status = response.status();
|
||||||
let mut http_response = http::Response::builder().status(status);
|
let mut http_response_builder = http::Response::builder()
|
||||||
let headers = http_response.headers_mut().unwrap();
|
.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() {
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
||||||
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);
|
warn!("server error {}", e);
|
||||||
Vec::new().into()
|
Vec::new().into()
|
||||||
}); // TODO: handle timeout
|
}); // TODO: handle timeout
|
||||||
|
@ -254,7 +254,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = T::IncomingResponse::try_from_http_response(
|
let response = T::IncomingResponse::try_from_http_response(
|
||||||
http_response
|
http_response_builder
|
||||||
.body(body)
|
.body(body)
|
||||||
.expect("reqwest body is valid http body"),
|
.expect("reqwest body is valid http body"),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue