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

async/request: add methods to split and reassemble a RequestBuilder #1770

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/async_impl/request.rs
Expand Up @@ -177,6 +177,14 @@ impl RequestBuilder {
}
}

/// Assemble a builder starting from an existing `Client` and a `Request`.
pub fn from_parts(client: Client, request: Request) -> RequestBuilder {
RequestBuilder {
client,
request: crate::Result::Ok(request),
}
}

/// Add a `Header` to this Request.
pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder
where
Expand Down Expand Up @@ -464,6 +472,15 @@ impl RequestBuilder {
self.request
}

/// Build a `Request`, which can be inspected, modified and executed with
/// `Client::execute()`.
///
/// This is similar to [`RequestBuilder::build()`], but also returns the
/// embedded `Client`.
pub fn build_split(self) -> (Client, crate::Result<Request>) {
(self.client, self.request)
}

/// Constructs the Request and sends it to the target URL, returning a
/// future Response.
///
Expand Down Expand Up @@ -630,7 +647,7 @@ impl TryFrom<Request> for HttpRequest<Body> {

#[cfg(test)]
mod tests {
use super::{Client, HttpRequest, Request, Version};
use super::{Client, HttpRequest, Request, RequestBuilder, Version};
use crate::Method;
use serde::Serialize;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -891,6 +908,18 @@ mod tests {
assert_eq!(req.version(), Version::HTTP_11);
}

#[test]
fn builder_split_reassemble() {
let builder = {
let client = Client::new();
client.get("http://example.com")
};
let (client, inner) = builder.build_split();
let request = inner.unwrap();
let builder = RequestBuilder::from_parts(client, request);
builder.build().unwrap();
}

/*
use {body, Method};
use super::Client;
Expand Down