Skip to content

Commit

Permalink
async/request: add methods to split and reassemble a RequestBuilder (#…
Browse files Browse the repository at this point in the history
…1770)

This adds two methods to split and reassemble a `RequestBuilder`.
It allows consumers to temporarily reach into the inner `Request`
and later reconstruct the original builder.
  • Loading branch information
lucab committed Mar 9, 2023
1 parent 673449a commit 119366e
Showing 1 changed file with 30 additions and 1 deletion.
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

0 comments on commit 119366e

Please sign in to comment.