Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions src/http_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].

Check failure on line 195 in src/http_interop.rs

View workflow job for this annotation

GitHub Actions / Docs

unresolved link to `Agent`
pub fn convert_from_http_builder_with_agent(value: http::request::Builder, agent: crate::Agent) -> Request {

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?

Choose a reason for hiding this comment

The 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 Agent or Request? I.e.: agent.request_builder(builder), or Request::from_builder(builder) and Request::from_builder_with_agent(builder, agent).

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`].
Expand All @@ -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`].

Check failure on line 239 in src/http_interop.rs

View workflow job for this annotation

GitHub Actions / Docs

unresolved link to `Agent`
pub fn convert_from_http_parts_with_agent(value: http::request::Parts, agent: crate::Agent) -> Request {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here regarding &crate::Agent.

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`].
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ mod testserver;

#[cfg(feature = "http-interop")]
// 0.2 version dependency (deprecated)
mod http_interop;
pub mod http_interop;

#[cfg(feature = "http-crate")]
// 1.0 version dependency.
Expand Down
Loading