Skip to content

Commit

Permalink
Add method_ref, uri_ref, headers_ref to request::Builder (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuk authored and seanmonstar committed Dec 19, 2018
1 parent d95c0ba commit 5018e48
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/request.rs
Expand Up @@ -771,6 +771,34 @@ impl Builder {
self
}

/// Get the HTTP Method for this request.
///
/// By default this is `GET`.
/// if builder has error, returns None.
///
/// # Examples
///
/// ```
/// # use http::*;
///
/// let mut req = Request::builder();
/// assert_eq!(req.method_ref(),Some(&Method::GET));
/// req.method("POST");
/// assert_eq!(req.method_ref(),Some(&Method::POST));
/// req.method("DELETE");
/// assert_eq!(req.method_ref(),Some(&Method::DELETE));
/// ```
pub fn method_ref(&self) -> Option<&Method>
{
if self.err.is_some() {
return None
}
match self.head {
Some(ref head) => Some(&head.method),
None => None
}
}

/// Set the URI for this request.
///
/// This function will configure the URI of the `Request` that will
Expand Down Expand Up @@ -800,6 +828,31 @@ impl Builder {
self
}

/// Get the URI for this request
///
/// By default this is `/`
/// # Examples
///
/// ```
/// # use http::*;
///
/// let mut req = Request::builder();
/// assert_eq!(req.uri_ref().unwrap().to_string(), "/" );
/// req.uri("https://www.rust-lang.org/");
/// assert_eq!(req.uri_ref().unwrap().to_string(), "https://www.rust-lang.org/" );
/// ```
pub fn uri_ref(&self) -> Option<&Uri>
{
if self.err.is_some() {
return None;
}
match self.head
{
Some(ref head) => Some(&head.uri),
None => None
}
}

/// Set the HTTP version for this request.
///
/// This function will configure the HTTP version of the `Request` that
Expand Down Expand Up @@ -860,6 +913,33 @@ impl Builder {
self
}

/// Get header on this request builder.
/// when builder has error returns None
///
/// # Example
///
/// ```
/// # use http::*;
/// # use http::header::HeaderValue;
/// # use http::request::Builder;
/// let mut req = Request::builder();
/// req.header("Accept", "text/html")
/// .header("X-Custom-Foo", "bar");
/// let headers = req.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>> {
if self.err.is_some() {
return None;
}
match self.head
{
Some(ref head) => Some(&head.headers),
None => None
}
}

/// Adds an extension to this builder
///
/// # Examples
Expand Down

0 comments on commit 5018e48

Please sign in to comment.