|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | +""" |
| 3 | +@file |
| 4 | +@brief Simple class to store and retrieve files through an API. |
| 5 | +""" |
| 6 | +import os |
| 7 | +from typing import Optional |
| 8 | +from fastapi import FastAPI, Request, HTTPException |
| 9 | +from pydantic import BaseModel # pylint: disable=E0611 |
| 10 | +from .filestore_sqlite import SqlLite3FileStore |
| 11 | + |
| 12 | + |
| 13 | +class Item(BaseModel): |
| 14 | + name: Optional[str] # pylint: disable=E1136 |
| 15 | + format: Optional[str] # pylint: disable=E1136 |
| 16 | + team: Optional[str] # pylint: disable=E1136 |
| 17 | + project: Optional[str] # pylint: disable=E1136 |
| 18 | + version: Optional[str] # pylint: disable=E1136 |
| 19 | + content: Optional[str] # pylint: disable=E1136 |
| 20 | + password: str |
| 21 | + |
| 22 | + |
| 23 | +class Metric(BaseModel): |
| 24 | + name: str |
| 25 | + password: str |
| 26 | + |
| 27 | + |
| 28 | +class Query(BaseModel): |
| 29 | + name: Optional[str] # pylint: disable=E1136 |
| 30 | + team: Optional[str] # pylint: disable=E1136 |
| 31 | + project: Optional[str] # pylint: disable=E1136 |
| 32 | + version: Optional[str] # pylint: disable=E1136 |
| 33 | + password: str |
| 34 | + |
| 35 | + |
| 36 | +def create_fast_api_app(db_path, password): |
| 37 | + """ |
| 38 | + Creates a :epkg:`REST` application based on :epkg:`FastAPI`. |
| 39 | +
|
| 40 | + :return: app |
| 41 | + """ |
| 42 | + store = SqlLite3FileStore(db_path) |
| 43 | + |
| 44 | + async def get_root(): |
| 45 | + return {"pyquickhelper": "FastAPI to load and query files"} |
| 46 | + |
| 47 | + async def add(item: Item, request: Request): |
| 48 | + if item.password != password: |
| 49 | + raise HTTPException(status_code=401, detail="Wrong password") |
| 50 | + kwargs = dict(name=item.name, format=item.format, |
| 51 | + team=item.team, project=item.project, |
| 52 | + version=item.version, content=item.content) |
| 53 | + kwargs['metadata'] = dict(client=request.client) |
| 54 | + res = store.add(**kwargs) |
| 55 | + if 'content' in res: |
| 56 | + del res['content'] |
| 57 | + return res |
| 58 | + |
| 59 | + async def metrics(query: Metric, request: Request): |
| 60 | + if query.password != password: |
| 61 | + raise HTTPException(status_code=401, detail="Wrong password") |
| 62 | + res = list(store.enumerate_data(name=query.name, join=True)) |
| 63 | + return res |
| 64 | + |
| 65 | + async def query(query: Query, request: Request): |
| 66 | + if query.password != password: |
| 67 | + raise HTTPException(status_code=401, detail="Wrong password") |
| 68 | + res = list(store.enumerate(name=query.name, team=query.team, |
| 69 | + project=query.project, version=query.version)) |
| 70 | + return res |
| 71 | + |
| 72 | + app = FastAPI() |
| 73 | + app.get("/")(get_root) |
| 74 | + app.post("/add/")(add) |
| 75 | + app.post("/metrics/")(metrics) |
| 76 | + app.post("/query/")(query) |
| 77 | + return app |
| 78 | + |
| 79 | + |
| 80 | +def create_app(): |
| 81 | + """ |
| 82 | + Creates an instance of application class returned |
| 83 | + by @see fn create_fast_api_app. It checks that |
| 84 | + environment variables ``PYQUICKHELPER_FASTAPI_PWD`` |
| 85 | + and ``PYQUICKHELPER_FASTAPI_PATH`` are set up with |
| 86 | + a password and a filename. Otherwise, the function |
| 87 | + raised an exception. |
| 88 | +
|
| 89 | + Inspired from the guidelines |
| 90 | + `uvicorn/deployment <https://www.uvicorn.org/deployment/>`_, |
| 91 | + `(2) <https://www.uvicorn.org/deployment/#running-programmatically>`_. |
| 92 | + Some command lines: |
| 93 | +
|
| 94 | + :: |
| 95 | +
|
| 96 | + uvicorn --factory pyquickhelper.server.filestore_fastapi:create_app --port 8798 |
| 97 | + --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem |
| 98 | + gunicorn --keyfile=./key.pem --certfile=./cert.pem -k uvicorn.workers.UvicornWorker |
| 99 | + --factory pyquickhelper.server.filestore_fastapi:create_app |
| 100 | +
|
| 101 | + :: |
| 102 | +
|
| 103 | + uvicorn.run("pyquickhelper.server.filestore_fastapi:create_app", |
| 104 | + host="127.0.0.1", port=8798, log_level="info", factory=True) |
| 105 | + """ |
| 106 | + if "PYQUICKHELPER_FASTAPI_PWD" not in os.environ: |
| 107 | + raise RuntimeError( |
| 108 | + "Environment variable PYQUICKHELPER_FASTAPI_PWD is missing.") |
| 109 | + if "PYQUICKHELPER_FASTAPI_PATH" not in os.environ: |
| 110 | + raise RuntimeError( |
| 111 | + "Environment variable PYQUICKHELPER_FASTAPI_PATH is missing.") |
| 112 | + app = create_fast_api_app(os.environ['PYQUICKHELPER_FASTAPI_PATH'], |
| 113 | + os.environ['PYQUICKHELPER_FASTAPI_PWD']) |
| 114 | + return app |
0 commit comments