fix webhooks errors

This commit is contained in:
Mathieu Broillet 2025-08-28 11:11:30 +02:00
parent 59325d4f2e
commit cdb5b4fc55
Signed by: mathieub
GPG Key ID: 4428608CDA3A98D3
2 changed files with 35 additions and 4 deletions

View File

@ -206,6 +206,36 @@ async def fake_claim_exchange(request: Request, _=host_required([Hosts.PLEX])):
return await return_edited_response(upstream_response, data_override) return await return_edited_response(upstream_response, data_override)
@app.post("/api/v2/user/webhooks", tags=[Hosts.CLIENTS.value])
async def fake_get_user_webhooks(request: Request, _=host_required([Hosts.CLIENTS])):
# FIXME: this doesnt really work because webhooks are sent from Plex themselves so unless we implement a complete
# webhook emitter this is kinda useless but at least removes errors from UI...
upstream_response = await call_official(request, request.url.path.lstrip("/"))
# body = await request.body()
# raw_text = body.decode("utf-8")
# parsed_urls = parse_qs(raw_text)
# data_override = [
# {"url": url} for url in parsed_urls.get("urls[]", [])
# ]
data_override = [{"url": "https://webhooks.are.not.supported.because.plex.sends.them/not/your/server"}]
return await return_edited_response(upstream_response, data_override)
@app.get("/api/v2/user/webhooks", tags=[Hosts.CLIENTS.value])
async def fake_list_user_webhooks(request: Request, _=host_required([Hosts.CLIENTS])):
# See note in POST /api/v2/user/webhooks
upstream_response = await call_official(request, request.url.path.lstrip("/"))
data_override = []
return await return_edited_response(upstream_response, data_override)
@app.get("/favicon.ico") @app.get("/favicon.ico")
async def favicon(): async def favicon():
return FileResponse("favicon.ico") return FileResponse("favicon.ico")

View File

@ -18,6 +18,7 @@ official_client = httpx.AsyncClient(
def host_required(allowed_hosts: list[Hosts]): def host_required(allowed_hosts: list[Hosts]):
async def dependency(request: Request): async def dependency(request: Request):
host = request.url.hostname host = request.url.hostname
if host != "localhost" and host != "127.0.0.1":
if host not in [h.value for h in allowed_hosts]: if host not in [h.value for h in allowed_hosts]:
logger.warning(f"Host '{host}' not allowed for url '{request.url}'") logger.warning(f"Host '{host}' not allowed for url '{request.url}'")
raise HTTPException(status_code=403, detail=f"Host '{host}' not allowed") raise HTTPException(status_code=403, detail=f"Host '{host}' not allowed")
@ -64,7 +65,7 @@ async def call_official(request: Request, path: str) -> httpx.Response:
async def return_edited_response( async def return_edited_response(
response: httpx.Response, response: httpx.Response,
new_content: dict | bytes | None = None new_content: dict | bytes | list | str | None = None
) -> Response: ) -> Response:
""" """
Return possibly modified response. Return possibly modified response.