-
Notifications
You must be signed in to change notification settings - Fork 190
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
Allow setting custom agent when converting from http
crate types
#735
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,26 +188,31 @@ | |
/// ``` | ||
impl From<http::request::Builder> for Request { | ||
fn from(value: http::request::Builder) -> Self { | ||
let mut new_request = crate::agent().request( | ||
value.method_ref().map_or("GET", |m| m.as_str()), | ||
&value | ||
.uri_ref() | ||
.map_or("https://example.com".to_string(), |u| u.to_string()), | ||
); | ||
convert_from_http_builder_with_agent(value, crate::agent()) | ||
} | ||
} | ||
|
||
if let Some(headers) = value.headers_ref() { | ||
for (name, value) in headers { | ||
let mut raw_header: Vec<u8> = name.to_string().into_bytes(); | ||
raw_header.extend(b": "); | ||
raw_header.extend(value.as_bytes()); | ||
let header = HeaderLine::from(raw_header).into_header().unwrap(); | ||
/// Converts an [`http::request::Builder`] into a [`Request`] with a custom [`Agent`]. | ||
pub fn convert_from_http_builder_with_agent(value: http::request::Builder, agent: crate::Agent) -> Request { | ||
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. Maybe this (and the from parts variant) should be an associated function on either |
||
let mut new_request = agent.request( | ||
value.method_ref().map_or("GET", |m| m.as_str()), | ||
&value | ||
.uri_ref() | ||
.map_or("https://example.com".to_string(), |u| u.to_string()), | ||
); | ||
|
||
if let Some(headers) = value.headers_ref() { | ||
for (name, value) in headers { | ||
let mut raw_header: Vec<u8> = name.to_string().into_bytes(); | ||
raw_header.extend(b": "); | ||
raw_header.extend(value.as_bytes()); | ||
let header = HeaderLine::from(raw_header).into_header().unwrap(); | ||
|
||
crate::header::add_header(&mut new_request.headers, header) | ||
} | ||
crate::header::add_header(&mut new_request.headers, header) | ||
} | ||
|
||
new_request | ||
} | ||
|
||
new_request | ||
} | ||
|
||
/// Converts [`http::request::Parts`] into a [`Request`]. | ||
|
@@ -227,23 +232,29 @@ | |
/// ``` | ||
impl From<http::request::Parts> for Request { | ||
fn from(value: http::request::Parts) -> Self { | ||
let mut new_request = crate::agent().request(value.method.as_str(), &value.uri.to_string()); | ||
convert_from_http_parts_with_agent(value, crate::agent()) | ||
} | ||
} | ||
|
||
for (name, value) in &value.headers { | ||
// TODO: Aren't complete header values available as raw byte slices? | ||
let mut raw_header: Vec<u8> = name.to_string().into_bytes(); | ||
raw_header.extend(b": "); | ||
raw_header.extend(value.as_bytes()); | ||
/// Converts a [`http::request::Parts`] into a [`Request`] with a custom [`Agent`]. | ||
pub fn convert_from_http_parts_with_agent(value: http::request::Parts, agent: crate::Agent) -> Request { | ||
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. Same here regarding |
||
let mut new_request = agent.request(value.method.as_str(), &value.uri.to_string()); | ||
|
||
let header = HeaderLine::from(raw_header) | ||
.into_header() | ||
.expect("Unreachable"); | ||
for (name, value) in &value.headers { | ||
// TODO: Aren't complete header values available as raw byte slices? | ||
let mut raw_header: Vec<u8> = name.to_string().into_bytes(); | ||
raw_header.extend(b": "); | ||
raw_header.extend(value.as_bytes()); | ||
|
||
crate::header::add_header(&mut new_request.headers, header) | ||
} | ||
let header = HeaderLine::from(raw_header) | ||
.into_header() | ||
.expect("Unreachable"); | ||
|
||
new_request | ||
crate::header::add_header(&mut new_request.headers, header) | ||
} | ||
|
||
new_request | ||
|
||
} | ||
|
||
/// Converts a [`Request`] into an [`http::request::Builder`]. | ||
|
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.
agent
is used only for the&self
request
call, so maybe take an&crate::Agent
here?