|  | 
| 18 | 18 | app.asgi_app = HmacVerifyMiddleware(app.asgi_app, bound.github_webhook_secret) | 
| 19 | 19 | 
 | 
| 20 | 20 | from .handlers import router as free_handler_router | 
|  | 21 | + | 
| 21 | 22 | handler_router = free_handler_router.bind(bound, app.logger) | 
| 22 | 23 | 
 | 
| 23 | 24 | # http client | 
| 24 | 25 | 
 | 
|  | 26 | + | 
| 25 | 27 | @app.before_serving | 
| 26 | 28 | async def setup_httpclient(): | 
| 27 |  | -  app.http_client = AsyncClient( | 
| 28 |  | -    headers={'User-Agent': 'pydisgit (kyori flavour)'} | 
| 29 |  | -  ) | 
|  | 29 | +  app.http_client = AsyncClient(headers={"User-Agent": "pydisgit (kyori flavour)"}) | 
|  | 30 | + | 
| 30 | 31 | 
 | 
| 31 | 32 | @app.after_serving | 
| 32 | 33 | async def teardown_httpclient(): | 
| 33 | 34 |   await app.http_client.aclose() | 
| 34 | 35 | 
 | 
| 35 |  | -@app.get('/') | 
|  | 36 | + | 
|  | 37 | +@app.get("/") | 
| 36 | 38 | async def hello() -> str: | 
| 37 | 39 |   """ | 
| 38 | 40 |   root handler | 
| 39 | 41 |   """ | 
| 40 | 42 |   return "begone foul beast", 400 | 
| 41 | 43 | 
 | 
| 42 | 44 | 
 | 
| 43 |  | -@app.post('/<hook_id>/<token>') | 
|  | 45 | +@app.post("/<hook_id>/<token>") | 
| 44 | 46 | async def gh_hook(hook_id: str, token: str) -> dict: | 
| 45 | 47 |   event = request.headers["X-GitHub-Event"] | 
| 46 | 48 |   if not event or not request.content_type: | 
| 47 | 49 |     raise BadRequest("No event or content type") | 
| 48 | 50 | 
 | 
| 49 |  | -  #if (!(await validateRequest(request, env.githubWebhookSecret))) { | 
|  | 51 | +  # if (!(await validateRequest(request, env.githubWebhookSecret))) { | 
| 50 | 52 |   #  return new Response('Invalid secret', { status: 403 }); | 
| 51 |  | -  #} | 
|  | 53 | +  # } | 
| 52 | 54 | 
 | 
| 53 | 55 |   if "application/json" in request.content_type: | 
| 54 | 56 |     json = await request.json | 
| 55 | 57 |   elif "application/x-www-form-urlencoded" in request.content_type: | 
| 56 |  | -    json = json.loads((await request.form)['payload']) | 
|  | 58 | +    json = json.loads((await request.form)["payload"]) | 
| 57 | 59 |   else: | 
| 58 | 60 |     raise BadRequest(f"Unknown content type {request.content_type}") | 
| 59 | 61 | 
 | 
| 60 | 62 |   embed = handler_router.process_request(event, json) | 
| 61 | 63 |   if not embed: | 
| 62 |  | -    return 'Webhook NO-OP', 200 | 
|  | 64 | +    return "Webhook NO-OP", 200 | 
| 63 | 65 | 
 | 
| 64 |  | -  if app.config['DEBUG']: | 
|  | 66 | +  if app.config["DEBUG"]: | 
| 65 | 67 |     pprint.pprint(embed) | 
| 66 | 68 |     pass | 
| 67 | 69 |     # embed = await bound.buildDebugPaste(embed) | 
| 68 | 70 | 
 | 
| 69 | 71 |   http: AsyncClient = app.http_client | 
| 70 | 72 | 
 | 
| 71 |  | -  result = await http.post(f"https://discord.com/api/webhooks/{hook_id}/{token}", json = embed) | 
|  | 73 | +  result = await http.post( | 
|  | 74 | +    f"https://discord.com/api/webhooks/{hook_id}/{token}", json=embed | 
|  | 75 | +  ) | 
| 72 | 76 | 
 | 
| 73 | 77 |   if result.status_code in (200, 204): | 
| 74 | 78 |     result_text = "".join([await a async for a in result.aiter_text()]) | 
| 75 |  | -    return {"message": f"We won! Webhook {hook_id} executed with token {token} :3, response: {result_text}"}, 200 | 
|  | 79 | +    return { | 
|  | 80 | +      "message": f"We won! Webhook {hook_id} executed with token {token} :3, response: {result_text}" | 
|  | 81 | +    }, 200 | 
| 76 | 82 |   else: | 
| 77 |  | -    return Response(response = await result.aread(), status = result.status_code, content_type=result.headers['content-type'], headers=result.headers) | 
|  | 83 | +    return Response( | 
|  | 84 | +      response=await result.aread(), | 
|  | 85 | +      status=result.status_code, | 
|  | 86 | +      content_type=result.headers["content-type"], | 
|  | 87 | +      headers=result.headers, | 
|  | 88 | +    ) | 
| 78 | 89 | 
 | 
| 79 | 90 | 
 | 
| 80 |  | -@app.get('/health') | 
|  | 91 | +@app.get("/health") | 
| 81 | 92 | async def health_check() -> str: | 
| 82 | 93 |   """ | 
| 83 | 94 |   simple aliveness check | 
|  | 
0 commit comments