Skip to content

Commit 80a7602

Browse files
authored
Merge pull request #149 from Nr18/static-types
feat: Static Types for AWS Lambda
2 parents 3be8d03 + 7d8a520 commit 80a7602

File tree

10 files changed

+200
-0
lines changed

10 files changed

+200
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Typing for developer ease in the IDE
5+
"""
6+
7+
from .lambda_context import LambdaContext
8+
9+
__all__ = [
10+
"LambdaContext",
11+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
from typing import Any, Dict
3+
4+
from aws_lambda_powertools.utilities.typing.lambda_client_context_mobile_client import LambdaClientContextMobileClient
5+
6+
7+
class LambdaClientContext(object):
8+
_client: LambdaClientContextMobileClient
9+
_custom: Dict[str, Any]
10+
_env: Dict[str, Any]
11+
12+
@property
13+
def client(self) -> LambdaClientContextMobileClient:
14+
"""Client context that's provided to Lambda by the client application."""
15+
return self._client
16+
17+
@property
18+
def custom(self) -> Dict[str, Any]:
19+
"""A dict of custom values set by the mobile client application."""
20+
return self._custom
21+
22+
@property
23+
def env(self) -> Dict[str, Any]:
24+
"""A dict of environment information provided by the AWS SDK."""
25+
return self._env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class LambdaClientContextMobileClient(object):
5+
"""Mobile Client context that's provided to Lambda by the client application."""
6+
7+
_installation_id: str
8+
_app_title: str
9+
_app_version_name: str
10+
_app_version_code: str
11+
_app_package_name: str
12+
13+
@property
14+
def installation_id(self) -> str:
15+
return self._installation_id
16+
17+
@property
18+
def app_title(self) -> str:
19+
return self._app_title
20+
21+
@property
22+
def app_version_name(self) -> str:
23+
return self._app_version_name
24+
25+
@property
26+
def app_version_code(self) -> str:
27+
return self._app_version_code
28+
29+
@property
30+
def app_package_name(self) -> str:
31+
return self._app_package_name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class LambdaCognitoIdentity(object):
5+
"""
6+
Information about the Amazon Cognito identity that authorized the request.
7+
"""
8+
9+
_cognito_identity_id: str
10+
_cognito_identity_pool_id: str
11+
12+
@property
13+
def cognito_identity_id(self) -> str:
14+
"""The authenticated Amazon Cognito identity."""
15+
return self._cognito_identity_id
16+
17+
@property
18+
def cognito_identity_pool_id(self) -> str:
19+
"""The Amazon Cognito identity pool that authorized the invocation."""
20+
return self._cognito_identity_pool_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
from aws_lambda_powertools.utilities.typing.lambda_client_context import LambdaClientContext
3+
from aws_lambda_powertools.utilities.typing.lambda_cognito_identity import LambdaCognitoIdentity
4+
5+
6+
class LambdaContext(object):
7+
"""The LambdaContext static object can be used to ease the development by providing the IDE type hints.
8+
9+
Example
10+
-------
11+
**A Lambda function using LambdaContext**
12+
13+
>>> from aws_lambda_powertools.utilities.typing import LambdaContext
14+
>>>
15+
>>> def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
16+
>>> # Insert business logic
17+
>>> return event
18+
19+
"""
20+
21+
_function_name: str
22+
_function_version: str
23+
_invoked_function_arn: str
24+
_memory_limit_in_mb: int
25+
_aws_request_id: str
26+
_log_group_name: str
27+
_log_stream_name: str
28+
_identity: LambdaCognitoIdentity
29+
_client_context: LambdaClientContext
30+
31+
@property
32+
def function_name(self) -> str:
33+
"""The name of the Lambda function."""
34+
return self._function_name
35+
36+
@property
37+
def function_version(self) -> str:
38+
"""The version of the function."""
39+
return self._function_version
40+
41+
@property
42+
def invoked_function_arn(self) -> str:
43+
"""The Amazon Resource Name (ARN) that's used to invoke the function. Indicates if the invoker specified a
44+
version number or alias."""
45+
return self._invoked_function_arn
46+
47+
@property
48+
def memory_limit_in_mb(self) -> int:
49+
"""The amount of memory that's allocated for the function."""
50+
return self._memory_limit_in_mb
51+
52+
@property
53+
def aws_request_id(self) -> str:
54+
"""The identifier of the invocation request."""
55+
return self._aws_request_id
56+
57+
@property
58+
def log_group_name(self) -> str:
59+
"""The log group for the function."""
60+
return self._log_group_name
61+
62+
@property
63+
def log_stream_name(self) -> str:
64+
"""The log stream for the function instance."""
65+
return self._log_stream_name
66+
67+
@property
68+
def identity(self) -> LambdaCognitoIdentity:
69+
"""(mobile apps) Information about the Amazon Cognito identity that authorized the request."""
70+
return self._identity
71+
72+
@property
73+
def client_context(self) -> LambdaClientContext:
74+
"""(mobile apps) Client context that's provided to Lambda by the client application."""
75+
return self._client_context
76+
77+
@staticmethod
78+
def get_remaining_time_in_millis() -> int:
79+
"""Returns the number of milliseconds left before the execution times out."""
80+
return 0

docs/content/index.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Utility | Description
7272
[Metrics](./core/metrics) | Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
7373
[Bring your own middleware](.//utilities/middleware_factory) | Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
7474
[Parameters utility](./utilities/parameters) | Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
75+
[Typing utility](./utilities/typing) | Static typing classes to speedup development in your IDE
7576

7677
## Environment variables
7778

79.9 KB
Loading

docs/content/utilities/typing.mdx

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Typing
3+
description: Utility
4+
---
5+
6+
import Note from "../../src/components/Note"
7+
8+
This typing utility provides static typing classes that can be used to ease the development by providing the IDE type hints.
9+
10+
![Utilities Typing](../media/utilities_typing.png)
11+
12+
## LambdaContext
13+
14+
The `LambdaContext` typing is typically used in the handler method for the Lambda function.
15+
16+
```python:title=index.py
17+
from typing import Any, Dict
18+
from aws_lambda_powertools.utilities.typing import LambdaContext
19+
20+
# highlight-start
21+
def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
22+
# highlight-end
23+
# Insert business logic
24+
return event
25+
```

docs/gatsby-config.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
'utilities/middleware_factory',
3434
'utilities/parameters',
3535
'utilities/batch',
36+
'utilities/typing',
3637
],
3738
},
3839
navConfig: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from aws_lambda_powertools.utilities.typing import LambdaContext
2+
3+
4+
def test_typing():
5+
context = LambdaContext()
6+
assert context.get_remaining_time_in_millis() == 0

0 commit comments

Comments
 (0)