-
Notifications
You must be signed in to change notification settings - Fork 281
add Method
& StatusCode
to azure_core
#849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||
use super::*; | ||||||
use std::{collections::HashMap, str::FromStr}; | ||||||
|
||||||
/// Construct a new `HttpClient` with the `reqwest` backend. | ||||||
pub fn new_http_client() -> std::sync::Arc<dyn HttpClient> { | ||||||
std::sync::Arc::new(::reqwest::Client::new()) | ||||||
} | ||||||
|
||||||
#[async_trait] | ||||||
impl HttpClient for ::reqwest::Client { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
async fn execute_request(&self, request: &crate::Request) -> crate::Result<crate::Response> { | ||||||
let url = request.url().clone(); | ||||||
let mut req = self.request(try_from_method(request.method())?, url); | ||||||
for (name, value) in request.headers().iter() { | ||||||
req = req.header(name.as_str(), value.as_str()); | ||||||
} | ||||||
|
||||||
let body = request.body().clone(); | ||||||
|
||||||
let reqwest_request = match body { | ||||||
Body::Bytes(bytes) => req | ||||||
.body(bytes) | ||||||
.build() | ||||||
.context(ErrorKind::Other, "failed to build request")?, | ||||||
Body::SeekableStream(mut seekable_stream) => { | ||||||
seekable_stream.reset().await.unwrap(); // TODO: remove unwrap when `HttpError` has been removed | ||||||
req.body(::reqwest::Body::wrap_stream(seekable_stream)) | ||||||
.build() | ||||||
.context(ErrorKind::Other, "failed to build request")? | ||||||
} | ||||||
}; | ||||||
|
||||||
let rsp = self | ||||||
.execute(reqwest_request) | ||||||
.await | ||||||
.context(ErrorKind::Io, "failed to execute request")?; | ||||||
|
||||||
let status = rsp.status(); | ||||||
let headers = to_headers(rsp.headers())?; | ||||||
let body: PinnedStream = Box::pin(rsp.bytes_stream().map_err(|error| { | ||||||
Error::full( | ||||||
ErrorKind::Io, | ||||||
error, | ||||||
"error converting `reqwest` request into a byte stream", | ||||||
) | ||||||
})); | ||||||
|
||||||
Ok(crate::Response::new( | ||||||
try_from_status(status)?, | ||||||
headers, | ||||||
body, | ||||||
)) | ||||||
} | ||||||
} | ||||||
|
||||||
fn to_headers(map: &::reqwest::header::HeaderMap) -> crate::Result<crate::headers::Headers> { | ||||||
let map = map | ||||||
.iter() | ||||||
.map(|(k, v)| { | ||||||
let key = k.as_str(); | ||||||
let value = std::str::from_utf8(v.as_bytes()).expect("non-UTF8 header value"); | ||||||
( | ||||||
crate::headers::HeaderName::from(key.to_owned()), | ||||||
crate::headers::HeaderValue::from(value.to_owned()), | ||||||
) | ||||||
}) | ||||||
.collect::<HashMap<_, _>>(); | ||||||
Ok(crate::headers::Headers::from(map)) | ||||||
} | ||||||
|
||||||
fn try_from_method(method: &crate::Method) -> crate::Result<::reqwest::Method> { | ||||||
::reqwest::Method::from_str(method.as_ref()).map_kind(ErrorKind::DataConversion) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This should never happen. I wonder if we should have a wrapper error that says something like: if you ever see this, please report a bug. |
||||||
} | ||||||
|
||||||
fn try_from_status(status: ::reqwest::StatusCode) -> crate::Result<crate::StatusCode> { | ||||||
let status = status.as_u16(); | ||||||
http_types::StatusCode::try_from(status) | ||||||
.map_err(|_| { | ||||||
Error::with_message(ErrorKind::DataConversion, || { | ||||||
format!("invalid status code {status}") | ||||||
}) | ||||||
}) | ||||||
.map(crate::StatusCode) | ||||||
Comment on lines
+77
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use |
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::error::{Error, ErrorKind}; | ||
use std::{ops::Deref, str::FromStr}; | ||
|
||
#[derive(PartialEq, Eq, Clone, Debug)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
pub struct Method(pub(crate) http_types::Method); | ||
|
||
impl Method { | ||
pub fn as_str(&self) -> &str { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl Deref for Method { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This puts |
||
type Target = http_types::Method; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Method { | ||
pub const CONNECT: Method = Method(http_types::Method::Connect); | ||
pub const DELETE: Method = Method(http_types::Method::Delete); | ||
pub const GET: Method = Method(http_types::Method::Get); | ||
pub const HEAD: Method = Method(http_types::Method::Head); | ||
pub const MERGE: Method = Method(http_types::Method::Merge); | ||
pub const OPTIONS: Method = Method(http_types::Method::Options); | ||
pub const PATCH: Method = Method(http_types::Method::Patch); | ||
pub const POST: Method = Method(http_types::Method::Post); | ||
pub const PUT: Method = Method(http_types::Method::Put); | ||
pub const TRACE: Method = Method(http_types::Method::Trace); | ||
} | ||
|
||
impl Default for Method { | ||
fn default() -> Method { | ||
Method::GET | ||
} | ||
} | ||
|
||
impl FromStr for Method { | ||
type Err = Error; | ||
fn from_str(s: &str) -> crate::Result<Self> { | ||
Ok(Method(http_types::Method::from_str(s).map_err( | ||
|error| Error::full(ErrorKind::DataConversion, error, "Method::from_str failed"), | ||
)?)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put these in a module named reqwest, so needed that prefix. Could rename the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm... or you could rename the 3rd party crate.
use ::request as hyperium_request
.