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
#![doc = include_str!("../README.md")]
use actix_web::http::StatusCode;
use actix_web::{dev, HttpResponse, ResponseError};
use awc::error::{
ConnectError, SendRequestError as AwcSendRequestError,
};
use awc::ClientResponse;
use std::fmt;
#[derive(Debug)]
pub struct SendRequestError(AwcSendRequestError);
impl fmt::Display for SendRequestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<AwcSendRequestError> for SendRequestError {
fn from(e: AwcSendRequestError) -> Self {
Self(e)
}
}
impl ResponseError for SendRequestError {
fn status_code(&self) -> StatusCode {
match self.0 {
AwcSendRequestError::Connect(ConnectError::Timeout) => {
StatusCode::GATEWAY_TIMEOUT
}
AwcSendRequestError::Connect(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
pub trait IntoHttpResponse {
fn into_http_response(self) -> HttpResponse;
fn into_wrapped_http_response<E>(self) -> Result<HttpResponse, E>
where
Self: Sized,
{
Ok(self.into_http_response())
}
}
impl IntoHttpResponse
for ClientResponse<dev::Decompress<dev::Payload>>
{
fn into_http_response(self) -> HttpResponse {
let mut response = HttpResponse::build(self.status());
self.headers().into_iter().for_each(|(k, v)| {
response.insert_header((k, v));
});
response.streaming(self)
}
}