Skip to content

chore(shared): fix cyclic import & refactor data extraction fn #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aws_lambda_powertools/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Shared exceptions that don't belong to a single utility"""


class InvalidEnvelopeExpressionError(Exception):
"""When JMESPath fails to parse expression"""
8 changes: 5 additions & 3 deletions aws_lambda_powertools/shared/jmespath_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import base64
import gzip
import json
import logging
from typing import Any, Dict, Optional, Union

import jmespath
from jmespath.exceptions import LexerError

from aws_lambda_powertools.utilities.validation import InvalidEnvelopeExpressionError
from aws_lambda_powertools.utilities.validation.base import logger
from aws_lambda_powertools.exceptions import InvalidEnvelopeExpressionError

logger = logging.getLogger(__name__)


class PowertoolsFunctions(jmespath.functions.Functions):
Expand All @@ -27,7 +29,7 @@ def _func_powertools_base64_gzip(self, value):
return uncompressed.decode()


def unwrap_event_from_envelope(data: Union[Dict, str], envelope: str, jmespath_options: Optional[Dict]) -> Any:
def extract_data_from_envelope(data: Union[Dict, str], envelope: str, jmespath_options: Optional[Dict]) -> Any:
"""Searches data using JMESPath expression

Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def get_configuration(self) -> Dict[str, Any]:
)

if self.envelope:
config = jmespath_utils.unwrap_event_from_envelope(
config = jmespath_utils.extract_data_from_envelope(
data=config, envelope=self.envelope, jmespath_options=self.jmespath_options
)

Expand Down
6 changes: 4 additions & 2 deletions aws_lambda_powertools/utilities/validation/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from ...exceptions import InvalidEnvelopeExpressionError


class SchemaValidationError(Exception):
"""When serialization fail schema validation"""

Expand All @@ -6,5 +9,4 @@ class InvalidSchemaFormatError(Exception):
"""When JSON Schema is in invalid format"""


class InvalidEnvelopeExpressionError(Exception):
"""When JMESPath fails to parse expression"""
__all__ = ["SchemaValidationError", "InvalidSchemaFormatError", "InvalidEnvelopeExpressionError"]
4 changes: 2 additions & 2 deletions aws_lambda_powertools/utilities/validation/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def handler(event, context):
When JMESPath expression to unwrap event is invalid
"""
if envelope:
event = jmespath_utils.unwrap_event_from_envelope(
event = jmespath_utils.extract_data_from_envelope(
data=event, envelope=envelope, jmespath_options=jmespath_options
)

Expand Down Expand Up @@ -219,7 +219,7 @@ def handler(event, context):
When JMESPath expression to unwrap event is invalid
"""
if envelope:
event = jmespath_utils.unwrap_event_from_envelope(
event = jmespath_utils.extract_data_from_envelope(
data=event, envelope=envelope, jmespath_options=jmespath_options
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pydantic = ["pydantic", "email-validator"]

[tool.coverage.run]
source = ["aws_lambda_powertools"]
omit = ["tests/*"]
omit = ["tests/*", "aws_lambda_powertools/exceptions/*"]
branch = true

[tool.coverage.html]
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/idempotency/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from botocore.config import Config
from jmespath import functions

from aws_lambda_powertools.shared.jmespath_utils import unwrap_event_from_envelope
from aws_lambda_powertools.shared.jmespath_utils import extract_data_from_envelope
from aws_lambda_powertools.shared.json_encoder import Encoder
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer
from aws_lambda_powertools.utilities.idempotency.idempotency import IdempotencyConfig
Expand Down Expand Up @@ -149,7 +149,7 @@ def hashed_idempotency_key(lambda_apigw_event, default_jmespath, lambda_context)

@pytest.fixture
def hashed_idempotency_key_with_envelope(lambda_apigw_event):
event = unwrap_event_from_envelope(
event = extract_data_from_envelope(
data=lambda_apigw_event, envelope=envelopes.API_GATEWAY_HTTP, jmespath_options={}
)
return "test-func#" + hashlib.md5(json.dumps(event).encode()).hexdigest()
Expand Down