Struct tide_disco::request::RequestParams
source · pub struct RequestParams { /* private fields */ }
Expand description
Parameters passed to a route handler.
These parameters describe the incoming request and the current server state.
Implementations§
source§impl RequestParams
impl RequestParams
sourcepub fn accept(&self) -> Result<Accept, RequestError>
pub fn accept(&self) -> Result<Accept, RequestError>
The [Accept] header of this request.
The media type proposals in the resulting header are sorted in order of decreasing weight.
If no [Accept] header was explicitly set, defaults to the wildcard Accept: *
.
§Error
Returns RequestError::Http if the [Accept] header is malformed.
sourcepub fn remote(&self) -> Option<&str>
pub fn remote(&self) -> Option<&str>
Get the remote address for this request.
This is determined in the following priority:
Forwarded
headerfor
key- The first
X-Forwarded-For
header - Peer address of the transport
sourcepub fn param<Name>(
&self,
name: &Name,
) -> Result<&RequestParamValue, RequestError>
pub fn param<Name>( &self, name: &Name, ) -> Result<&RequestParamValue, RequestError>
Get the value of a named parameter.
The name of the parameter can be given by any type that implements Display. Of course, the simplest option is to use str or String, as in
req.param("foo")
However, you have the option of defining a statically typed enum representing the possible parameters of a given route and using enum variants as parameter names. Among other benefits, this allows you to change the client-facing parameter names just by tweaking the Display implementation of your enum, without changing other code.
use std::fmt::{self, Display, Formatter};
enum RouteParams {
Param1,
Param2,
}
impl Display for RouteParams {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let name = match self {
Self::Param1 => "param1",
Self::Param2 => "param2",
};
write!(f, "{}", name)
}
}
req.param(&RouteParams::Param1)
You can also use [strum_macros] to automatically derive the Display implementation, so you only have to specify the client-facing names of each parameter:
#[derive(strum_macros::Display)]
enum RouteParams {
#[strum(serialize = "param1")]
Param1,
#[strum(serialize = "param2")]
Param2,
}
req.param(&RouteParams::Param1)
§Errors
Returns RequestError::MissingParam if a parameter called name
was not provided with the
request.
It is recommended to implement From<RequestError>
for the error type for your API, so that
you can use ?
with this function in a route handler. If your error type implements
Error, you can easily use the catch_all
constructor to do this:
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use tide_disco::{Error, RequestError, RequestParams, StatusCode};
type ApiState = ();
#[derive(Debug, Snafu, Deserialize, Serialize)]
struct ApiError {
status: StatusCode,
msg: String,
}
impl Error for ApiError {
fn catch_all(status: StatusCode, msg: String) -> Self {
Self { status, msg }
}
fn status(&self) -> StatusCode {
self.status
}
}
impl From<RequestError> for ApiError {
fn from(err: RequestError) -> Self {
Self::catch_all(StatusCode::BAD_REQUEST, err.to_string())
}
}
async fn my_route_handler(req: RequestParams, _state: &ApiState) -> Result<(), ApiError> {
let param = req.param("my_param")?;
Ok(())
}
sourcepub fn opt_param<Name>(&self, name: &Name) -> Option<&RequestParamValue>
pub fn opt_param<Name>(&self, name: &Name) -> Option<&RequestParamValue>
sourcepub fn integer_param<Name, T>(&self, name: &Name) -> Result<T, RequestError>
pub fn integer_param<Name, T>(&self, name: &Name) -> Result<T, RequestError>
sourcepub fn opt_integer_param<Name, T>(
&self,
name: &Name,
) -> Result<Option<T>, RequestError>
pub fn opt_integer_param<Name, T>( &self, name: &Name, ) -> Result<Option<T>, RequestError>
sourcepub fn boolean_param<Name>(&self, name: &Name) -> Result<bool, RequestError>
pub fn boolean_param<Name>(&self, name: &Name) -> Result<bool, RequestError>
sourcepub fn opt_boolean_param<Name>(
&self,
name: &Name,
) -> Result<Option<bool>, RequestError>
pub fn opt_boolean_param<Name>( &self, name: &Name, ) -> Result<Option<bool>, RequestError>
sourcepub fn string_param<Name>(&self, name: &Name) -> Result<&str, RequestError>
pub fn string_param<Name>(&self, name: &Name) -> Result<&str, RequestError>
sourcepub fn opt_string_param<Name>(
&self,
name: &Name,
) -> Result<Option<&str>, RequestError>
pub fn opt_string_param<Name>( &self, name: &Name, ) -> Result<Option<&str>, RequestError>
sourcepub fn tagged_base64_param<Name>(
&self,
name: &Name,
) -> Result<&TaggedBase64, RequestError>
pub fn tagged_base64_param<Name>( &self, name: &Name, ) -> Result<&TaggedBase64, RequestError>
sourcepub fn opt_tagged_base64_param<Name>(
&self,
name: &Name,
) -> Result<Option<&TaggedBase64>, RequestError>
pub fn opt_tagged_base64_param<Name>( &self, name: &Name, ) -> Result<Option<&TaggedBase64>, RequestError>
sourcepub fn blob_param<'a, Name, T>(&'a self, name: &Name) -> Result<T, RequestError>
pub fn blob_param<'a, Name, T>(&'a self, name: &Name) -> Result<T, RequestError>
sourcepub fn opt_blob_param<'a, Name, T>(
&'a self,
name: &Name,
) -> Result<Option<T>, RequestError>
pub fn opt_blob_param<'a, Name, T>( &'a self, name: &Name, ) -> Result<Option<T>, RequestError>
pub fn body_bytes(&self) -> Vec<u8> ⓘ
pub fn body_json<T>(&self) -> Result<T, RequestError>where
T: DeserializeOwned,
sourcepub fn body_auto<T, VER: StaticVersionType>(
&self,
_: VER,
) -> Result<T, RequestError>where
T: DeserializeOwned,
pub fn body_auto<T, VER: StaticVersionType>(
&self,
_: VER,
) -> Result<T, RequestError>where
T: DeserializeOwned,
Deserialize the body of a request.
The Content-Type header is used to determine the serialization format.
Trait Implementations§
source§impl Clone for RequestParams
impl Clone for RequestParams
source§fn clone(&self) -> RequestParams
fn clone(&self) -> RequestParams
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for RequestParams
impl !RefUnwindSafe for RequestParams
impl Send for RequestParams
impl Sync for RequestParams
impl Unpin for RequestParams
impl !UnwindSafe for RequestParams
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
§fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
§fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
§fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
§fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
§fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
§fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
§fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
§fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
§fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
§fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
§fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
§fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
§fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
§fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
§fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
§fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
§fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
§fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
§fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
§fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
§fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
§fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
§fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
§fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
§fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more