Struct rocket_http::hyper::request::Builder
source · pub struct Builder { /* private fields */ }
Expand description
An HTTP request builder
This type can be used to construct an instance or Request
through a builder-like pattern.
Implementations§
source§impl Builder
impl Builder
sourcepub fn new() -> Builder
pub fn new() -> Builder
Creates a new default instance of Builder
to construct a Request
.
Examples
let req = request::Builder::new()
.method("POST")
.body(())
.unwrap();
sourcepub fn method<T>(self, method: T) -> Builderwhere
Method: TryFrom<T>,
<Method as TryFrom<T>>::Error: Into<Error>,
pub fn method<T>(self, method: T) -> Builderwhere Method: TryFrom<T>, <Method as TryFrom<T>>::Error: Into<Error>,
Set the HTTP method for this request.
This function will configure the HTTP method of the Request
that will
be returned from Builder::build
.
By default this is GET
.
Examples
let req = Request::builder()
.method("POST")
.body(())
.unwrap();
sourcepub fn method_ref(&self) -> Option<&Method>
pub fn method_ref(&self) -> Option<&Method>
Get the HTTP Method for this request.
By default this is GET
. If builder has error, returns None.
Examples
let mut req = Request::builder();
assert_eq!(req.method_ref(),Some(&Method::GET));
req = req.method("POST");
assert_eq!(req.method_ref(),Some(&Method::POST));
sourcepub fn uri<T>(self, uri: T) -> Builderwhere
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<Error>,
pub fn uri<T>(self, uri: T) -> Builderwhere Uri: TryFrom<T>, <Uri as TryFrom<T>>::Error: Into<Error>,
Set the URI for this request.
This function will configure the URI of the Request
that will
be returned from Builder::build
.
By default this is /
.
Examples
let req = Request::builder()
.uri("https://www.rust-lang.org/")
.body(())
.unwrap();
sourcepub fn uri_ref(&self) -> Option<&Uri>
pub fn uri_ref(&self) -> Option<&Uri>
Get the URI for this request
By default this is /
.
Examples
let mut req = Request::builder();
assert_eq!(req.uri_ref().unwrap(), "/" );
req = req.uri("https://www.rust-lang.org/");
assert_eq!(req.uri_ref().unwrap(), "https://www.rust-lang.org/" );
sourcepub fn version(self, version: Version) -> Builder
pub fn version(self, version: Version) -> Builder
Set the HTTP version for this request.
This function will configure the HTTP version of the Request
that
will be returned from Builder::build
.
By default this is HTTP/1.1
Examples
let req = Request::builder()
.version(Version::HTTP_2)
.body(())
.unwrap();
sourcepub fn version_ref(&self) -> Option<&Version>
pub fn version_ref(&self) -> Option<&Version>
Get the HTTP version for this request
By default this is HTTP/1.1.
Examples
let mut req = Request::builder();
assert_eq!(req.version_ref().unwrap(), &Version::HTTP_11 );
req = req.version(Version::HTTP_2);
assert_eq!(req.version_ref().unwrap(), &Version::HTTP_2 );
sourcepub fn header<K, V>(self, key: K, value: V) -> Builderwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
pub fn header<K, V>(self, key: K, value: V) -> Builderwhere HeaderName: TryFrom<K>, <HeaderName as TryFrom<K>>::Error: Into<Error>, HeaderValue: TryFrom<V>, <HeaderValue as TryFrom<V>>::Error: Into<Error>,
Appends a header to this request builder.
This function will append the provided key/value as a header to the
internal HeaderMap
being constructed. Essentially this is equivalent
to calling HeaderMap::append
.
Examples
let req = Request::builder()
.header("Accept", "text/html")
.header("X-Custom-Foo", "bar")
.body(())
.unwrap();
sourcepub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>>
pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>>
Get header on this request builder. when builder has error returns None
Example
let req = Request::builder()
.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" );
sourcepub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>>
pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>>
Get headers on this request builder.
When builder has error returns None.
Example
let mut req = Request::builder();
{
let headers = req.headers_mut().unwrap();
headers.insert("Accept", HeaderValue::from_static("text/html"));
headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
}
let headers = req.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
sourcepub fn extension<T>(self, extension: T) -> Builderwhere
T: Any + Send + Sync + 'static,
pub fn extension<T>(self, extension: T) -> Builderwhere T: Any + Send + Sync + 'static,
Adds an extension to this builder
Examples
let req = Request::builder()
.extension("My Extension")
.body(())
.unwrap();
assert_eq!(req.extensions().get::<&'static str>(),
Some(&"My Extension"));
sourcepub fn extensions_ref(&self) -> Option<&Extensions>
pub fn extensions_ref(&self) -> Option<&Extensions>
Get a reference to the extensions for this request builder.
If the builder has an error, this returns None
.
Example
let req = Request::builder().extension("My Extension").extension(5u32);
let extensions = req.extensions_ref().unwrap();
assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
assert_eq!(extensions.get::<u32>(), Some(&5u32));
sourcepub fn extensions_mut(&mut self) -> Option<&mut Extensions>
pub fn extensions_mut(&mut self) -> Option<&mut Extensions>
Get a mutable reference to the extensions for this request builder.
If the builder has an error, this returns None
.
Example
let mut req = Request::builder().extension("My Extension");
let mut extensions = req.extensions_mut().unwrap();
assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
extensions.insert(5u32);
assert_eq!(extensions.get::<u32>(), Some(&5u32));
sourcepub fn body<T>(self, body: T) -> Result<Request<T>, Error>
pub fn body<T>(self, body: T) -> Result<Request<T>, Error>
“Consumes” this builder, using the provided body
to return a
constructed Request
.
Errors
This function may return an error if any previously configured argument
failed to parse or get converted to the internal representation. For
example if an invalid head
was specified via header("Foo", "Bar\r\n")
the error will be returned when this function is called
rather than when header
was called.
Examples
let request = Request::builder()
.body(())
.unwrap();
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl !UnwindSafe for Builder
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
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
Example
println!("{}", value.bright_yellow());
source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.bright_magenta());
source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
Example
println!("{}", value.on_bright_black());
source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
Example
println!("{}", value.on_bright_green());
source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
Example
println!("{}", value.on_bright_yellow());
source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
Example
println!("{}", value.on_bright_blue());
source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.on_bright_magenta());
source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
Example
println!("{}", value.on_bright_cyan());
source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
Example
println!("{}", value.on_bright_white());
source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
Example
println!("{}", value.underline());
source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
Example
println!("{}", value.rapid_blink());
source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);