use axum::extract::FromRequestParts;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
#[derive(Debug, Clone)]
pub struct DynamoClient(pub aws_sdk_dynamodb::Client);
impl std::ops::Deref for DynamoClient {
type Target = aws_sdk_dynamodb::Client;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug)]
pub struct DynamoClientNotAvailable;
impl IntoResponse for DynamoClientNotAvailable {
fn into_response(self) -> Response {
rusty_gasket::error::quick_error_response(
StatusCode::INTERNAL_SERVER_ERROR,
"DYNAMODB_NOT_AVAILABLE",
"DynamoDB client not available — is DynamoPlugin registered?",
)
}
}
impl<S> FromRequestParts<S> for DynamoClient
where
S: Send + Sync,
{
type Rejection = DynamoClientNotAvailable;
async fn from_request_parts(
parts: &mut http::request::Parts,
_state: &S,
) -> Result<Self, Self::Rejection> {
parts
.extensions
.get::<axum::Extension<Self>>()
.map(|ext| ext.0.clone())
.or_else(|| parts.extensions.get::<Self>().cloned())
.ok_or(DynamoClientNotAvailable)
}
}