diff --git a/aws_lambda_powertools/utilities/typing/__init__.py b/aws_lambda_powertools/utilities/typing/__init__.py new file mode 100644 index 00000000000..626a0fd6fcf --- /dev/null +++ b/aws_lambda_powertools/utilities/typing/__init__.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +""" +Typing for developer ease in the IDE +""" + +from .lambda_context import LambdaContext + +__all__ = [ + "LambdaContext", +] diff --git a/aws_lambda_powertools/utilities/typing/lambda_client_context.py b/aws_lambda_powertools/utilities/typing/lambda_client_context.py new file mode 100644 index 00000000000..5b9e9506b4c --- /dev/null +++ b/aws_lambda_powertools/utilities/typing/lambda_client_context.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from typing import Any, Dict + +from aws_lambda_powertools.utilities.typing.lambda_client_context_mobile_client import LambdaClientContextMobileClient + + +class LambdaClientContext(object): + _client: LambdaClientContextMobileClient + _custom: Dict[str, Any] + _env: Dict[str, Any] + + @property + def client(self) -> LambdaClientContextMobileClient: + """Client context that's provided to Lambda by the client application.""" + return self._client + + @property + def custom(self) -> Dict[str, Any]: + """A dict of custom values set by the mobile client application.""" + return self._custom + + @property + def env(self) -> Dict[str, Any]: + """A dict of environment information provided by the AWS SDK.""" + return self._env diff --git a/aws_lambda_powertools/utilities/typing/lambda_client_context_mobile_client.py b/aws_lambda_powertools/utilities/typing/lambda_client_context_mobile_client.py new file mode 100644 index 00000000000..bd204891d2b --- /dev/null +++ b/aws_lambda_powertools/utilities/typing/lambda_client_context_mobile_client.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- + + +class LambdaClientContextMobileClient(object): + """Mobile Client context that's provided to Lambda by the client application.""" + + _installation_id: str + _app_title: str + _app_version_name: str + _app_version_code: str + _app_package_name: str + + @property + def installation_id(self) -> str: + return self._installation_id + + @property + def app_title(self) -> str: + return self._app_title + + @property + def app_version_name(self) -> str: + return self._app_version_name + + @property + def app_version_code(self) -> str: + return self._app_version_code + + @property + def app_package_name(self) -> str: + return self._app_package_name diff --git a/aws_lambda_powertools/utilities/typing/lambda_cognito_identity.py b/aws_lambda_powertools/utilities/typing/lambda_cognito_identity.py new file mode 100644 index 00000000000..9cced6a59c2 --- /dev/null +++ b/aws_lambda_powertools/utilities/typing/lambda_cognito_identity.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + + +class LambdaCognitoIdentity(object): + """ + Information about the Amazon Cognito identity that authorized the request. + """ + + _cognito_identity_id: str + _cognito_identity_pool_id: str + + @property + def cognito_identity_id(self) -> str: + """The authenticated Amazon Cognito identity.""" + return self._cognito_identity_id + + @property + def cognito_identity_pool_id(self) -> str: + """The Amazon Cognito identity pool that authorized the invocation.""" + return self._cognito_identity_pool_id diff --git a/aws_lambda_powertools/utilities/typing/lambda_context.py b/aws_lambda_powertools/utilities/typing/lambda_context.py new file mode 100644 index 00000000000..6db486412a3 --- /dev/null +++ b/aws_lambda_powertools/utilities/typing/lambda_context.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from aws_lambda_powertools.utilities.typing.lambda_client_context import LambdaClientContext +from aws_lambda_powertools.utilities.typing.lambda_cognito_identity import LambdaCognitoIdentity + + +class LambdaContext(object): + """The LambdaContext static object can be used to ease the development by providing the IDE type hints. + + Example + ------- + **A Lambda function using LambdaContext** + + >>> from aws_lambda_powertools.utilities.typing import LambdaContext + >>> + >>> def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]: + >>> # Insert business logic + >>> return event + + """ + + _function_name: str + _function_version: str + _invoked_function_arn: str + _memory_limit_in_mb: int + _aws_request_id: str + _log_group_name: str + _log_stream_name: str + _identity: LambdaCognitoIdentity + _client_context: LambdaClientContext + + @property + def function_name(self) -> str: + """The name of the Lambda function.""" + return self._function_name + + @property + def function_version(self) -> str: + """The version of the function.""" + return self._function_version + + @property + def invoked_function_arn(self) -> str: + """The Amazon Resource Name (ARN) that's used to invoke the function. Indicates if the invoker specified a + version number or alias.""" + return self._invoked_function_arn + + @property + def memory_limit_in_mb(self) -> int: + """The amount of memory that's allocated for the function.""" + return self._memory_limit_in_mb + + @property + def aws_request_id(self) -> str: + """The identifier of the invocation request.""" + return self._aws_request_id + + @property + def log_group_name(self) -> str: + """The log group for the function.""" + return self._log_group_name + + @property + def log_stream_name(self) -> str: + """The log stream for the function instance.""" + return self._log_stream_name + + @property + def identity(self) -> LambdaCognitoIdentity: + """(mobile apps) Information about the Amazon Cognito identity that authorized the request.""" + return self._identity + + @property + def client_context(self) -> LambdaClientContext: + """(mobile apps) Client context that's provided to Lambda by the client application.""" + return self._client_context + + @staticmethod + def get_remaining_time_in_millis() -> int: + """Returns the number of milliseconds left before the execution times out.""" + return 0 diff --git a/docs/content/index.mdx b/docs/content/index.mdx index ea8c88d265a..ec2dd862e38 100644 --- a/docs/content/index.mdx +++ b/docs/content/index.mdx @@ -72,6 +72,7 @@ Utility | Description [Metrics](./core/metrics) | Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF) [Bring your own middleware](.//utilities/middleware_factory) | Decorator factory to create your own middleware to run logic before, and after each Lambda invocation [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 +[Typing utility](./utilities/typing) | Static typing classes to speedup development in your IDE ## Environment variables diff --git a/docs/content/media/utilities_typing.png b/docs/content/media/utilities_typing.png new file mode 100644 index 00000000000..0f293abb6ec Binary files /dev/null and b/docs/content/media/utilities_typing.png differ diff --git a/docs/content/utilities/typing.mdx b/docs/content/utilities/typing.mdx new file mode 100644 index 00000000000..9192b095887 --- /dev/null +++ b/docs/content/utilities/typing.mdx @@ -0,0 +1,25 @@ +--- +title: Typing +description: Utility +--- + +import Note from "../../src/components/Note" + +This typing utility provides static typing classes that can be used to ease the development by providing the IDE type hints. + +![Utilities Typing](../media/utilities_typing.png) + +## LambdaContext + +The `LambdaContext` typing is typically used in the handler method for the Lambda function. + +```python:title=index.py +from typing import Any, Dict +from aws_lambda_powertools.utilities.typing import LambdaContext + +# highlight-start +def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]: +# highlight-end + # Insert business logic + return event +``` diff --git a/docs/gatsby-config.js b/docs/gatsby-config.js index 74afd828716..a4286e0d55f 100644 --- a/docs/gatsby-config.js +++ b/docs/gatsby-config.js @@ -33,6 +33,7 @@ module.exports = { 'utilities/middleware_factory', 'utilities/parameters', 'utilities/batch', + 'utilities/typing', ], }, navConfig: { diff --git a/tests/functional/test_utilities_typing.py b/tests/functional/test_utilities_typing.py new file mode 100644 index 00000000000..b2b4a752d9f --- /dev/null +++ b/tests/functional/test_utilities_typing.py @@ -0,0 +1,6 @@ +from aws_lambda_powertools.utilities.typing import LambdaContext + + +def test_typing(): + context = LambdaContext() + assert context.get_remaining_time_in_millis() == 0