2019-12-05 17:35:43 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-09-12 04:40:56 +00:00
|
|
|
use std::net;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use bytes::Bytes;
|
2019-12-13 05:24:57 +00:00
|
|
|
use futures_core::Stream;
|
2019-09-12 04:40:56 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use actix_http::body::Body;
|
|
|
|
use actix_http::http::header::IntoHeaderValue;
|
2019-12-05 17:35:43 +00:00
|
|
|
use actix_http::http::{Error as HttpError, HeaderMap, HeaderName, Method, Uri};
|
2019-09-12 04:40:56 +00:00
|
|
|
use actix_http::{Error, RequestHead};
|
|
|
|
|
|
|
|
use crate::sender::{RequestSender, SendClientRequest};
|
|
|
|
use crate::ClientConfig;
|
|
|
|
|
|
|
|
/// `FrozenClientRequest` struct represents clonable client request.
|
|
|
|
/// It could be used to send same request multiple times.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FrozenClientRequest {
|
|
|
|
pub(crate) head: Rc<RequestHead>,
|
|
|
|
pub(crate) addr: Option<net::SocketAddr>,
|
|
|
|
pub(crate) response_decompress: bool,
|
|
|
|
pub(crate) timeout: Option<Duration>,
|
2021-03-18 17:53:22 +00:00
|
|
|
pub(crate) config: ClientConfig,
|
2019-09-12 04:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FrozenClientRequest {
|
|
|
|
/// Get HTTP URI of request
|
|
|
|
pub fn get_uri(&self) -> &Uri {
|
|
|
|
&self.head.uri
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get HTTP method of this request
|
|
|
|
pub fn get_method(&self) -> &Method {
|
|
|
|
&self.head.method
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns request's headers.
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
|
|
|
&self.head.headers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a body.
|
|
|
|
pub fn send_body<B>(&self, body: B) -> SendClientRequest
|
|
|
|
where
|
|
|
|
B: Into<Body>,
|
|
|
|
{
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send_body(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a json body.
|
|
|
|
pub fn send_json<T: Serialize>(&self, value: &T) -> SendClientRequest {
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send_json(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send an urlencoded body.
|
|
|
|
pub fn send_form<T: Serialize>(&self, value: &T) -> SendClientRequest {
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send_form(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a streaming body.
|
|
|
|
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
|
|
|
|
where
|
2019-11-19 03:55:17 +00:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
2019-09-12 04:40:56 +00:00
|
|
|
E: Into<Error> + 'static,
|
|
|
|
{
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send_stream(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
stream,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send an empty body.
|
|
|
|
pub fn send(&self) -> SendClientRequest {
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a `FrozenSendBuilder` with extra headers
|
|
|
|
pub fn extra_headers(&self, extra_headers: HeaderMap) -> FrozenSendBuilder {
|
|
|
|
FrozenSendBuilder::new(self.clone(), extra_headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a `FrozenSendBuilder` with an extra header
|
|
|
|
pub fn extra_header<K, V>(&self, key: K, value: V) -> FrozenSendBuilder
|
|
|
|
where
|
2019-12-05 17:35:43 +00:00
|
|
|
HeaderName: TryFrom<K>,
|
|
|
|
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
2019-09-12 04:40:56 +00:00
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
|
|
|
self.extra_headers(HeaderMap::new())
|
|
|
|
.extra_header(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder that allows to modify extra headers.
|
|
|
|
pub struct FrozenSendBuilder {
|
|
|
|
req: FrozenClientRequest,
|
|
|
|
extra_headers: HeaderMap,
|
|
|
|
err: Option<HttpError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FrozenSendBuilder {
|
|
|
|
pub(crate) fn new(req: FrozenClientRequest, extra_headers: HeaderMap) -> Self {
|
|
|
|
Self {
|
|
|
|
req,
|
|
|
|
extra_headers,
|
|
|
|
err: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a header, it overrides existing header in `FrozenClientRequest`.
|
|
|
|
pub fn extra_header<K, V>(mut self, key: K, value: V) -> Self
|
|
|
|
where
|
2019-12-05 17:35:43 +00:00
|
|
|
HeaderName: TryFrom<K>,
|
|
|
|
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
2019-09-12 04:40:56 +00:00
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
|
|
|
match HeaderName::try_from(key) {
|
2021-01-15 02:11:10 +00:00
|
|
|
Ok(key) => match value.try_into_value() {
|
2021-02-09 22:59:17 +00:00
|
|
|
Ok(value) => {
|
|
|
|
self.extra_headers.insert(key, value);
|
|
|
|
}
|
2019-09-12 04:40:56 +00:00
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
},
|
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send a body.
|
|
|
|
pub fn send_body<B>(self, body: B) -> SendClientRequest
|
|
|
|
where
|
|
|
|
B: Into<Body>,
|
|
|
|
{
|
|
|
|
if let Some(e) = self.err {
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_body(
|
|
|
|
self.req.addr,
|
|
|
|
self.req.response_decompress,
|
|
|
|
self.req.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.req.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send a json body.
|
|
|
|
pub fn send_json<T: Serialize>(self, value: &T) -> SendClientRequest {
|
|
|
|
if let Some(e) = self.err {
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_json(
|
|
|
|
self.req.addr,
|
|
|
|
self.req.response_decompress,
|
|
|
|
self.req.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.req.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send an urlencoded body.
|
|
|
|
pub fn send_form<T: Serialize>(self, value: &T) -> SendClientRequest {
|
|
|
|
if let Some(e) = self.err {
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_form(
|
|
|
|
self.req.addr,
|
|
|
|
self.req.response_decompress,
|
|
|
|
self.req.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.req.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send a streaming body.
|
|
|
|
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
|
|
|
|
where
|
2019-11-19 03:55:17 +00:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
2019-09-12 04:40:56 +00:00
|
|
|
E: Into<Error> + 'static,
|
|
|
|
{
|
|
|
|
if let Some(e) = self.err {
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_stream(
|
|
|
|
self.req.addr,
|
|
|
|
self.req.response_decompress,
|
|
|
|
self.req.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.req.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
stream,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send an empty body.
|
|
|
|
pub fn send(self) -> SendClientRequest {
|
|
|
|
if let Some(e) = self.err {
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send(
|
|
|
|
self.req.addr,
|
|
|
|
self.req.response_decompress,
|
|
|
|
self.req.timeout,
|
2021-03-18 17:53:22 +00:00
|
|
|
&self.req.config,
|
2019-09-12 04:40:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|