Skip to content

Commit ed98ab7

Browse files
author
zeroed
committed
Introduce method shorthands for Request constructor
Expose easy shorthands for Request constructors to match the approach used in Surf. Refs: #156
1 parent 64541a6 commit ed98ab7

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

src/request.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,110 @@ impl Request {
665665
self.url.set_query(Some(&query));
666666
Ok(())
667667
}
668+
669+
/// Create a GET request.
670+
///
671+
/// # Examples
672+
pub fn get<U>(url: U) -> Self
673+
where
674+
U: TryInto<Url>,
675+
U::Error: std::fmt::Debug,
676+
{
677+
Request::new(Method::Get, url)
678+
}
679+
680+
/// Create a HEAD request.
681+
///
682+
/// # Examples
683+
pub fn head<U>(url: U) -> Self
684+
where
685+
U: TryInto<Url>,
686+
U::Error: std::fmt::Debug,
687+
{
688+
Request::new(Method::Head, url)
689+
}
690+
691+
/// Create a POST request.
692+
///
693+
/// # Examples
694+
pub fn post<U>(url: U) -> Self
695+
where
696+
U: TryInto<Url>,
697+
U::Error: std::fmt::Debug,
698+
{
699+
Request::new(Method::Post, url)
700+
}
701+
702+
/// Create a PUT request.
703+
///
704+
/// # Examples
705+
pub fn put<U>(url: U) -> Self
706+
where
707+
U: TryInto<Url>,
708+
U::Error: std::fmt::Debug,
709+
{
710+
Request::new(Method::Put, url)
711+
}
712+
713+
/// Create a DELETE request.
714+
///
715+
/// # Examples
716+
pub fn delete<U>(url: U) -> Self
717+
where
718+
U: TryInto<Url>,
719+
U::Error: std::fmt::Debug,
720+
{
721+
Request::new(Method::Delete, url)
722+
}
723+
724+
/// Create a CONNECT request.
725+
///
726+
/// # Examples
727+
pub fn connect<U>(url: U) -> Self
728+
where
729+
U: TryInto<Url>,
730+
U::Error: std::fmt::Debug,
731+
{
732+
Request::new(Method::Connect, url)
733+
}
734+
735+
/// Create a OPTIONS request.
736+
///
737+
/// # Examples
738+
pub fn optiond<U>(url: U) -> Self
739+
where
740+
U: TryInto<Url>,
741+
U::Error: std::fmt::Debug,
742+
{
743+
Request::new(Method::Options, url)
744+
}
745+
746+
/// Create a TRACE request.
747+
///
748+
/// # Examples
749+
pub fn trace<U>(url: U) -> Self
750+
where
751+
U: TryInto<Url>,
752+
U::Error: std::fmt::Debug,
753+
{
754+
Request::new(Method::Trace, url)
755+
}
756+
757+
/// Create a PATCH request.
758+
///
759+
/// # Examples
760+
pub fn patch<U>(url: U) -> Self
761+
where
762+
U: TryInto<Url>,
763+
U::Error: std::fmt::Debug,
764+
{
765+
Request::new(Method::Patch, url)
766+
}
668767
}
669768

670769
impl Clone for Request {
671-
/// Clone the request, resolving the body to `Body::empty()` and removing extensions.
770+
/// Clone the request, resolving the body to `Body::empty()` and removing
771+
/// extensions.
672772
fn clone(&self) -> Self {
673773
Request {
674774
method: self.method.clone(),

0 commit comments

Comments
 (0)