alessandro trinca tornidor commited on
Commit
7496446
·
1 Parent(s): 8a3e357

feat: add pydantic validation within get_words_frequency()

Browse files
Files changed (1) hide show
  1. my_ghost_writer/app.py +4 -5
my_ghost_writer/app.py CHANGED
@@ -18,6 +18,7 @@ from my_ghost_writer import pymongo_operations_rw
18
  from my_ghost_writer.constants import (app_logger, ALLOWED_ORIGIN_LIST, API_MODE, DOMAIN, IS_TESTING, LOG_LEVEL, PORT,
19
  STATIC_FOLDER, WORDSAPI_KEY, WORDSAPI_URL, RAPIDAPI_HOST, MONGO_USE_OK, MONGO_HEALTHCHECK_SLEEP)
20
  from my_ghost_writer.pymongo_utils import mongodb_health_check
 
21
  from my_ghost_writer.type_hints import RequestTextFrequencyBody, RequestQueryThesaurusWordsapiBody
22
 
23
 
@@ -86,13 +87,11 @@ def health_mongo() -> str:
86
 
87
  @app.post("/words-frequency")
88
  def get_words_frequency(body: RequestTextFrequencyBody | str) -> JSONResponse:
89
- from my_ghost_writer.text_parsers import text_stemming
90
-
91
  t0 = datetime.now()
92
  app_logger.info(f"body type: {type(body)}.")
93
- app_logger.debug(f"body: {body}.")
94
- body = json.loads(body)
95
- text = body["text"]
96
  app_logger.info(f"LOG_LEVEL: '{LOG_LEVEL}', length of text: {len(text)}, type of 'text':'{type(text)}'.")
97
  if len(text) < 100:
98
  app_logger.debug(f"text from request: {text} ...")
 
18
  from my_ghost_writer.constants import (app_logger, ALLOWED_ORIGIN_LIST, API_MODE, DOMAIN, IS_TESTING, LOG_LEVEL, PORT,
19
  STATIC_FOLDER, WORDSAPI_KEY, WORDSAPI_URL, RAPIDAPI_HOST, MONGO_USE_OK, MONGO_HEALTHCHECK_SLEEP)
20
  from my_ghost_writer.pymongo_utils import mongodb_health_check
21
+ from my_ghost_writer.text_parsers import text_stemming
22
  from my_ghost_writer.type_hints import RequestTextFrequencyBody, RequestQueryThesaurusWordsapiBody
23
 
24
 
 
87
 
88
  @app.post("/words-frequency")
89
  def get_words_frequency(body: RequestTextFrequencyBody | str) -> JSONResponse:
 
 
90
  t0 = datetime.now()
91
  app_logger.info(f"body type: {type(body)}.")
92
+ app_logger.info(f"body: {body}.")
93
+ body_validated = RequestTextFrequencyBody.model_validate_json(body)
94
+ text = body_validated.text
95
  app_logger.info(f"LOG_LEVEL: '{LOG_LEVEL}', length of text: {len(text)}, type of 'text':'{type(text)}'.")
96
  if len(text) < 100:
97
  app_logger.debug(f"text from request: {text} ...")