|
| 1 | +from fastapi import APIRouter, HTTPException, status, UploadFile |
| 2 | +from app.schemas import HealthCheck |
| 3 | +import subprocess |
| 4 | + |
| 5 | +router = APIRouter( |
| 6 | + prefix="/spectra", |
| 7 | + tags=["spectra"], |
| 8 | + dependencies=[], |
| 9 | + responses={404: {"description": "Not found"}}, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@router.get("/", include_in_schema=False) |
| 14 | +@router.get( |
| 15 | + "/health", |
| 16 | + tags=["healthcheck"], |
| 17 | + summary="Perform a Health Check on Chem Module", |
| 18 | + response_description="Return HTTP Status Code 200 (OK)", |
| 19 | + status_code=status.HTTP_200_OK, |
| 20 | + include_in_schema=False, |
| 21 | + response_model=HealthCheck, |
| 22 | +) |
| 23 | +def get_health() -> HealthCheck: |
| 24 | + """ |
| 25 | + ## Perform a Health Check |
| 26 | + Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker |
| 27 | + to ensure a robust container orchestration and management is in place. Other |
| 28 | + services which rely on proper functioning of the API service will not deploy if this |
| 29 | + endpoint returns any other HTTP status code except 200 (OK). |
| 30 | + Returns: |
| 31 | + HealthCheck: Returns a JSON response with the health status |
| 32 | + """ |
| 33 | + return HealthCheck(status="OK") |
| 34 | + |
| 35 | + |
| 36 | +@router.post( |
| 37 | + "/parse", |
| 38 | + tags=["spectra"], |
| 39 | + summary="Parse the input spectra format and extract metadata", |
| 40 | + response_description="", |
| 41 | + status_code=status.HTTP_200_OK, |
| 42 | +) |
| 43 | +async def parse_spectra(file: UploadFile): |
| 44 | + """ |
| 45 | + ## Parse the spectra file and extract meta-data |
| 46 | + Endpoint to uses nmr-load-save to read the input spectra file (.jdx,.nmredata,.dx) and extracts metadata |
| 47 | +
|
| 48 | + Returns: |
| 49 | + data: spectra data in json format |
| 50 | + """ |
| 51 | + try: |
| 52 | + contents = file.file.read() |
| 53 | + file_path = "/tmp/" + file.filename |
| 54 | + with open(file_path, "wb") as f: |
| 55 | + f.write(contents) |
| 56 | + p = subprocess.Popen( |
| 57 | + "npx nmr-cli -p " + file_path, stdout=subprocess.PIPE, shell=True |
| 58 | + ) |
| 59 | + (output, err) = p.communicate() |
| 60 | + p_status = p.wait() |
| 61 | + return output |
| 62 | + except Exception as e: |
| 63 | + raise HTTPException( |
| 64 | + status_code=422, |
| 65 | + detail="Error paring the structure " |
| 66 | + + e.message |
| 67 | + + ". Error: " |
| 68 | + + err |
| 69 | + + ". Status:" |
| 70 | + + p_status, |
| 71 | + headers={"X-Error": "RDKit molecule input parse error"}, |
| 72 | + ) |
| 73 | + finally: |
| 74 | + file.file.close() |
0 commit comments