ai-lls-lib 1.4.0rc2__tar.gz → 1.4.0rc4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/PKG-INFO +1 -1
  2. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/README.md +253 -253
  3. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/pyproject.toml +1 -1
  4. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/__init__.py +1 -1
  5. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/auth/__init__.py +4 -4
  6. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/auth/context_parser.py +68 -68
  7. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/__init__.py +3 -3
  8. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/__main__.py +30 -30
  9. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/aws_client.py +115 -115
  10. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/commands/__init__.py +3 -3
  11. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/commands/admin.py +174 -174
  12. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/commands/cache.py +142 -142
  13. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/commands/stripe.py +377 -377
  14. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/commands/test_stack.py +216 -216
  15. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/commands/verify.py +111 -111
  16. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/cli/env_loader.py +122 -122
  17. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/core/__init__.py +3 -3
  18. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/core/cache.py +106 -106
  19. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/core/models.py +77 -77
  20. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/core/processor.py +295 -295
  21. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/core/verifier.py +84 -84
  22. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/payment/__init__.py +13 -13
  23. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/payment/credit_manager.py +186 -193
  24. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/payment/models.py +102 -102
  25. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/payment/stripe_manager.py +487 -487
  26. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/payment/webhook_processor.py +215 -215
  27. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/providers/__init__.py +7 -7
  28. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/providers/base.py +28 -28
  29. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/providers/external.py +87 -87
  30. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/providers/stub.py +48 -48
  31. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/testing/__init__.py +3 -3
  32. {ai_lls_lib-1.4.0rc2 → ai_lls_lib-1.4.0rc4}/src/ai_lls_lib/testing/fixtures.py +104 -104
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: ai-lls-lib
3
- Version: 1.4.0rc2
3
+ Version: 1.4.0rc4
4
4
  Summary: Landline Scrubber core library - phone verification and DNC checking
5
5
  Author: LandlineScrubber Team
6
6
  Requires-Python: >=3.12,<4.0
