Spaces:
Build error
Build error
fix middleware
Browse files- gradio_webhooks.py +9 -4
gradio_webhooks.py
CHANGED
|
@@ -3,7 +3,8 @@ from pathlib import Path
|
|
| 3 |
from typing import Literal, Optional, Set, Union
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
-
from fastapi import
|
|
|
|
| 7 |
from pydantic import BaseModel
|
| 8 |
|
| 9 |
|
|
@@ -68,7 +69,7 @@ class GradioWebhookApp:
|
|
| 68 |
def add_webhook(self, path: str):
|
| 69 |
"""Decorator to add a webhook to the server app."""
|
| 70 |
self.webhook_paths.add(path)
|
| 71 |
-
return self.fastapi_app.
|
| 72 |
|
| 73 |
def ready(self) -> None:
|
| 74 |
"""Set the app as "ready" and block main thread to keep it running."""
|
|
@@ -88,9 +89,13 @@ class GradioWebhookApp:
|
|
| 88 |
if self._webhook_secret is not None:
|
| 89 |
request_secret = request.headers.get("x-webhook-secret")
|
| 90 |
if request_secret is None:
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
if request_secret != self._webhook_secret:
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
return await call_next(request)
|
| 95 |
|
| 96 |
|
|
|
|
| 3 |
from typing import Literal, Optional, Set, Union
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
+
from fastapi import Request
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
|
| 10 |
|
|
|
|
| 69 |
def add_webhook(self, path: str):
|
| 70 |
"""Decorator to add a webhook to the server app."""
|
| 71 |
self.webhook_paths.add(path)
|
| 72 |
+
return self.fastapi_app.post(path)
|
| 73 |
|
| 74 |
def ready(self) -> None:
|
| 75 |
"""Set the app as "ready" and block main thread to keep it running."""
|
|
|
|
| 89 |
if self._webhook_secret is not None:
|
| 90 |
request_secret = request.headers.get("x-webhook-secret")
|
| 91 |
if request_secret is None:
|
| 92 |
+
return JSONResponse(
|
| 93 |
+
{"error": "x-webhook-secret header not set."}, status_code=401
|
| 94 |
+
)
|
| 95 |
if request_secret != self._webhook_secret:
|
| 96 |
+
return JSONResponse(
|
| 97 |
+
{"error": "Invalid webhook secret."}, status_code=403
|
| 98 |
+
)
|
| 99 |
return await call_next(request)
|
| 100 |
|
| 101 |
|