Skip to content

Commit

Permalink
REF: Rename functions to use "event" instead of "message"
Browse files Browse the repository at this point in the history
  • Loading branch information
cortadocodes committed Nov 21, 2023
1 parent 67b0849 commit 232bb35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions octue/cloud/pub_sub/message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from octue.cloud import EXCEPTIONS_MAPPING
from octue.cloud.pub_sub.messages import extract_event_and_attributes_from_pub_sub
from octue.cloud.validation import SERVICE_COMMUNICATION_SCHEMA, is_message_valid
from octue.cloud.validation import SERVICE_COMMUNICATION_SCHEMA, is_event_valid
from octue.definitions import GOOGLE_COMPUTE_PROVIDERS
from octue.log_handlers import COLOUR_PALETTE
from octue.resources.manifest import Manifest
Expand Down Expand Up @@ -232,8 +232,8 @@ def _pull_and_enqueue_message(self, timeout):
if not self._child_sdk_version:
self._child_sdk_version = attributes["octue_sdk_version"]

if not is_message_valid(
message=event,
if not is_event_valid(
event=event,
attributes=attributes,
receiving_service=self.receiving_service,
parent_sdk_version=importlib.metadata.version("octue"),
Expand Down
6 changes: 3 additions & 3 deletions octue/cloud/pub_sub/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
split_service_id,
validate_sruid,
)
from octue.cloud.validation import raise_if_message_is_invalid
from octue.cloud.validation import raise_if_event_is_invalid
from octue.compatibility import warn_if_incompatible
from octue.utils.decoders import OctueJSONDecoder
from octue.utils.encoders import OctueJSONEncoder
Expand Down Expand Up @@ -546,8 +546,8 @@ def _parse_question(self, question):
if event.get("input_manifest"):
event_for_validation["input_manifest"] = json.loads(event["input_manifest"], cls=OctueJSONDecoder)

raise_if_message_is_invalid(
message=event_for_validation,
raise_if_event_is_invalid(
event=event_for_validation,
attributes=attributes,
receiving_service=self,
parent_sdk_version=attributes["octue_sdk_version"],
Expand Down
40 changes: 20 additions & 20 deletions octue/cloud/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
SERVICE_COMMUNICATION_SCHEMA_INFO_URL = "https://strands.octue.com/octue/service-communication"


def is_message_valid(message, attributes, receiving_service, parent_sdk_version, child_sdk_version, schema=None):
"""Check if the message or its attributes are valid according to the schema.
def is_event_valid(event, attributes, receiving_service, parent_sdk_version, child_sdk_version, schema=None):
"""Check if the event or its attributes are valid according to the schema.
:param dict message: the message to validate
:param dict attributes: the attributes of the message to validate
:param octue.cloud.pub_sub.service.Service receiving_service: the service that received the message and is validating it
:param dict event: the event to validate
:param dict attributes: the attributes of the event to validate
:param octue.cloud.pub_sub.service.Service receiving_service: the service that received the event and is validating it
:param str parent_sdk_version: the semantic version of Octue SDK running the parent
:param str child_sdk_version: the semantic version of Octue SDK running the child
:param dict|None schema: the schema to validate the message and its attributes against; if `None`, this defaults to the service communication schema used in this version of Octue SDK
:return bool: `True` if the message and its attributes are valid
:param dict|None schema: the schema to validate the event and its attributes against; if `None`, this defaults to the service communication schema used in this version of Octue SDK
:return bool: `True` if the event and its attributes are valid
"""
try:
raise_if_message_is_invalid(
message,
raise_if_event_is_invalid(
event,
attributes,
receiving_service,
parent_sdk_version,
Expand All @@ -37,38 +37,38 @@ def is_message_valid(message, attributes, receiving_service, parent_sdk_version,
return True


def raise_if_message_is_invalid(
message,
def raise_if_event_is_invalid(
event,
attributes,
receiving_service,
parent_sdk_version,
child_sdk_version,
schema=None,
):
"""Raise an error if the message or its attributes aren't valid according to the schema.
"""Raise an error if the event or its attributes aren't valid according to the schema.
:param dict message: the message to validate
:param dict attributes: the attributes of the message to validate
:param octue.cloud.pub_sub.service.Service receiving_service: the service that received the message and is validating it
:param dict event: the event to validate
:param dict attributes: the attributes of the event to validate
:param octue.cloud.pub_sub.service.Service receiving_service: the service that received the event and is validating it
:param str parent_sdk_version: the semantic version of Octue SDK running the parent
:param str child_sdk_version: the semantic version of Octue SDK running the child
:param dict|None schema: the schema to validate the message and its attributes against; if `None`, this defaults to the service communication schema used in this version of Octue SDK
:raise jsonschema.ValidationError: if the message or its attributes are invalid
:param dict|None schema: the schema to validate the event and its attributes against; if `None`, this defaults to the service communication schema used in this version of Octue SDK
:raise jsonschema.ValidationError: if the event or its attributes are invalid
:return None:
"""
if schema is None:
schema = {"$ref": SERVICE_COMMUNICATION_SCHEMA}

try:
jsonschema.validate({"event": message, "attributes": dict(attributes)}, schema)
jsonschema.validate({"event": event, "attributes": dict(attributes)}, schema)
except jsonschema.ValidationError as error:
warn_if_incompatible(parent_sdk_version=parent_sdk_version, child_sdk_version=child_sdk_version)

logger.exception(
"%r received a message that doesn't conform with the service communication schema (%s): %r.",
"%r received an event that doesn't conform with the service communication schema (%s): %r.",
receiving_service,
SERVICE_COMMUNICATION_SCHEMA_INFO_URL,
message,
event,
)

raise error

0 comments on commit 232bb35

Please sign in to comment.