Home

/zotero_fastapi_proxy

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import requests

app = FastAPI()

# CORSMiddleware zur Anwendung hinzufügen
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],     
    allow_credentials=False,  # important if allow_origins=["*"]
    allow_methods=["*"],      
    allow_headers=["*"],      
)

BASE_URL = "http://localhost:23119"

@app.get("/")
def index():
    return "zotero proxy"


@app.get("/{path:path}")
def proxy(path: str, request: Request):
    print("path:",path)
    print("request.query_params:",request.query_params)
    
    url = f"{BASE_URL}/{path}"
    print("url:",url)

    r = requests.get(
        url,
        params=request.query_params
    )

    try:
        return r.json()
    except requests.exceptions.JSONDecodeError:
        return JSONResponse(
            status_code=r.status_code,
            content={
                "error": "Upstream returned non-JSON response",
                "text": r.text[:1000]
            }
        )