Skip to content

Refactor validate_event into helper, use issubset method #7

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
44 changes: 4 additions & 40 deletions unicorn_contracts/src/contracts_service/create_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@

from contracts_service.contract_status import ContractStatus
from contracts_service.exceptions import EventValidationException
from contracts_service.helper import get_current_date, get_event_body, publish_event
from contracts_service.helper import get_current_date, get_event_body, validate_event, publish_event, get_env

# Initialise Environment variables
if (SERVICE_NAMESPACE := os.environ.get("SERVICE_NAMESPACE")) is None:
raise EnvironmentError("SERVICE_NAMESPACE environment variable is undefined")
if (DYNAMODB_TABLE := os.environ.get("DYNAMODB_TABLE")) is None:
raise EnvironmentError("DYNAMODB_TABLE environment variable is undefined")
SERVICE_NAMESPACE = get_env("SERVICE_NAMESPACE")
DYNAMODB_TABLE = get_env("DYNAMODB_TABLE")

# Initialise PowerTools
logger: Logger = Logger()
Expand Down Expand Up @@ -50,7 +48,7 @@ def lambda_handler(event, context):
"""
# Get contract and property details from the event
try:
event_json = validate_event(event)
event_json = validate_event(event, {"property_id", "address", "seller_name"})
except EventValidationException as ex:
return ex.apigw_return

Expand Down Expand Up @@ -98,37 +96,3 @@ def create_contract(contract) -> dict:
"""
# TODO: create entry in DDB for new contract
return table.put_item(Item=contract,)


@tracer.capture_method
def validate_event(event):
"""Validates the body of the API Gateway event

Parameters
----------
event : dict
API Gateway event

Returns
-------
dict
The body of the API

Raises
------
EventValidationException
The ``Raises`` section is a list of all exceptions
that are relevant to the interface.
"""

try:
event_json = get_event_body(event)
except Exception as ex:
logger.exception(ex)
raise EventValidationException() from ex

for i in ["property_id", "address", "seller_name"]:
if i not in event_json.keys():
raise EventValidationException()

return event_json
40 changes: 40 additions & 0 deletions unicorn_contracts/src/contracts_service/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,43 @@ def publish_event(contract, request_id):
}
]
)

@tracer.capture_method
def validate_event(event, required_keys):
"""Validates the body of the API Gateway event

Parameters
----------
event : dict
API Gateway event
required_keys: set
Set of required field names

Returns
-------
dict
The body of the API

Raises
------
EventValidationException
The ``Raises`` section is a list of all exceptions
that are relevant to the interface.
"""

try:
event_json = get_event_body(event)
except Exception as ex:
logger.exception(ex)
raise EventValidationException() from ex

if not required_keys.issubset(event_json.keys()):
raise EventValidationException()

return event_json

def get_env(name):
try:
return os.environ[name]
except KeyError:
raise EnvironmentError("%s environment variable is undefined" % name)
44 changes: 4 additions & 40 deletions unicorn_contracts/src/contracts_service/update_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
from contracts_service.contract_status import ContractStatus
from contracts_service.exceptions import (ContractNotFoundException,
EventValidationException)
from contracts_service.helper import get_current_date, get_event_body, publish_event
from contracts_service.helper import get_current_date, get_event_body, publish_event, validate_event, get_env

# Initialise Environment variables
if (SERVICE_NAMESPACE := os.environ.get("SERVICE_NAMESPACE")) is None:
raise EnvironmentError("SERVICE_NAMESPACE environment variable is undefined")

if (DYNAMODB_TABLE := os.environ.get("DYNAMODB_TABLE")) is None:
raise EnvironmentError("DYNAMODB_TABLE environment variable is undefined")
SERVICE_NAMESPACE = get_env("SERVICE_NAMESPACE")
DYNAMODB_TABLE = get_env("DYNAMODB_TABLE")

# Initialise PowerTools
logger: Logger = Logger()
Expand Down Expand Up @@ -53,7 +50,7 @@ def lambda_handler(event, context):

# Get contract and property details from the event
try:
event_json = validate_event(event)
event_json = validate_event(event, {"property_id"})
except EventValidationException as ex:
return ex.apigw_return

Expand Down Expand Up @@ -155,36 +152,3 @@ def get_existing_contract(property_id: str) -> dict:
raise error
except KeyError as _:
raise ContractNotFoundException() from _


@tracer.capture_method
def validate_event(event):
"""Validates the body of the API Gateway event

Parameters
----------
event : dict
API Gateway event

Returns
-------
dict
The body of the API

Raises
------
EventValidationException
The ``Raises`` section is a list of all exceptions
that are relevant to the interface.
"""
try:
event_json = get_event_body(event)
except Exception as ex:
logger.exception(ex)
raise EventValidationException() from ex

for i in ["property_id"]:
if i not in event_json.keys():
raise EventValidationException()

return event_json