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 lambda extractors #2038

Merged
merged 7 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/request/lambda.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

//! The [`lambda_http`] types included in [`http::Request`]s when [`LambdaHandler`](crate::routing::LambdaHandler) is
//! used. Each are given a [`FromParts`] implementation for easy use within handlers.

use lambda_http::request::RequestContext;
#[doc(inline)]
pub use lambda_http::{
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
aws_lambda_events::apigw::{ApiGatewayProxyRequestContext, ApiGatewayV2httpRequestContext},
Context,
};

use super::{extension::MissingExtension, FromParts};
use crate::Extension;

impl<P> FromParts<P> for Context {
type Rejection = MissingExtension;
hlbarber marked this conversation as resolved.
Show resolved Hide resolved

fn from_parts(parts: &mut http::request::Parts) -> Result<Self, Self::Rejection> {
let Extension(context) = <Extension<Self> as FromParts<P>>::from_parts(parts)?;
Ok(context)
}
}

impl<P> FromParts<P> for ApiGatewayProxyRequestContext {
type Rejection = MissingExtension;

fn from_parts(parts: &mut http::request::Parts) -> Result<Self, Self::Rejection> {
let Extension(context) = <Extension<RequestContext> as FromParts<P>>::from_parts(parts)?;
if let RequestContext::ApiGatewayV1(context) = context {
Ok(context)
} else {
Err(MissingExtension)
}
}
}

impl<P> FromParts<P> for ApiGatewayV2httpRequestContext {
type Rejection = MissingExtension;

fn from_parts(parts: &mut http::request::Parts) -> Result<Self, Self::Rejection> {
let Extension(context) = <Extension<RequestContext> as FromParts<P>>::from_parts(parts)?;
if let RequestContext::ApiGatewayV2(context) = context {
Ok(context)
} else {
Err(MissingExtension)
}
}
}
3 changes: 3 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ use crate::{rejection::any_rejections, response::IntoResponse};

pub mod connect_info;
pub mod extension;
#[cfg(feature = "aws-lambda")]
#[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))]
pub mod lambda;

#[doc(hidden)]
#[derive(Debug)]
Expand Down