mirror of https://github.com/fafhrd91/actix-web
use TryIntoHeaderPair for `extra_header`
This commit is contained in:
parent
b824acd726
commit
07472f340a
|
@ -1,6 +1,11 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## 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
|
## 3.0.0-beta.15 - 2021-12-27
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{convert::TryFrom, net, rc::Rc, time::Duration};
|
use std::{net, rc::Rc, time::Duration};
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
@ -7,7 +7,7 @@ use serde::Serialize;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::MessageBody,
|
body::MessageBody,
|
||||||
error::HttpError,
|
error::HttpError,
|
||||||
header::{HeaderMap, HeaderName, TryIntoHeaderValue},
|
header::{HeaderMap, TryIntoHeaderPair},
|
||||||
Method, RequestHead, Uri,
|
Method, RequestHead, Uri,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `FrozenClientRequest` struct represents cloneable client request.
|
/// `FrozenClientRequest` struct represents cloneable client request.
|
||||||
|
///
|
||||||
/// It could be used to send same request multiple times.
|
/// It could be used to send same request multiple times.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FrozenClientRequest {
|
pub struct FrozenClientRequest {
|
||||||
|
@ -83,7 +84,7 @@ impl FrozenClientRequest {
|
||||||
/// Send a streaming body.
|
/// Send a streaming body.
|
||||||
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
|
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||||
E: Into<BoxError> + 'static,
|
E: Into<BoxError> + 'static,
|
||||||
{
|
{
|
||||||
RequestSender::Rc(self.head.clone(), None).send_stream(
|
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 {
|
pub fn extra_headers(&self, extra_headers: HeaderMap) -> FrozenSendBuilder {
|
||||||
FrozenSendBuilder::new(self.clone(), extra_headers)
|
FrozenSendBuilder::new(self.clone(), extra_headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `FrozenSendBuilder` with an extra header
|
/// Clones this `FrozenClientRequest`, returning a new one with the extra header added.
|
||||||
pub fn extra_header<K, V>(&self, key: K, value: V) -> FrozenSendBuilder
|
pub fn extra_header(&self, header: impl TryIntoHeaderPair) -> FrozenSendBuilder {
|
||||||
where
|
self.extra_headers(HeaderMap::new()).extra_header(header)
|
||||||
HeaderName: TryFrom<K>,
|
|
||||||
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
|
||||||
V: TryIntoHeaderValue,
|
|
||||||
{
|
|
||||||
self.extra_headers(HeaderMap::new())
|
|
||||||
.extra_header(key, value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,29 +134,20 @@ impl FrozenSendBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert a header, it overrides existing header in `FrozenClientRequest`.
|
/// Insert a header, it overrides existing header in `FrozenClientRequest`.
|
||||||
pub fn extra_header<K, V>(mut self, key: K, value: V) -> Self
|
pub fn extra_header(mut self, header: impl TryIntoHeaderPair) -> Self {
|
||||||
where
|
match header.try_into_pair() {
|
||||||
HeaderName: TryFrom<K>,
|
Ok((key, value)) => {
|
||||||
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
self.extra_headers.insert(key, value);
|
||||||
V: TryIntoHeaderValue,
|
}
|
||||||
{
|
|
||||||
match HeaderName::try_from(key) {
|
Err(err) => self.err = Some(err.into()),
|
||||||
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()),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Complete request construction and send a body.
|
/// Complete request construction and send a body.
|
||||||
pub fn send_body<B>(self, body: B) -> SendClientRequest
|
pub fn send_body(self, body: impl MessageBody + 'static) -> SendClientRequest {
|
||||||
where
|
|
||||||
B: MessageBody + 'static,
|
|
||||||
{
|
|
||||||
if let Some(e) = self.err {
|
if let Some(e) = self.err {
|
||||||
return e.into();
|
return e.into();
|
||||||
}
|
}
|
||||||
|
@ -176,9 +162,9 @@ impl FrozenSendBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Complete request construction and send a json body.
|
/// Complete request construction and send a json body.
|
||||||
pub fn send_json<T: Serialize>(self, value: &T) -> SendClientRequest {
|
pub fn send_json(self, value: impl Serialize) -> SendClientRequest {
|
||||||
if let Some(e) = self.err {
|
if let Some(err) = self.err {
|
||||||
return e.into();
|
return err.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_json(
|
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.
|
/// 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 {
|
if let Some(e) = self.err {
|
||||||
return e.into();
|
return e.into();
|
||||||
}
|
}
|
||||||
|
@ -208,7 +194,7 @@ impl FrozenSendBuilder {
|
||||||
/// Complete request construction and send a streaming body.
|
/// Complete request construction and send a streaming body.
|
||||||
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
|
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||||
E: Into<BoxError> + 'static,
|
E: Into<BoxError> + 'static,
|
||||||
{
|
{
|
||||||
if let Some(e) = self.err {
|
if let Some(e) = self.err {
|
||||||
|
|
|
@ -385,7 +385,7 @@ impl ClientRequest {
|
||||||
/// Set an streaming body and generate `ClientRequest`.
|
/// Set an streaming body and generate `ClientRequest`.
|
||||||
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
|
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||||
E: Into<BoxError> + 'static,
|
E: Into<BoxError> + 'static,
|
||||||
{
|
{
|
||||||
let slf = match self.prep_for_sending() {
|
let slf = match self.prep_for_sending() {
|
||||||
|
|
|
@ -181,17 +181,14 @@ pub(crate) enum RequestSender {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RequestSender {
|
impl RequestSender {
|
||||||
pub(crate) fn send_body<B>(
|
pub(crate) fn send_body(
|
||||||
self,
|
self,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
response_decompress: bool,
|
response_decompress: bool,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
config: &ClientConfig,
|
config: &ClientConfig,
|
||||||
body: B,
|
body: impl MessageBody + 'static,
|
||||||
) -> SendClientRequest
|
) -> SendClientRequest {
|
||||||
where
|
|
||||||
B: MessageBody + 'static,
|
|
||||||
{
|
|
||||||
let req = match self {
|
let req = match self {
|
||||||
RequestSender::Owned(head) => ConnectRequest::Client(
|
RequestSender::Owned(head) => ConnectRequest::Client(
|
||||||
RequestHeadType::Owned(head),
|
RequestHeadType::Owned(head),
|
||||||
|
@ -210,15 +207,15 @@ impl RequestSender {
|
||||||
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn send_json<T: Serialize>(
|
pub(crate) fn send_json(
|
||||||
mut self,
|
mut self,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
response_decompress: bool,
|
response_decompress: bool,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
config: &ClientConfig,
|
config: &ClientConfig,
|
||||||
value: &T,
|
value: impl Serialize,
|
||||||
) -> SendClientRequest {
|
) -> SendClientRequest {
|
||||||
let body = match serde_json::to_string(value) {
|
let body = match serde_json::to_string(&value) {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
Err(err) => return PrepForSendingError::Json(err).into(),
|
Err(err) => return PrepForSendingError::Json(err).into(),
|
||||||
};
|
};
|
||||||
|
@ -227,22 +224,16 @@ impl RequestSender {
|
||||||
return e.into();
|
return e.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.send_body(
|
self.send_body(addr, response_decompress, timeout, config, body)
|
||||||
addr,
|
|
||||||
response_decompress,
|
|
||||||
timeout,
|
|
||||||
config,
|
|
||||||
AnyBody::from_message_body(body.into_bytes()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn send_form<T: Serialize>(
|
pub(crate) fn send_form(
|
||||||
mut self,
|
mut self,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
response_decompress: bool,
|
response_decompress: bool,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
config: &ClientConfig,
|
config: &ClientConfig,
|
||||||
value: &T,
|
value: impl Serialize,
|
||||||
) -> SendClientRequest {
|
) -> SendClientRequest {
|
||||||
let body = match serde_urlencoded::to_string(value) {
|
let body = match serde_urlencoded::to_string(value) {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
|
@ -250,19 +241,13 @@ impl RequestSender {
|
||||||
};
|
};
|
||||||
|
|
||||||
// set content-type
|
// set content-type
|
||||||
if let Err(e) =
|
if let Err(err) =
|
||||||
self.set_header_if_none(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
self.set_header_if_none(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||||
{
|
{
|
||||||
return e.into();
|
return err.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.send_body(
|
self.send_body(addr, response_decompress, timeout, config, body)
|
||||||
addr,
|
|
||||||
response_decompress,
|
|
||||||
timeout,
|
|
||||||
config,
|
|
||||||
AnyBody::from_message_body(body.into_bytes()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn send_stream<S, E>(
|
pub(crate) fn send_stream<S, E>(
|
||||||
|
@ -274,7 +259,7 @@ impl RequestSender {
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> SendClientRequest
|
) -> SendClientRequest
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||||
E: Into<BoxError> + 'static,
|
E: Into<BoxError> + 'static,
|
||||||
{
|
{
|
||||||
self.send_body(
|
self.send_body(
|
||||||
|
@ -282,7 +267,7 @@ impl RequestSender {
|
||||||
response_decompress,
|
response_decompress,
|
||||||
timeout,
|
timeout,
|
||||||
config,
|
config,
|
||||||
AnyBody::new_boxed(BodyStream::new(stream)),
|
BodyStream::new(stream),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,7 +278,7 @@ impl RequestSender {
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
config: &ClientConfig,
|
config: &ClientConfig,
|
||||||
) -> SendClientRequest {
|
) -> 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>
|
fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError>
|
||||||
|
|
Loading…
Reference in New Issue