@@ -1,253 +1,253 @@
1
- # AI LLS Library
2
-
3
- Core business logic library and CLI tools for Landline Scrubber - phone verification and DNC checking.
4
-
5
- ## Version 2.1.0 - Streaming & Provider Architecture
6
-
7
- New features:
8
- - **Streaming support** for large CSV files to reduce memory usage
9
- - **Provider architecture** for clean separation of verification logic
10
- - **Contract tests** ensuring all providers behave consistently
11
-
12
- ## Version 2.0.0 - Breaking Changes
13
-
14
- This is a greenfield rewrite with no backwards compatibility:
15
- - All file-based CSV processing replaced with text-based methods
16
- - Removed `_sync` suffix from all methods (everything is sync)
17
- - `process_csv_sync(file_path)` → `process_csv(csv_text)`
18
- - `generate_results_csv(...)` now returns CSV string instead of writing to file
19
-
20
- ## Features
21
-
22
- - Phone number normalization (E.164 format)
23
- - Line type detection (mobile/landline/voip)
24
- - DNC (Do Not Call) list checking
25
- - DynamoDB caching with 30-day TTL
26
- - Bulk CSV processing
27
- - Infrastructure-aware CLI for admin operations
28
- - AWS Lambda PowerTools integration
29
-
30
- ## Installation
31
-
32
- ```bash
33
- # Install library with Poetry
34
- poetry install
35
-
36
- # Install CLI globally
37
- pip install -e .
38
- ```
39
-
40
- ## Library Usage
41
-
42
- ### Single Phone Verification
43
-
44
- ```python
45
- from ai_lls_lib import PhoneVerifier, DynamoDBCache
46
-
47
- cache = DynamoDBCache(table_name="phone-cache")
48
- verifier = PhoneVerifier(cache)
49
-
50
- result = verifier.verify("+15551234567")
51
- print(f"Line type: {result.line_type}")
52
- print(f"DNC: {result.dnc}")
53
- print(f"From cache: {result.cached}")
54
- ```
55
-
56
- ### Bulk Processing
57
-
58
- ```python
59
- from ai_lls_lib import BulkProcessor, PhoneVerifier, DynamoDBCache
60
-
61
- cache = DynamoDBCache(table_name="phone-cache")
62
- verifier = PhoneVerifier(cache)
63
- processor = BulkProcessor(verifier)
64
-
65
- # Process CSV text content
66
- csv_text = "name,phone\nJohn,+15551234567\nJane,+15551234568"
67
- results = processor.process_csv(csv_text)
68
-
69
- # Generate results CSV
70
- results_csv = processor.generate_results_csv(csv_text, results)
71
- print(results_csv) # CSV string with added line_type, dnc, cached columns
72
- ```
73
-
74
- ### Streaming Large Files
75
-
76
- For memory-efficient processing of large CSV files:
77
-
78
- ```python
79
- from ai_lls_lib import BulkProcessor, PhoneVerifier, DynamoDBCache
80
-
81
- cache = DynamoDBCache(table_name="phone-cache")
82
- verifier = PhoneVerifier(cache)
83
- processor = BulkProcessor(verifier)
84
-
85
- # Process CSV as a stream, yielding batches
86
- csv_lines = open('large_file.csv').readlines()
87
- for batch in processor.process_csv_stream(csv_lines, batch_size=100):
88
- print(f"Processed batch of {len(batch)} phones")
89
- # Each batch is a list of PhoneVerification objects
90
- ```
91
-
92
- ### Custom Verification Providers
93
-
94
- Use different verification providers based on your needs:
95
-
96
- ```python
97
- from ai_lls_lib import PhoneVerifier, DynamoDBCache
98
- from ai_lls_lib.providers import StubProvider
99
-
100
- # Use stub provider for testing
101
- cache = DynamoDBCache(table_name="phone-cache")
102
- provider = StubProvider() # Deterministic testing provider
103
- verifier = PhoneVerifier(cache, provider=provider)
104
-
105
- # When external APIs are ready, switch to:
106
- # from ai_lls_lib.providers.external import ExternalAPIProvider
107
- # provider = ExternalAPIProvider(phone_api_key="...", dnc_api_key="...")
108
- ```
109
-
110
- ## CLI Usage
111
-
112
- The `ai-lls` CLI provides infrastructure-aware administrative tools:
113
-
114
- ### Verification Commands
115
- ```bash
116
- # Verify single phone
117
- ai-lls verify phone +15551234567 --stack landline-api
118
-
119
- # Bulk verify CSV
120
- ai-lls verify bulk input.csv -o output.csv --stack landline-api
121
- ```
122
-
123
- ### Cache Management
124
- ```bash
125
- # Show cache statistics
126
- ai-lls cache stats --stack landline-api
127
-
128
- # Get cached entry
129
- ai-lls cache get +15551234567 --stack landline-api
130
-
131
- # Invalidate cache entry
132
- ai-lls cache invalidate +15551234567 --stack landline-api
133
-
134
- # Clear old entries
135
- ai-lls cache clear --older-than 20 --stack landline-api
136
- ```
137
-
138
- ### Administrative Commands
139
- ```bash
140
- # Manage user credits
141
- ai-lls admin user-credits user123 --add 100
142
- ai-lls admin user-credits user123 --set 500
143
-
144
- # List API keys
145
- ai-lls admin api-keys --user user123
146
-
147
- # Check queue status
148
- ai-lls admin queue-stats
149
-
150
- # View secrets (masked)
151
- ai-lls admin secrets --stack landline-api
152
- ```
153
-
154
- ### Test Stack Management
155
- ```bash
156
- # Deploy test stack
157
- ai-lls test-stack deploy
158
-
159
- # Check status
160
- ai-lls test-stack status
161
-
162
- # Run integration tests
163
- ai-lls test-stack test
164
-
165
- # Delete test stack
166
- ai-lls test-stack delete
167
- ```
168
-
169
- ## Project Structure
170
-
171
- ```
172
- ai-lls-lib/
173
- ├── src/ai_lls_lib/
174
- │ ├── core/ # Business logic (infrastructure-agnostic)
175
- │ │ ├── models.py # Pydantic models
176
- │ │ ├── verifier.py # Phone verification
177
- │ │ ├── processor.py # Bulk processing
178
- │ │ └── cache.py # DynamoDB cache
179
- │ ├── cli/ # Infrastructure-aware CLI
180
- │ │ ├── __main__.py # Entry point
181
- │ │ ├── commands/ # Command modules
182
- │ │ └── aws_client.py # AWS operations
183
- │ └── testing/ # Test utilities
184
- │ └── fixtures.py # Test data
185
- ├── tests/
186
- │ ├── unit/ # Mocked tests
187
- │ └── integration/ # AWS integration tests
188
- └── test-stack.yaml # Test infrastructure
189
- ```
190
-
191
- ## Testing
192
-
193
- ```bash
194
- # Run unit tests (mocked AWS)
195
- poetry run pytest tests/unit -v
196
-
197
- # Deploy test stack for integration tests
198
- ai-lls test-stack deploy
199
-
200
- # Run integration tests (requires test stack)
201
- TEST_STACK_NAME=ai-lls-lib-test poetry run pytest tests/integration -v
202
-
203
- # All tests with coverage
204
- poetry run pytest --cov=src --cov-report=html
205
-
206
- # Clean up
207
- ai-lls test-stack delete
208
- ```
209
-
210
- ## Development
211
-
212
- ### Current Stub Implementation
213
-
214
- For demo purposes, verification uses stub logic based on last digit:
215
- - Ends in 3: mobile, not on DNC
216
- - Ends in 2: landline, not on DNC
217
- - Ends in 1: mobile, on DNC
218
- - Ends in 0: landline, on DNC
219
- - Otherwise: mobile, not on DNC
220
-
221
- TODO markers indicate where real API integration will be added.
222
-
223
- ### Code Quality
224
-
225
- ```bash
226
- # Format code
227
- poetry run black src/ tests/
228
- poetry run isort src/ tests/
229
-
230
- # Type checking
231
- poetry run mypy src/
232
-
233
- # Run pre-commit hooks
234
- pre-commit run --all-files
235
- ```
236
-
237
- ## Environment Variables
238
-
239
- - `DNC_API_KEY` - DNC verification API key
240
- - `DNC_CHECK_API_KEY` - Alternative DNC service
241
- - `PHONE_VERIFY_API_KEY` - Line type verification
242
- - `AWS_REGION` - AWS region (default: us-east-1)
243
- - `AWS_PROFILE` - AWS profile for CLI operations
244
-
245
- ## License
246
-
247
- Proprietary - All rights reserved
248
-
249
- ## Release Process
250
-
251
- This library uses semantic versioning and publishes to:
252
- - TestPyPI on dev branch pushes (pre-release versions)
253
- - PyPI on main branch pushes (stable releases)
1
+ # AI LLS Library
2
+
3
+ Core business logic library and CLI tools for Landline Scrubber - phone verification and DNC checking.
4
+
5
+ ## Version 2.1.0 - Streaming & Provider Architecture
6
+
7
+ New features:
8
+ - **Streaming support** for large CSV files to reduce memory usage
9
+ - **Provider architecture** for clean separation of verification logic
10
+ - **Contract tests** ensuring all providers behave consistently
11
+
12
+ ## Version 2.0.0 - Breaking Changes
13
+
14
+ This is a greenfield rewrite with no backwards compatibility:
15
+ - All file-based CSV processing replaced with text-based methods
16
+ - Removed `_sync` suffix from all methods (everything is sync)
17
+ - `process_csv_sync(file_path)` → `process_csv(csv_text)`
18
+ - `generate_results_csv(...)` now returns CSV string instead of writing to file
19
+
20
+ ## Features
21
+
22
+ - Phone number normalization (E.164 format)
23
+ - Line type detection (mobile/landline/voip)
24
+ - DNC (Do Not Call) list checking
25
+ - DynamoDB caching with 30-day TTL
26
+ - Bulk CSV processing
27
+ - Infrastructure-aware CLI for admin operations
28
+ - AWS Lambda PowerTools integration
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ # Install library with Poetry
34
+ poetry install
35
+
36
+ # Install CLI globally
37
+ pip install -e .
38
+ ```
39
+
40
+ ## Library Usage
41
+
42
+ ### Single Phone Verification
43
+
44
+ ```python
45
+ from ai_lls_lib import PhoneVerifier, DynamoDBCache
46
+
47
+ cache = DynamoDBCache(table_name="phone-cache")
48
+ verifier = PhoneVerifier(cache)
49
+
50
+ result = verifier.verify("+15551234567")
51
+ print(f"Line type: {result.line_type}")
52
+ print(f"DNC: {result.dnc}")
53
+ print(f"From cache: {result.cached}")
54
+ ```
55
+
56
+ ### Bulk Processing
57
+
58
+ ```python
59
+ from ai_lls_lib import BulkProcessor, PhoneVerifier, DynamoDBCache
60
+
61
+ cache = DynamoDBCache(table_name="phone-cache")
62
+ verifier = PhoneVerifier(cache)
63
+ processor = BulkProcessor(verifier)
64
+
65
+ # Process CSV text content
66
+ csv_text = "name,phone\nJohn,+15551234567\nJane,+15551234568"
67
+ results = processor.process_csv(csv_text)
68
+
69
+ # Generate results CSV
70
+ results_csv = processor.generate_results_csv(csv_text, results)
71
+ print(results_csv) # CSV string with added line_type, dnc, cached columns
72
+ ```
73
+
74
+ ### Streaming Large Files
75
+
76
+ For memory-efficient processing of large CSV files:
77
+
78
+ ```python
79
+ from ai_lls_lib import BulkProcessor, PhoneVerifier, DynamoDBCache
80
+
81
+ cache = DynamoDBCache(table_name="phone-cache")
82
+ verifier = PhoneVerifier(cache)
83
+ processor = BulkProcessor(verifier)
84
+
85
+ # Process CSV as a stream, yielding batches
86
+ csv_lines = open('large_file.csv').readlines()
87
+ for batch in processor.process_csv_stream(csv_lines, batch_size=100):
88
+ print(f"Processed batch of {len(batch)} phones")
89
+ # Each batch is a list of PhoneVerification objects
90
+ ```
91
+
92
+ ### Custom Verification Providers
93
+
94
+ Use different verification providers based on your needs:
95
+
96
+ ```python
97
+ from ai_lls_lib import PhoneVerifier, DynamoDBCache
98
+ from ai_lls_lib.providers import StubProvider
99
+
100
+ # Use stub provider for testing
101
+ cache = DynamoDBCache(table_name="phone-cache")
102
+ provider = StubProvider() # Deterministic testing provider
103
+ verifier = PhoneVerifier(cache, provider=provider)
104
+
105
+ # When external APIs are ready, switch to:
106
+ # from ai_lls_lib.providers.external import ExternalAPIProvider
107
+ # provider = ExternalAPIProvider(phone_api_key="...", dnc_api_key="...")
108
+ ```
109
+
110
+ ## CLI Usage
111
+
112
+ The `ai-lls` CLI provides infrastructure-aware administrative tools:
113
+
114
+ ### Verification Commands
115
+ ```bash
116
+ # Verify single phone
117
+ ai-lls verify phone +15551234567 --stack landline-api
118
+
119
+ # Bulk verify CSV
120
+ ai-lls verify bulk input.csv -o output.csv --stack landline-api
121
+ ```
122
+
123
+ ### Cache Management
124
+ ```bash
125
+ # Show cache statistics
126
+ ai-lls cache stats --stack landline-api
127
+
128
+ # Get cached entry
129
+ ai-lls cache get +15551234567 --stack landline-api
130
+
131
+ # Invalidate cache entry
132
+ ai-lls cache invalidate +15551234567 --stack landline-api
133
+
134
+ # Clear old entries
135
+ ai-lls cache clear --older-than 20 --stack landline-api
136
+ ```
137
+
138
+ ### Administrative Commands
139
+ ```bash
140
+ # Manage user credits
141
+ ai-lls admin user-credits user123 --add 100
142
+ ai-lls admin user-credits user123 --set 500
143
+
144
+ # List API keys
145
+ ai-lls admin api-keys --user user123
146
+
147
+ # Check queue status
148
+ ai-lls admin queue-stats
149
+
150
+ # View secrets (masked)
151
+ ai-lls admin secrets --stack landline-api
152
+ ```
153
+
154
+ ### Test Stack Management
155
+ ```bash
156
+ # Deploy test stack
157
+ ai-lls test-stack deploy
158
+
159
+ # Check status
160
+ ai-lls test-stack status
161
+
162
+ # Run integration tests
163
+ ai-lls test-stack test
164
+
165
+ # Delete test stack
166
+ ai-lls test-stack delete
167
+ ```
168
+
169
+ ## Project Structure
170
+
171
+ ```
172
+ ai-lls-lib/
173
+ ├── src/ai_lls_lib/
174
+ │ ├── core/ # Business logic (infrastructure-agnostic)
175
+ │ │ ├── models.py # Pydantic models
176
+ │ │ ├── verifier.py # Phone verification
177
+ │ │ ├── processor.py # Bulk processing
178
+ │ │ └── cache.py # DynamoDB cache
179
+ │ ├── cli/ # Infrastructure-aware CLI
180
+ │ │ ├── __main__.py # Entry point
181
+ │ │ ├── commands/ # Command modules
182
+ │ │ └── aws_client.py # AWS operations
183
+ │ └── testing/ # Test utilities
184
+ │ └── fixtures.py # Test data
185
+ ├── tests/
186
+ │ ├── unit/ # Mocked tests
187
+ │ └── integration/ # AWS integration tests
188
+ └── test-stack.yaml # Test infrastructure
189
+ ```
190
+
191
+ ## Testing
192
+
193
+ ```bash
194
+ # Run unit tests (mocked AWS)
195
+ poetry run pytest tests/unit -v
196
+
197
+ # Deploy test stack for integration tests
198
+ ai-lls test-stack deploy
199
+
200
+ # Run integration tests (requires test stack)
201
+ TEST_STACK_NAME=ai-lls-lib-test poetry run pytest tests/integration -v
202
+
203
+ # All tests with coverage
204
+ poetry run pytest --cov=src --cov-report=html
205
+
206
+ # Clean up
207
+ ai-lls test-stack delete
208
+ ```
209
+
210
+ ## Development
211
+
212
+ ### Current Stub Implementation
213
+
214
+ For demo purposes, verification uses stub logic based on last digit:
215
+ - Ends in 3: mobile, not on DNC
216
+ - Ends in 2: landline, not on DNC
217
+ - Ends in 1: mobile, on DNC
218
+ - Ends in 0: landline, on DNC
219
+ - Otherwise: mobile, not on DNC
220
+
221
+ TODO markers indicate where real API integration will be added.
222
+
223
+ ### Code Quality
224
+
225
+ ```bash
226
+ # Format code
227
+ poetry run black src/ tests/
228
+ poetry run isort src/ tests/
229
+
230
+ # Type checking
231
+ poetry run mypy src/
232
+
233
+ # Run pre-commit hooks
234
+ pre-commit run --all-files
235
+ ```
236
+
237
+ ## Environment Variables
238
+
239
+ - `DNC_API_KEY` - DNC verification API key
240
+ - `DNC_CHECK_API_KEY` - Alternative DNC service
241
+ - `PHONE_VERIFY_API_KEY` - Line type verification
242
+ - `AWS_REGION` - AWS region (default: us-east-1)
243
+ - `AWS_PROFILE` - AWS profile for CLI operations
244
+
245
+ ## License
246
+
247
+ Proprietary - All rights reserved
248
+
249
+ ## Release Process
250
+
251
+ This library uses semantic versioning and publishes to:
252
+ - TestPyPI on dev branch pushes (pre-release versions)
253
+ - PyPI on main branch pushes (stable releases)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ai-lls-lib"
3
- version = "1.4.0-rc.2"
3
+ version = "1.4.0-rc.4"
4
4
  description = "Landline Scrubber core library - phone verification and DNC checking"
5
5
  authors = ["LandlineScrubber Team"]
6
6
  readme = "README.md"
@@ -13,7 +13,7 @@ from ai_lls_lib.core.verifier import PhoneVerifier
13
13
  from ai_lls_lib.core.processor import BulkProcessor
14
14
  from ai_lls_lib.core.cache import DynamoDBCache
15
15
 
16
- __version__ = "1.4.0-rc.2"
16
+ __version__ = "1.4.0-rc.4"
17
17
  __all__ = [
18
18
  "PhoneVerification",
19
19
  "BulkJob",
@@ -1,4 +1,4 @@
1
- """Auth module for handling authentication and authorization."""
2
- from .context_parser import get_email_from_event, get_user_from_event
3
-
4
- __all__ = ["get_user_from_event", "get_email_from_event"]
1
+ """Auth module for handling authentication and authorization."""
2
+ from .context_parser import get_email_from_event, get_user_from_event
3
+
4
+ __all__ = ["get_user_from_event", "get_email_from_event"]
@@ -1,68 +1,68 @@
1
- """Auth context parser for HTTP API v2.0 events."""
2
- from typing import Any, Dict, Optional
3
-
4
-
5
- def get_user_from_event(event: Dict[str, Any]) -> Optional[str]:
6
- """
7
- Extract user ID from HTTP API v2.0 event with all possible paths.
8
- Handles both JWT and API key authentication contexts.
9
-
10
- This function handles the complexities of AWS API Gateway authorizer contexts,
11
- especially when EnableSimpleResponses is set to false, which wraps the
12
- context in a 'lambda' key.
13
-
14
- Args:
15
- event: The Lambda event from API Gateway HTTP API v2.0
16
-
17
- Returns:
18
- User ID string if found, None otherwise
19
- """
20
- request_context = event.get("requestContext", {})
21
- auth = request_context.get("authorizer", {})
22
-
23
- # Handle lambda-wrapped context (EnableSimpleResponses: false)
24
- # When EnableSimpleResponses is false, the authorizer context is wrapped
25
- lam_ctx = auth.get("lambda", auth) if isinstance(auth.get("lambda"), dict) else auth
26
-
27
- # Try all possible paths for user_id in priority order
28
- user_id = (
29
- # Lambda authorizer paths (most common with current setup)
30
- lam_ctx.get("principal_id") or
31
- lam_ctx.get("principalId") or
32
- lam_ctx.get("sub") or
33
- lam_ctx.get("user_id") or
34
- # JWT paths (when using JWT authorizer directly)
35
- auth.get("jwt", {}).get("claims", {}).get("sub") or
36
- # Direct auth paths (fallback)
37
- auth.get("principal_id") or
38
- auth.get("principalId") or
39
- auth.get("sub")
40
- )
41
-
42
- return user_id
43
-
44
-
45
- def get_email_from_event(event: Dict[str, Any]) -> Optional[str]:
46
- """
47
- Extract email from HTTP API v2.0 event.
48
-
49
- Args:
50
- event: The Lambda event from API Gateway HTTP API v2.0
51
-
52
- Returns:
53
- Email string if found, None otherwise
54
- """
55
- request_context = event.get("requestContext", {})
56
- auth = request_context.get("authorizer", {})
57
-
58
- # Handle lambda-wrapped context
59
- lam_ctx = auth.get("lambda", auth) if isinstance(auth.get("lambda"), dict) else auth
60
-
61
- # Try to get email from various locations
62
- email = (
63
- lam_ctx.get("email") or
64
- auth.get("jwt", {}).get("claims", {}).get("email") or
65
- auth.get("email")
66
- )
67
-
68
- return email
1
+ """Auth context parser for HTTP API v2.0 events."""
2
+ from typing import Any, Dict, Optional
3
+
4
+
5
+ def get_user_from_event(event: Dict[str, Any]) -> Optional[str]:
6
+ """
7
+ Extract user ID from HTTP API v2.0 event with all possible paths.
8
+ Handles both JWT and API key authentication contexts.
9
+
10
+ This function handles the complexities of AWS API Gateway authorizer contexts,
11
+ especially when EnableSimpleResponses is set to false, which wraps the
12
+ context in a 'lambda' key.
13
+
14
+ Args:
15
+ event: The Lambda event from API Gateway HTTP API v2.0
16
+
17
+ Returns:
18
+ User ID string if found, None otherwise
19
+ """
20
+ request_context = event.get("requestContext", {})
21
+ auth = request_context.get("authorizer", {})
22
+
23
+ # Handle lambda-wrapped context (EnableSimpleResponses: false)
24
+ # When EnableSimpleResponses is false, the authorizer context is wrapped
25
+ lam_ctx = auth.get("lambda", auth) if isinstance(auth.get("lambda"), dict) else auth
26
+
27
+ # Try all possible paths for user_id in priority order
28
+ user_id = (
29
+ # Lambda authorizer paths (most common with current setup)
30
+ lam_ctx.get("principal_id") or
31
+ lam_ctx.get("principalId") or
32
+ lam_ctx.get("sub") or
33
+ lam_ctx.get("user_id") or
34
+ # JWT paths (when using JWT authorizer directly)
35
+ auth.get("jwt", {}).get("claims", {}).get("sub") or
36
+ # Direct auth paths (fallback)
37
+ auth.get("principal_id") or
38
+ auth.get("principalId") or
39
+ auth.get("sub")
40
+ )
41
+
42
+ return user_id
43
+
44
+
45
+ def get_email_from_event(event: Dict[str, Any]) -> Optional[str]:
46
+ """
47
+ Extract email from HTTP API v2.0 event.
48
+
49
+ Args:
50
+ event: The Lambda event from API Gateway HTTP API v2.0
51
+
52
+ Returns:
53
+ Email string if found, None otherwise
54
+ """
55
+ request_context = event.get("requestContext", {})
56
+ auth = request_context.get("authorizer", {})
57
+
58
+ # Handle lambda-wrapped context
59
+ lam_ctx = auth.get("lambda", auth) if isinstance(auth.get("lambda"), dict) else auth
60
+
61
+ # Try to get email from various locations
62
+ email = (
63
+ lam_ctx.get("email") or
64
+ auth.get("jwt", {}).get("claims", {}).get("email") or
65
+ auth.get("email")
66
+ )
67
+
68
+ return email