Replaced custom log handler and async report generation logic with a simplified fake data streamer for the StreamingResponse. Added uvicorn server startup code for direct script execution.
55 lines
No EOL
1.7 KiB
Python
55 lines
No EOL
1.7 KiB
Python
import uvicorn
|
|
from fastapi import FastAPI, HTTPException, Request, Depends
|
|
from pydantic import BaseModel
|
|
from phoenix_technologies import ReportGenerator, CustomLogsHandler
|
|
from fastapi.responses import StreamingResponse
|
|
from typing import Dict, Any, AsyncGenerator, Coroutine, Generator
|
|
import os
|
|
import asyncio
|
|
import time
|
|
|
|
# FastAPI app instance
|
|
app = FastAPI()
|
|
|
|
# Define a request body structure using Pydantic
|
|
class ReportRequest(BaseModel):
|
|
query: str
|
|
report_type: str
|
|
|
|
# Shared log array using asyncio.Queue
|
|
log_queue = asyncio.Queue()
|
|
|
|
# Define a dependency to validate the API Key
|
|
def verify_api_key(request: Request):
|
|
# Define the API key from the environment variables
|
|
expected_api_key = os.getenv("API_KEY", None)
|
|
if not expected_api_key:
|
|
raise HTTPException(
|
|
status_code=500, detail="API key is not configured on the server."
|
|
)
|
|
|
|
# Get the API key from the request headers
|
|
provided_api_key = request.headers.get("X-API-KEY", None)
|
|
|
|
# Check if the API key is correct
|
|
if not provided_api_key or provided_api_key != expected_api_key:
|
|
raise HTTPException(status_code=403, detail="Invalid or missing API key.")
|
|
|
|
|
|
@app.post("/get_report", dependencies=[Depends(verify_api_key)])
|
|
async def get_report_endpoint(request: ReportRequest):
|
|
"""
|
|
Expose the `get_report` function as a POST API endpoint, with a streaming response.
|
|
"""
|
|
|
|
def fake_data_streamer():
|
|
for i in range(5):
|
|
yield f"My custom Log: {i}"
|
|
time.sleep(5)
|
|
|
|
# Return streaming response
|
|
return StreamingResponse(fake_data_streamer(), media_type="text/plain")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app='main:app', host="127.0.0.1", port=8000) |