Trait lamedh_http::ext::RequestExt [−][src]
Extentions for lambda_http::Request
structs that
provide access to API gateway
and ALB
features.
Examples
A request’s body can be deserialized if its correctly encoded as per
the request’s Content-Type
header. The two supported content types are
application/x-www-form-urlencoded
and application/json
.
The following handler will work an http request body of x=1&y=2
as well as {"x":1, "y":2}
respectively.
use lamedh_http::{handler, lambda::{self, Context, Error}, IntoResponse, Request, Response, RequestExt}; use aws_lambda_events::encodings::Body; use serde::Deserialize; #[derive(Debug,Deserialize,Default)] struct Args { #[serde(default)] x: usize, #[serde(default)] y: usize } #[tokio::main] async fn main() -> Result<(), Error> { lamedh_runtime::run(handler(add)).await?; Ok(()) } async fn add( request: Request, _: Context ) -> Result<Response<Body>, Error> { let args: Args = request.payload() .unwrap_or_else(|_parse_err| None) .unwrap_or_default(); Ok( Response::new( format!( "{} + {} = {}", args.x, args.y, args.x + args.y ).into() ) ) }
Required methods
fn query_string_parameters(&self) -> StrMap
[src]
Return pre-parsed http query string parameters, parameters
provided after the ?
portion of a url,
associated with the API gateway request.
The yielded value represents both single and multi-valued
parameters alike. When multiple query string parameters with the same
name are expected, query_string_parameters().get_all("many")
to retrieve them all.
No query parameters
will yield an empty StrMap
.
fn with_query_string_parameters<Q>(self, parameters: Q) -> Self where
Q: Into<StrMap>,
[src]
Q: Into<StrMap>,
Configures instance with query string parameters under #[cfg(test)] configurations
This is intended for use in mock testing contexts.
fn path_parameters(&self) -> StrMap
[src]
Return pre-extracted path parameters, parameter provided in url placeholders
/foo/{bar}/baz/{boom}
,
associated with the API gateway request. No path parameters
will yield an empty StrMap
These will always be empty for ALB triggered requests
fn with_path_parameters<P>(self, parameters: P) -> Self where
P: Into<StrMap>,
[src]
P: Into<StrMap>,
Configures instance with path parameters under #[cfg(test)] configurations
This is intended for use in mock testing contexts.
fn stage_variables(&self) -> StrMap
[src]
Return stage variables
associated with the API gateway request. No stage parameters
will yield an empty StrMap
These will always be empty for ALB triggered requests
fn request_context(&self) -> RequestContext
[src]
Return request context data assocaited with the ALB or API gateway request
fn payload<D>(&self) -> Result<Option<D>, PayloadError> where
D: Deserialize<'de>,
[src]
D: Deserialize<'de>,
Return the Result of a payload parsed into a serde Deserializeable type
Currently only application/x-www-form-urlencoded
and application/json
flavors of content type
are supported
A PayloadError will be returned for undeserializable
payloads. If no body is provided, Ok(None)
will be returned.
Implementations on Foreign Types
impl RequestExt for Request<Body>
[src]
fn query_string_parameters(&self) -> StrMap
[src]
fn with_query_string_parameters<Q>(self, parameters: Q) -> Self where
Q: Into<StrMap>,
[src]
Q: Into<StrMap>,
fn path_parameters(&self) -> StrMap
[src]
fn with_path_parameters<P>(self, parameters: P) -> Self where
P: Into<StrMap>,
[src]
P: Into<StrMap>,
fn stage_variables(&self) -> StrMap
[src]
fn request_context(&self) -> RequestContext
[src]
fn payload<D>(&self) -> Result<Option<D>, PayloadError> where
D: Deserialize<'de>,
[src]
D: Deserialize<'de>,