2021-12-28 04:22:22 +01:00
|
|
|
use std::{net, rc::Rc, time::Duration};
|
2019-09-12 06:40:56 +02:00
|
|
|
|
|
|
|
use bytes::Bytes;
|
2019-12-13 06:24:57 +01:00
|
|
|
use futures_core::Stream;
|
2019-09-12 06:40:56 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
use actix_http::{
|
2021-12-25 03:33:37 +01:00
|
|
|
body::MessageBody,
|
2021-12-05 15:37:20 +01:00
|
|
|
error::HttpError,
|
2021-12-28 04:22:22 +01:00
|
|
|
header::{HeaderMap, TryIntoHeaderPair},
|
2021-12-05 15:37:20 +01:00
|
|
|
Method, RequestHead, Uri,
|
2021-06-17 18:57:58 +02:00
|
|
|
};
|
2019-09-12 06:40:56 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
use crate::{
|
2021-12-25 03:23:22 +01:00
|
|
|
client::ClientConfig,
|
2021-06-17 18:57:58 +02:00
|
|
|
sender::{RequestSender, SendClientRequest},
|
2021-12-25 03:23:22 +01:00
|
|
|
BoxError,
|
2021-06-17 18:57:58 +02:00
|
|
|
};
|
2019-09-12 06:40:56 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
/// `FrozenClientRequest` struct represents cloneable client request.
|
2021-12-28 04:22:22 +01:00
|
|
|
///
|
2019-09-12 06:40:56 +02:00
|
|
|
/// 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 18:53:22 +01:00
|
|
|
pub(crate) config: ClientConfig,
|
2019-09-12 06:40:56 +02: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
|
2021-12-25 03:33:37 +01:00
|
|
|
B: MessageBody + 'static,
|
2019-09-12 06:40:56 +02:00
|
|
|
{
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send_body(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 18:53:22 +01:00
|
|
|
&self.config,
|
2019-09-12 06:40:56 +02: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 18:53:22 +01:00
|
|
|
&self.config,
|
2019-09-12 06:40:56 +02: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 18:53:22 +01:00
|
|
|
&self.config,
|
2019-09-12 06:40:56 +02:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a streaming body.
|
|
|
|
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
|
|
|
|
where
|
2021-12-28 04:22:22 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
2021-12-04 20:40:47 +01:00
|
|
|
E: Into<BoxError> + 'static,
|
2019-09-12 06:40:56 +02:00
|
|
|
{
|
|
|
|
RequestSender::Rc(self.head.clone(), None).send_stream(
|
|
|
|
self.addr,
|
|
|
|
self.response_decompress,
|
|
|
|
self.timeout,
|
2021-03-18 18:53:22 +01:00
|
|
|
&self.config,
|
2019-09-12 06:40:56 +02: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 18:53:22 +01:00
|
|
|
&self.config,
|
2019-09-12 06:40:56 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-28 04:22:22 +01:00
|
|
|
/// Clones this `FrozenClientRequest`, returning a new one with extra headers added.
|
2019-09-12 06:40:56 +02:00
|
|
|
pub fn extra_headers(&self, extra_headers: HeaderMap) -> FrozenSendBuilder {
|
|
|
|
FrozenSendBuilder::new(self.clone(), extra_headers)
|
|
|
|
}
|
|
|
|
|
2021-12-28 04:22:22 +01:00
|
|
|
/// Clones this `FrozenClientRequest`, returning a new one with the extra header added.
|
|
|
|
pub fn extra_header(&self, header: impl TryIntoHeaderPair) -> FrozenSendBuilder {
|
|
|
|
self.extra_headers(HeaderMap::new()).extra_header(header)
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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`.
|
2021-12-28 04:22:22 +01:00
|
|
|
pub fn extra_header(mut self, header: impl TryIntoHeaderPair) -> Self {
|
|
|
|
match header.try_into_pair() {
|
|
|
|
Ok((key, value)) => {
|
|
|
|
self.extra_headers.insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(err) => self.err = Some(err.into()),
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
2021-12-28 04:22:22 +01:00
|
|
|
|
2019-09-12 06:40:56 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send a body.
|
2021-12-28 04:22:22 +01:00
|
|
|
pub fn send_body(self, body: impl MessageBody + 'static) -> SendClientRequest {
|
2019-09-12 06:40:56 +02:00
|
|
|
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 18:53:22 +01:00
|
|
|
&self.req.config,
|
2019-09-12 06:40:56 +02:00
|
|
|
body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send a json body.
|
2021-12-28 04:22:22 +01:00
|
|
|
pub fn send_json(self, value: impl Serialize) -> SendClientRequest {
|
|
|
|
if let Some(err) = self.err {
|
|
|
|
return err.into();
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_json(
|
|
|
|
self.req.addr,
|
|
|
|
self.req.response_decompress,
|
|
|
|
self.req.timeout,
|
2021-03-18 18:53:22 +01:00
|
|
|
&self.req.config,
|
2019-09-12 06:40:56 +02:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send an urlencoded body.
|
2021-12-28 04:22:22 +01:00
|
|
|
pub fn send_form(self, value: impl Serialize) -> SendClientRequest {
|
2019-09-12 06:40:56 +02:00
|
|
|
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 18:53:22 +01:00
|
|
|
&self.req.config,
|
2019-09-12 06:40:56 +02:00
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Complete request construction and send a streaming body.
|
|
|
|
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
|
|
|
|
where
|
2021-12-28 04:22:22 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
2021-12-04 20:40:47 +01:00
|
|
|
E: Into<BoxError> + 'static,
|
2019-09-12 06:40:56 +02:00
|
|
|
{
|
|
|
|
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 18:53:22 +01:00
|
|
|
&self.req.config,
|
2019-09-12 06:40:56 +02: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 18:53:22 +01:00
|
|
|
&self.req.config,
|
2019-09-12 06:40:56 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|