mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
26 lines
772 B
Python
26 lines
772 B
Python
from fastapi import FastAPI, Depends, HTTPException, status
|
|
from fastapi.security import APIKeyHeader
|
|
|
|
API_KEY = "example-api-key"
|
|
API_KEY_NAME = "X-API-KEY"
|
|
|
|
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
async def validate_api_key(api_key_header: str = Depends(api_key_header)):
|
|
if api_key_header != API_KEY:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN, detail="Could not validate credentials"
|
|
)
|
|
return api_key_header
|
|
|
|
|
|
@app.get("/portfolio/{portfolio_id}", dependencies=[Depends(validate_api_key)])
|
|
async def get_portfolio(portfolio_id: int):
|
|
return {
|
|
"portfolio_id": portfolio_id,
|
|
"name": "My Portfolio",
|
|
"description": "This is my portfolio",
|
|
}
|