From 29be0e89dd70ce5654f47670350b4e6a23d2b7ef Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 26 Sep 2023 16:58:13 -0400 Subject: [PATCH] chore: start using the FastAPI version and other cleanup (#256) * Set the current version in the FastAPI constructor and use `app.version` elsewhere * Update version-sync commands accordingly * Do not dump stack traces for unhandled errors * Add a startup log, other startup is silent without `uvicorn.error` --- CHANGELOG.md | 5 +++-- Makefile | 4 ++-- prepline_general/api/app.py | 24 ++++++------------------ prepline_general/api/general.py | 2 +- 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bdccea9..de40573e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -## 0.0.48-dev0 +## 0.0.48 -* **Adds `languages` kwarg** `ocr_languages` will eventually be depricated and replaced by `lanugages` to specify what languages to use for OCR +* **Adds `languages` kwarg** `ocr_languages` will eventually be deprecated and replaced by `lanugages` to specify what languages to use for OCR +* Adds a startup log and other minor cleanups ## 0.0.47 diff --git a/Makefile b/Makefile index a1b21468..9d0f3b6d 100644 --- a/Makefile +++ b/Makefile @@ -130,7 +130,7 @@ check-version: scripts/version-sync.sh -c \ -s CHANGELOG.md \ -f preprocessing-pipeline-family.yaml release \ - -f ${PACKAGE_NAME}/api/general.py release + -f ${PACKAGE_NAME}/api/app.py release ## version-sync: update references to version with most recent version from CHANGELOG.md .PHONY: version-sync @@ -138,4 +138,4 @@ version-sync: scripts/version-sync.sh \ -s CHANGELOG.md \ -f preprocessing-pipeline-family.yaml release \ - -f ${PACKAGE_NAME}/api/general.py release + -f ${PACKAGE_NAME}/api/app.py release diff --git a/prepline_general/api/app.py b/prepline_general/api/app.py index fab5381d..b03b3988 100644 --- a/prepline_general/api/app.py +++ b/prepline_general/api/app.py @@ -1,9 +1,7 @@ from fastapi import FastAPI, Request, status, HTTPException from fastapi.responses import JSONResponse -import json import logging import os -import traceback from .general import router as general_router @@ -13,7 +11,7 @@ app = FastAPI( title="Unstructured Pipeline API", description="""""", - version="1.0.0", + version="0.0.48", docs_url="/general/docs", openapi_url="/general/openapi.json", ) @@ -28,26 +26,13 @@ @app.exception_handler(HTTPException) async def http_error_handler(request: Request, e: HTTPException): logger.error(e.detail) - return JSONResponse(status_code=e.status_code, content={"detail": e.detail}) -# Note(austin) - Convert any other errors to HTTPException -# to be handled above, and log the stack trace +# Catch any other errors and return as 500 @app.exception_handler(Exception) async def error_handler(request: Request, e: Exception): - trace = traceback.format_exc() - - # Note(austin) - If ENV is set, dump the stack in json - # for nicer parsing. Soon we'll just have a json logger do this. - if os.environ.get("ENV") in ["dev", "prod"]: - trace = json.dumps(trace) - - logger.error(trace) - - error = HTTPException(status_code=500, detail=str(e)) - - return await http_error_handler(request, error) + return JSONResponse(status_code=500, content={"detail": str(e)}) allowed_origins = os.environ.get("ALLOWED_ORIGINS", None) @@ -83,3 +68,6 @@ def filter(self, record: logging.LogRecord) -> bool: @app.get("/healthcheck", status_code=status.HTTP_200_OK, include_in_schema=False) def healthcheck(request: Request): return {"healthcheck": "HEALTHCHECK STATUS: EVERYTHING OK!"} + + +logger.info("Started Unstructured API") diff --git a/prepline_general/api/general.py b/prepline_general/api/general.py index 75be5ca6..2c95e08b 100644 --- a/prepline_general/api/general.py +++ b/prepline_general/api/general.py @@ -569,7 +569,7 @@ def return_content_type(filename): @router.post("/general/v0/general") -@router.post("/general/v0.0.48/general") +@router.post(f"/general/v{app.version}/general") def pipeline_1( request: Request, gz_uncompressed_content_type: Optional[str] = Form(default=None),