33 lines
971 B
Markdown
33 lines
971 B
Markdown
|
|
# Add Endpoint
|
|||
|
|
|
|||
|
|
Füge einen neuen API-Endpoint zum Backend hinzu.
|
|||
|
|
|
|||
|
|
## Template:
|
|||
|
|
```python
|
|||
|
|
@app.get("/api/mein-endpoint")
|
|||
|
|
def mein_endpoint(
|
|||
|
|
limit: int = 100,
|
|||
|
|
session: dict = Depends(require_auth) # ← IMMER als separater Parameter!
|
|||
|
|
):
|
|||
|
|
pid = session['profile_id']
|
|||
|
|
with get_db() as conn:
|
|||
|
|
rows = conn.execute(
|
|||
|
|
"SELECT * FROM meine_tabelle WHERE profile_id=? LIMIT ?",
|
|||
|
|
(pid, limit)
|
|||
|
|
).fetchall()
|
|||
|
|
return [r2d(r) for r in rows]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## Regeln:
|
|||
|
|
- `session: dict = Depends(require_auth)` IMMER als letzter/separater Parameter
|
|||
|
|
- NIEMALS innerhalb von `Header(default=None, ...)` einbetten
|
|||
|
|
- Profile-ID immer aus `session['profile_id']` – nie aus Header
|
|||
|
|
- Admin-Endpoints: `session=Depends(require_admin)`
|
|||
|
|
- Rate-Limiting für sensitive Endpoints: `@limiter.limit("5/minute")`
|
|||
|
|
|
|||
|
|
## Nach dem Endpoint:
|
|||
|
|
api.js um neue Methode ergänzen:
|
|||
|
|
```javascript
|
|||
|
|
meinEndpoint: (params) => req(`/mein-endpoint?limit=${params}`),
|
|||
|
|
```
|