Skip to content

Commit

Permalink
Add TryFrom<Request> for HttpRequest<Option<Body>>
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored and seanmonstar committed Aug 26, 2021
1 parent 66c1b48 commit 3879694
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/async_impl/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,31 @@ where
}
}

impl TryFrom<Request> for HttpRequest<Body> {
type Error = crate::Error;

fn try_from(req: Request) -> crate::Result<Self> {
let Request {
method,
url,
headers,
body,
version,
..
} = req;

let mut req = HttpRequest::builder()
.version(version)
.method(method)
.uri(url.as_str())
.body(body.unwrap_or_else(Body::empty))
.map_err(crate::error::builder)?;

*req.headers_mut() = headers;
Ok(req)
}
}

#[cfg(test)]
mod tests {
use super::{Client, HttpRequest, Request, Version};
Expand Down
24 changes: 24 additions & 0 deletions src/wasm/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::convert::TryFrom;
use std::fmt;

use bytes::Bytes;
use http::{request::Parts, Method, Request as HttpRequest};
use serde::Serialize;
#[cfg(feature = "json")]
Expand Down Expand Up @@ -435,3 +436,26 @@ where
})
}
}

impl TryFrom<Request> for HttpRequest<Body> {
type Error = crate::Error;

fn try_from(req: Request) -> crate::Result<Self> {
let Request {
method,
url,
headers,
body,
..
} = req;

let mut req = HttpRequest::builder()
.method(method)
.uri(url.as_str())
.body(body.unwrap_or_else(|| Body::from(Bytes::default())))
.map_err(crate::error::builder)?;

*req.headers_mut() = headers;
Ok(req)
}
}

0 comments on commit 3879694

Please sign in to comment.