-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathlambda-runtime-authorizer-builder.rs
More file actions
96 lines (81 loc) · 3.15 KB
/
Copy pathlambda-runtime-authorizer-builder.rs
File metadata and controls
96 lines (81 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Example showing how builders work with generic types and custom context structs
//
// Demonstrates:
// 1. Generic types (ApiGatewayV2CustomAuthorizerSimpleResponse<T>)
// 2. Custom context struct WITHOUT Default implementation
// 3. Custom context struct WITH Default implementation
use aws_lambda_events::event::apigw::{
ApiGatewayV2CustomAuthorizerSimpleResponse, ApiGatewayV2CustomAuthorizerV2Request,
};
use lambda_runtime::{Error, LambdaEvent};
use serde::{Deserialize, Serialize};
// Custom context WITHOUT Default - requires builder pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextWithoutDefault {
pub user_id: String,
pub api_key: String,
pub permissions: Vec<String>,
}
// Custom context WITH Default - works both ways
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ContextWithDefault {
pub user_id: String,
pub role: String,
}
// Handler using context WITHOUT Default - builder pattern required
pub async fn handler_without_default(
_event: LambdaEvent<ApiGatewayV2CustomAuthorizerV2Request>,
) -> Result<ApiGatewayV2CustomAuthorizerSimpleResponse<ContextWithoutDefault>, Error> {
let context = ContextWithoutDefault {
user_id: "user-123".to_string(),
api_key: "secret-key".to_string(),
permissions: vec!["read".to_string()],
};
let response = ApiGatewayV2CustomAuthorizerSimpleResponse::builder()
.is_authorized(true)
.context(context)
.build();
Ok(response)
}
// Handler using context WITH Default - builder pattern still preferred
pub async fn handler_with_default(
_event: LambdaEvent<ApiGatewayV2CustomAuthorizerV2Request>,
) -> Result<ApiGatewayV2CustomAuthorizerSimpleResponse<ContextWithDefault>, Error> {
let context = ContextWithDefault {
user_id: "user-456".to_string(),
role: "admin".to_string(),
};
let response = ApiGatewayV2CustomAuthorizerSimpleResponse::builder()
.is_authorized(true)
.context(context)
.build();
Ok(response)
}
fn main() {
// Example 1: Context WITHOUT Default
let context_no_default = ContextWithoutDefault {
user_id: "user-123".to_string(),
api_key: "secret-key".to_string(),
permissions: vec!["read".to_string(), "write".to_string()],
};
let response1 = ApiGatewayV2CustomAuthorizerSimpleResponse::builder()
.is_authorized(true)
.context(context_no_default)
.build();
println!("Response with context WITHOUT Default:");
println!(" User: {}", response1.context.user_id);
println!(" Authorized: {}", response1.is_authorized);
// Example 2: Context WITH Default
let context_with_default = ContextWithDefault {
user_id: "user-456".to_string(),
role: "admin".to_string(),
};
let response2 = ApiGatewayV2CustomAuthorizerSimpleResponse::builder()
.is_authorized(false)
.context(context_with_default)
.build();
println!("\nResponse with context WITH Default:");
println!(" User: {}", response2.context.user_id);
println!(" Role: {}", response2.context.role);
println!(" Authorized: {}", response2.is_authorized);
}