Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method_ref, uri_ref, headers_ref to request::Builder #284

Merged
merged 3 commits into from Dec 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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