use TryIntoHeaderPair for `extra_header`

This commit is contained in:
Rob Ede 2021-12-28 02:34:21 +00:00
parent b824acd726
commit 07472f340a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 43 additions and 67 deletions

View File

@ -1,6 +1,11 @@
# Changes
## Unreleased - 2021-xx-xx
- `*::send_json` and `*::send_form` methods now receive `impl Serialize`. [#2553]
- `FrozenClientRequest::extra_header` now uses receives an `impl TryIntoHeaderPair`. [#2553]
- Remove unnecessary `Unpin` bounds on `*::send_stream`. [#2553]
[#2553]: https://github.com/actix/actix-web/pull/2553
## 3.0.0-beta.15 - 2021-12-27

View File

@ -1,4 +1,4 @@
use std::{convert::TryFrom, net, rc::Rc, time::Duration};
use std::{net, rc::Rc, time::Duration};
use bytes::Bytes;
use futures_core::Stream;
@ -7,7 +7,7 @@ use serde::Serialize;
use actix_http::{
body::MessageBody,
error::HttpError,
header::{HeaderMap, HeaderName, TryIntoHeaderValue},
header::{HeaderMap, TryIntoHeaderPair},
Method, RequestHead, Uri,
};
@ -18,6 +18,7 @@ use crate::{
};
/// `FrozenClientRequest` struct represents cloneable client request.
///
/// It could be used to send same request multiple times.
#[derive(Clone)]
pub struct FrozenClientRequest {
@ -83,7 +84,7 @@ impl FrozenClientRequest {
/// Send a streaming body.
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
RequestSender::Rc(self.head.clone(), None).send_stream(
@ -105,20 +106,14 @@ impl FrozenClientRequest {
)
}
/// Create a `FrozenSendBuilder` with extra headers
/// Clones this `FrozenClientRequest`, returning a new one with extra headers added.
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
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: TryIntoHeaderValue,
{
self.extra_headers(HeaderMap::new())
.extra_header(key, value)
/// 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)
}
}
@ -139,29 +134,20 @@ impl FrozenSendBuilder {
}
/// Insert a header, it overrides existing header in `FrozenClientRequest`.
pub fn extra_header<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: TryIntoHeaderValue,
{
match HeaderName::try_from(key) {
Ok(key) => match value.try_into_value() {
Ok(value) => {
self.extra_headers.insert(key, value);
}
Err(e) => self.err = Some(e.into()),
},
Err(e) => self.err = Some(e.into()),
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()),
}
self
}
/// Complete request construction and send a body.
pub fn send_body<B>(self, body: B) -> SendClientRequest
where
B: MessageBody + 'static,
{
pub fn send_body(self, body: impl MessageBody + 'static) -> SendClientRequest {
if let Some(e) = self.err {
return e.into();
}
@ -176,9 +162,9 @@ impl FrozenSendBuilder {
}
/// 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();
pub fn send_json(self, value: impl Serialize) -> SendClientRequest {
if let Some(err) = self.err {
return err.into();
}
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_json(
@ -191,7 +177,7 @@ impl FrozenSendBuilder {
}
/// Complete request construction and send an urlencoded body.
pub fn send_form<T: Serialize>(self, value: &T) -> SendClientRequest {
pub fn send_form(self, value: impl Serialize) -> SendClientRequest {
if let Some(e) = self.err {
return e.into();
}
@ -208,7 +194,7 @@ impl FrozenSendBuilder {
/// Complete request construction and send a streaming body.
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
if let Some(e) = self.err {

View File

@ -385,7 +385,7 @@ impl ClientRequest {
/// Set an streaming body and generate `ClientRequest`.
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
let slf = match self.prep_for_sending() {

View File

@ -181,17 +181,14 @@ pub(crate) enum RequestSender {
}
impl RequestSender {
pub(crate) fn send_body<B>(
pub(crate) fn send_body(
self,
addr: Option<net::SocketAddr>,
response_decompress: bool,
timeout: Option<Duration>,
config: &ClientConfig,
body: B,
) -> SendClientRequest
where
B: MessageBody + 'static,
{
body: impl MessageBody + 'static,
) -> SendClientRequest {
let req = match self {
RequestSender::Owned(head) => ConnectRequest::Client(
RequestHeadType::Owned(head),
@ -210,15 +207,15 @@ impl RequestSender {
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
}
pub(crate) fn send_json<T: Serialize>(
pub(crate) fn send_json(
mut self,
addr: Option<net::SocketAddr>,
response_decompress: bool,
timeout: Option<Duration>,
config: &ClientConfig,
value: &T,
value: impl Serialize,
) -> SendClientRequest {
let body = match serde_json::to_string(value) {
let body = match serde_json::to_string(&value) {
Ok(body) => body,
Err(err) => return PrepForSendingError::Json(err).into(),
};
@ -227,22 +224,16 @@ impl RequestSender {
return e.into();
}
self.send_body(
addr,
response_decompress,
timeout,
config,
AnyBody::from_message_body(body.into_bytes()),
)
self.send_body(addr, response_decompress, timeout, config, body)
}
pub(crate) fn send_form<T: Serialize>(
pub(crate) fn send_form(
mut self,
addr: Option<net::SocketAddr>,
response_decompress: bool,
timeout: Option<Duration>,
config: &ClientConfig,
value: &T,
value: impl Serialize,
) -> SendClientRequest {
let body = match serde_urlencoded::to_string(value) {
Ok(body) => body,
@ -250,19 +241,13 @@ impl RequestSender {
};
// set content-type
if let Err(e) =
if let Err(err) =
self.set_header_if_none(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
{
return e.into();
return err.into();
}
self.send_body(
addr,
response_decompress,
timeout,
config,
AnyBody::from_message_body(body.into_bytes()),
)
self.send_body(addr, response_decompress, timeout, config, body)
}
pub(crate) fn send_stream<S, E>(
@ -274,7 +259,7 @@ impl RequestSender {
stream: S,
) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
self.send_body(
@ -282,7 +267,7 @@ impl RequestSender {
response_decompress,
timeout,
config,
AnyBody::new_boxed(BodyStream::new(stream)),
BodyStream::new(stream),
)
}
@ -293,7 +278,7 @@ impl RequestSender {
timeout: Option<Duration>,
config: &ClientConfig,
) -> SendClientRequest {
self.send_body(addr, response_decompress, timeout, config, AnyBody::empty())
self.send_body(addr, response_decompress, timeout, config, ())
}
fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError>