pub struct RequestFactory { /* private fields */ }Expand description
§Request Factory
This is a starting point for building HTTP requests with a configured server address, method, path, query parameters. Handy when you need to create multiple requests tha all require the same base configuration.
For more docs see Request::factory
Implementations§
Source§impl RequestFactory
impl RequestFactory
Sourcepub fn builder(&self) -> RequestBuilder
pub fn builder(&self) -> RequestBuilder
§Start a new request builder
Initializes a new request builder with the base configuration that was set in the factory.
let factory = Request::factory()
.address_port(3000)
.method(Method::Put)
.header("Content-Type", "application/json")
.query_param("secret", "key")
.build();
let request = factory.builder()
.path("/api/v1/resource")
.body_text(r#"{"foo":"bar"}"#)
.build();
assert_eq!(request.method, Method::Put);
assert_eq!(request.path.as_str(), "/api/v1/resource");
assert_eq!(request.headers.first("Content-Type"), Some("application/json"));
assert_eq!(request.query.first("secret"), Some("key"));
assert_eq!(request.body.text(), Some(r#"{"foo":"bar"}"#));
assert_eq!(
request.build_url().as_str(),
"http://localhost:3000/api/v1/resource?secret=key"
);Sourcepub fn request(&self, method: Method, path: &str) -> RequestBuilder
pub fn request(&self, method: Method, path: &str) -> RequestBuilder
Starts a builder with the specified method and path.
let factory = Request::factory().address_port(3000).build();
let req = factory
.request(Method::Delete, "/api/v1/resource/42")
.build();
assert_eq!(req.method, Method::Delete);
assert_eq!(req.path.as_str(), "/api/v1/resource/42");
assert_eq!(req.address.port, Some(3000));
assert_eq!(
req.build_url().as_str(),
"http://localhost:3000/api/v1/resource/42"
);Sourcepub fn get(&self, path: &str) -> RequestBuilder
pub fn get(&self, path: &str) -> RequestBuilder
GET path
let factory = RequestFactory::default();
let request = factory.get("/api/v1/resource").build();
assert_eq!(request.method, Method::Get);
assert_eq!(request.path.as_str(), "/api/v1/resource");
assert!(request.query.is_empty());Sourcepub fn post(&self, path: &str) -> RequestBuilder
pub fn post(&self, path: &str) -> RequestBuilder
POST path
let factory = Request::factory()
.header("Content-Type", "application/json")
.build();
let req = factory
.post("/widgets")
.body_text(r#"{"name":"foo"}"#)
.build();
assert_eq!(req.method, Method::Post);
assert_eq!(req.headers.first("content-type"), Some("application/json"));
assert_eq!(req.path.as_str(), "/widgets");
assert_eq!(req.body.text(), Some(r#"{"name":"foo"}"#));Sourcepub fn put(&self, path: &str) -> RequestBuilder
pub fn put(&self, path: &str) -> RequestBuilder
PUT path
let factory = Request::factory()
.header("Content-Type", "application/json")
.build();
let req = factory
.put("/foo")
.body_text("fiz-buz")
.build();
assert_eq!(req.method, Method::Put);
assert_eq!(req.headers.first("content-type"), Some("application/json"));
assert_eq!(req.path.as_str(), "/foo");
assert_eq!(req.body.text(), Some("fiz-buz"));Sourcepub fn delete(&self, path: &str) -> RequestBuilder
pub fn delete(&self, path: &str) -> RequestBuilder
DELETE path
let factory = Request::factory()
.header("Authorization", "Bearer some-token")
.build();
let req = factory
.delete("/foo/123")
.build();
assert_eq!(req.method, Method::Delete);
assert_eq!(req.headers.first("authorization"), Some("Bearer some-token"));
assert_eq!(req.path.as_str(), "/foo/123");
assert!(req.body.is_empty());Sourcepub fn patch(&self, path: &str) -> RequestBuilder
pub fn patch(&self, path: &str) -> RequestBuilder
PATCH path
let factory = Request::factory()
.header("Authorization", "ApiKey $rofl$")
.build();
let req = factory
.patch("/my/email")
.body_text("domain=example.com")
.build();
assert_eq!(req.method, Method::Patch);
assert_eq!(req.headers.first("authorization"), Some("ApiKey $rofl$"));
assert_eq!(req.path.as_str(), "/my/email");
assert_eq!(req.body.text(), Some("domain=example.com"));Trait Implementations§
Source§impl Clone for RequestFactory
impl Clone for RequestFactory
Source§fn clone(&self) -> RequestFactory
fn clone(&self) -> RequestFactory
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for RequestFactory
impl Debug for RequestFactory
Source§impl Default for RequestFactory
impl Default for RequestFactory
Source§fn default() -> RequestFactory
fn default() -> RequestFactory
Returns the “default value” for a type. Read more
Source§impl From<Request> for RequestFactory
impl From<Request> for RequestFactory
Source§impl PartialEq for RequestFactory
impl PartialEq for RequestFactory
impl Eq for RequestFactory
impl StructuralPartialEq for RequestFactory
Auto Trait Implementations§
impl Freeze for RequestFactory
impl RefUnwindSafe for RequestFactory
impl Send for RequestFactory
impl Sync for RequestFactory
impl Unpin for RequestFactory
impl UnsafeUnpin for RequestFactory
impl UnwindSafe for RequestFactory
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more