From 4991b7d5c91c02996c2ad59bbaaf383db300d45a Mon Sep 17 00:00:00 2001 From: Harpo Date: Thu, 6 Jul 2023 08:37:24 -0700 Subject: [PATCH] Adds /run for single clams-app run (#11) ## Usage `curl -v http://localhost:8000/run?url=localhost:5000 -H 'Content-Type:application/json' -d@input.json` --- fastclam/app.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/fastclam/app.py b/fastclam/app.py index 31e3670..7037f01 100644 --- a/fastclam/app.py +++ b/fastclam/app.py @@ -1,22 +1,23 @@ +from json import loads +from json.decoder import JSONDecodeError +from xml.etree import ElementTree +from xml.etree.ElementTree import ParseError + +import requests +from clams.source import generate_source_mmif from fastapi import FastAPI, HTTPException from fastapi.responses import PlainTextResponse from starlette.exceptions import HTTPException as StarletteHTTPException -from clams.source import generate_source_mmif -from json import loads -import requests + from .log import log from .models import Inputs, Pipeline from .version import __VERSION__ -from xml.etree import ElementTree -from xml.etree.ElementTree import ParseError -from json.decoder import JSONDecodeError class MMIFException(HTTPException): pass -app = FastAPI() app = FastAPI(title='FastCLAM') @@ -41,6 +42,23 @@ def generate_source(files: Inputs) -> dict: return json_value +@app.post('/run') +def run_app(url: str, mmif: dict) -> dict: + """Run a single app with a single MMIF""" + log.info(f'Running {url}') + response = requests.post('http://' + url, json=mmif) + log.debug(f'received response {response.status_code}, {response.headers}') + if response.status_code != 200: + log.debug(response.content) + raise HTTPException( + status_code=response.status_code, + detail=response.content, + ) + results = response.json() + log.success(f'{url} Success!') + return results + + @app.post('/pipeline') def run_pipeline(pipeline: Pipeline) -> list | dict | str: """Run a list of media through a list of apps