From 5229b1acdec540c4e459a156be31e53cf38c0ca3 Mon Sep 17 00:00:00 2001 From: ashkankzme Date: Wed, 14 Aug 2024 15:18:33 -0700 Subject: [PATCH] WIP: implementing a sample input parsing and verification implementation for classycat --- lib/model/classycat.py | 31 ++++++++++++++++++++++++---- lib/model/classycat_schema_create.py | 28 +++++++++++++++++++++---- lib/model/classycat_schema_lookup.py | 15 +++++++++++--- lib/schemas.py | 2 +- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/lib/model/classycat.py b/lib/model/classycat.py index 72e0ae9..a339ea8 100644 --- a/lib/model/classycat.py +++ b/lib/model/classycat.py @@ -1,7 +1,7 @@ from typing import Union, Dict, Any from lib.logger import logger from lib.model.model import Model -from lib.schemas import Message, ClassyCatSchemaResponse, ClassyCatBatchClassificationResponse +from lib.schemas import Message, ClassyCatSchemaResponse, ClassyCatBatchClassificationResponse, ClassyCatResponse from lib.model.classycat_classify import Model as ClassifyModel from lib.model.classycat_schema_create import Model as ClassyCatSchemaCreateModel from lib.model.classycat_schema_lookup import Model as ClassyCatSchemaLookupModel @@ -29,11 +29,34 @@ def validate_input(cls, data: Dict) -> None: """ Validate input data. Must be implemented by all child "Model" classes. """ - pass + event_type = data['parameters']['event_type'] + + if event_type == 'classify': + ClassifyModel.validate_input(data) + elif event_type == 'schema_lookup': + ClassyCatSchemaLookupModel.validate_input(data) + elif event_type == 'schema_create': + ClassyCatSchemaCreateModel.validate_input(data) + else: + logger.error(f"Unknown event type {event_type}") + raise PrestoBaseException(f"Unknown event type {event_type}", 422) @classmethod def parse_input_message(cls, data: Dict) -> Any: """ - Validate input data. Must be implemented by all child "Model" classes. + Parse input into appropriate response instances. """ - return None + event_type = data['parameters']['event_type'] + + if event_type == 'classify': + result_instance_class = ClassifyModel + elif event_type == 'schema_lookup': + result_instance_class = ClassyCatSchemaLookupModel + elif event_type == 'schema_create': + result_instance_class = ClassyCatSchemaCreateModel + + else: + logger.error(f"Unknown event type {event_type}") + raise PrestoBaseException(f"Unknown event type {event_type}", 422) + + return result_instance_class.parse_input_message(data) diff --git a/lib/model/classycat_schema_create.py b/lib/model/classycat_schema_create.py index 2ef8986..d8dc009 100644 --- a/lib/model/classycat_schema_create.py +++ b/lib/model/classycat_schema_create.py @@ -211,7 +211,8 @@ def process(self, message: Message) -> ClassyCatSchemaResponse: raise PrestoBaseException(f"Error creating schema: {e}", 500) from e - def verify_schema_parameters(self, schema_name, topics, examples, languages): #todo + @classmethod + def verify_schema_parameters(schema_name, topics, examples, languages): if not schema_name or not isinstance(schema_name, str) or len(schema_name) == 0: raise ValueError("schema_name is invalid. It must be a non-empty string") @@ -251,11 +252,30 @@ def validate_input(cls, data: Dict) -> None: """ Validate input data. Must be implemented by all child "Model" classes. """ - pass + schema_specs = data['parameters'] + + schema_name = schema_specs["schema_name"] + topics = schema_specs["topics"] + examples = schema_specs["examples"] + languages = schema_specs["languages"] # ['English', 'Spanish'] + + try: + cls.verify_schema_parameters(schema_name, topics, examples, languages) + except Exception as e: + logger.exception(f"Error verifying schema parameters: {e}") + raise PrestoBaseException(f"Error verifying schema parameters: {e}", 422) from e @classmethod def parse_input_message(cls, data: Dict) -> Any: """ - Validate input data. Must be implemented by all child "Model" classes. + Parse input into appropriate response instances. """ - return None + event_type = data['parameters']['event_type'] + result_data = data.get('result', {}) + + if event_type == 'schema_create': + result_instance = ClassyCatSchemaResponse(**result_data) + else: + raise PrestoBaseException(f"Unknown event type {event_type}", 422) + + return result_instance diff --git a/lib/model/classycat_schema_lookup.py b/lib/model/classycat_schema_lookup.py index 480bc63..b7d8cc9 100644 --- a/lib/model/classycat_schema_lookup.py +++ b/lib/model/classycat_schema_lookup.py @@ -83,11 +83,20 @@ def validate_input(cls, data: Dict) -> None: """ Validate input data. Must be implemented by all child "Model" classes. """ - pass + if "schema_name" not in data["parameters"] or data["parameters"]["schema_name"] == "": + raise PrestoBaseException("schema_name is required as input to schema look up", 422) @classmethod def parse_input_message(cls, data: Dict) -> Any: """ - Validate input data. Must be implemented by all child "Model" classes. + Parse input into appropriate response instances. """ - return None + event_type = data['parameters']['event_type'] + result_data = data.get('result', {}) + + if event_type == 'schema_lookup': + result_instance = ClassyCatSchemaResponse(**result_data) + else: + raise PrestoBaseException(f"Unknown event type {event_type}", 422) + + return result_instance diff --git a/lib/schemas.py b/lib/schemas.py index 05a435c..3ede687 100644 --- a/lib/schemas.py +++ b/lib/schemas.py @@ -49,7 +49,7 @@ def parse_input_message(message_data: Dict) -> Message: result_data = body_data.get('result', {}) modelClass = get_class('lib.model.', os.environ.get('MODEL_NAME')) - modelClass.validate_input(result_data) # will raise exceptions in case of validation errors + modelClass.validate_input(body_data) # will raise exceptions in case of validation errors # parse_input_message will enable us to have more complicated result types without having to change the schema file result_instance = modelClass.parse_input_message(result_data) # assumes input is valid