Skip to content

Commit 147216a

Browse files
author
zeroed
committed
Examples and docs on method shorthands for Request constructor
1 parent ed98ab7 commit 147216a

File tree

1 file changed

+99
-10
lines changed

1 file changed

+99
-10
lines changed

src/request.rs

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,20 @@ impl Request {
666666
Ok(())
667667
}
668668

669-
/// Create a GET request.
669+
/// Create a `GET` request.
670+
///
671+
/// The `GET` method requests a representation of the specified resource.
672+
/// Requests using `GET` should only retrieve data.
670673
///
671674
/// # Examples
675+
///
676+
/// ```
677+
/// use http_types::{Method, Request, Url};
678+
///
679+
/// let mut req = Request::get(Url::parse("https://example.com").unwrap());
680+
/// req.set_body("Hello, Nori!");
681+
/// assert_eq!(req.method(), Method::Get);
682+
/// ```
672683
pub fn get<U>(url: U) -> Self
673684
where
674685
U: TryInto<Url>,
@@ -677,9 +688,19 @@ impl Request {
677688
Request::new(Method::Get, url)
678689
}
679690

680-
/// Create a HEAD request.
691+
/// Create a `HEAD` request.
692+
///
693+
/// The `HEAD` method asks for a response identical to that of a `GET`
694+
/// request, but without the response body.
681695
///
682696
/// # Examples
697+
///
698+
/// ```
699+
/// use http_types::{Method, Request, Url};
700+
///
701+
/// let mut req = Request::head(Url::parse("https://example.com").unwrap());
702+
/// assert_eq!(req.method(), Method::Head);
703+
/// ```
683704
pub fn head<U>(url: U) -> Self
684705
where
685706
U: TryInto<Url>,
@@ -688,9 +709,19 @@ impl Request {
688709
Request::new(Method::Head, url)
689710
}
690711

691-
/// Create a POST request.
712+
/// Create a `POST` request.
713+
///
714+
/// The `POST` method is used to submit an entity to the specified resource,
715+
/// often causing a change in state or side effects on the server.
692716
///
693717
/// # Examples
718+
///
719+
/// ```
720+
/// use http_types::{Method, Request, Url};
721+
///
722+
/// let mut req = Request::post(Url::parse("https://example.com").unwrap());
723+
/// assert_eq!(req.method(), Method::Post);
724+
/// ```
694725
pub fn post<U>(url: U) -> Self
695726
where
696727
U: TryInto<Url>,
@@ -699,9 +730,19 @@ impl Request {
699730
Request::new(Method::Post, url)
700731
}
701732

702-
/// Create a PUT request.
733+
/// Create a `PUT` request.
734+
///
735+
/// The `PUT` method replaces all current representations of the target
736+
/// resource with the request payload.
703737
///
704738
/// # Examples
739+
///
740+
/// ```
741+
/// use http_types::{Method, Request, Url};
742+
///
743+
/// let mut req = Request::put(Url::parse("https://example.com").unwrap());
744+
/// assert_eq!(req.method(), Method::Put);
745+
/// ```
705746
pub fn put<U>(url: U) -> Self
706747
where
707748
U: TryInto<Url>,
@@ -710,9 +751,18 @@ impl Request {
710751
Request::new(Method::Put, url)
711752
}
712753

713-
/// Create a DELETE request.
754+
/// Create a `DELETE` request.
755+
///
756+
/// The `DELETE` method deletes the specified resource.
714757
///
715758
/// # Examples
759+
///
760+
/// ```
761+
/// use http_types::{Method, Request, Url};
762+
///
763+
/// let mut req = Request::delete(Url::parse("https://example.com").unwrap());
764+
/// assert_eq!(req.method(), Method::Delete);
765+
/// ```
716766
pub fn delete<U>(url: U) -> Self
717767
where
718768
U: TryInto<Url>,
@@ -721,9 +771,19 @@ impl Request {
721771
Request::new(Method::Delete, url)
722772
}
723773

724-
/// Create a CONNECT request.
774+
/// Create a `CONNECT` request.
775+
///
776+
/// The `CONNECT` method establishes a tunnel to the server identified by
777+
/// the target resource.
725778
///
726779
/// # Examples
780+
///
781+
/// ```
782+
/// use http_types::{Method, Request, Url};
783+
///
784+
/// let mut req = Request::connect(Url::parse("https://example.com").unwrap());
785+
/// assert_eq!(req.method(), Method::Connect);
786+
/// ```
727787
pub fn connect<U>(url: U) -> Self
728788
where
729789
U: TryInto<Url>,
@@ -732,20 +792,40 @@ impl Request {
732792
Request::new(Method::Connect, url)
733793
}
734794

735-
/// Create a OPTIONS request.
795+
/// Create a `OPTIONS` request.
796+
///
797+
/// The `OPTIONS` method is used to describe the communication options for
798+
/// the target resource.
736799
///
737800
/// # Examples
738-
pub fn optiond<U>(url: U) -> Self
801+
///
802+
/// ```
803+
/// use http_types::{Method, Request, Url};
804+
///
805+
/// let mut req = Request::options(Url::parse("https://example.com").unwrap());
806+
/// assert_eq!(req.method(), Method::Options);
807+
/// ```
808+
pub fn options<U>(url: U) -> Self
739809
where
740810
U: TryInto<Url>,
741811
U::Error: std::fmt::Debug,
742812
{
743813
Request::new(Method::Options, url)
744814
}
745815

746-
/// Create a TRACE request.
816+
/// Create a `TRACE` request.
817+
///
818+
/// The `TRACE` method performs a message loop-back test along the path to
819+
/// the target resource.
747820
///
748821
/// # Examples
822+
///
823+
/// ```
824+
/// use http_types::{Method, Request, Url};
825+
///
826+
/// let mut req = Request::trace(Url::parse("https://example.com").unwrap());
827+
/// assert_eq!(req.method(), Method::Trace);
828+
/// ```
749829
pub fn trace<U>(url: U) -> Self
750830
where
751831
U: TryInto<Url>,
@@ -754,9 +834,18 @@ impl Request {
754834
Request::new(Method::Trace, url)
755835
}
756836

757-
/// Create a PATCH request.
837+
/// Create a `PATCH` request.
838+
///
839+
/// The `PATCH` method is used to apply partial modifications to a resource.
758840
///
759841
/// # Examples
842+
///
843+
/// ```
844+
/// use http_types::{Method, Request, Url};
845+
///
846+
/// let mut req = Request::patch(Url::parse("https://example.com").unwrap());
847+
/// assert_eq!(req.method(), Method::Patch);
848+
/// ```
760849
pub fn patch<U>(url: U) -> Self
761850
where
762851
U: TryInto<Url>,

0 commit comments

Comments
 (0)