From b9799c73a00e79372af1a6d606f05afc108de554 Mon Sep 17 00:00:00 2001 From: Matthew Thornton <44626690+ThorntonMatthewD@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:24:57 -0500 Subject: [PATCH] Update dependencies and remove middleware workaround The middleware workaround was put in place due to an issue where calling await req.body() within middleware would cause the thread to become blocking. This would cause Starlette to throw and exception. This is no longer an issue and can safely be removed. --- pyproject.toml | 49 +++++++++++++++++-------------------------------- src/auth.py | 1 + src/database.py | 1 + src/server.py | 31 ++----------------------------- 4 files changed, 21 insertions(+), 61 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1bf760b..c51b76c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,37 +4,27 @@ description = "An automated Slack bot to syndicate local events into Slack chann readme = "README.md" requires-python = ">=3.11" license = { text = "MIT" } -version="0.1.0" -authors = [ - { name = "Olivia Sculley", email = "olivia@sculley.dev" }, -] -keywords = [ - "automation", - "black", - "bolt", - "bot", - "pylint", - "pytest", - "slack" -] +version = "0.1.0" +authors = [{ name = "Olivia Sculley", email = "olivia@sculley.dev" }] +keywords = ["automation", "black", "bolt", "bot", "pylint", "pytest", "slack"] dependencies = [ - "aiohttp==3.9.1", - "fastapi==0.103.2", + "aiohttp==3.9.3", + "fastapi==0.109.2", "python_dateutil==2.8.2", - "pytz==2023.3", - "slack_bolt==1.18.0", - "uvicorn==0.23.2" + "pytz==2024.1", + "slack_bolt==1.18.1", + "uvicorn==0.27.0.post1", ] [project.optional-dependencies] test = [ - "black==23.9.1", - "httpx==0.25.0", - "isort==5.12.0", - "pylint==2.17.5", - "pytest==7.4.2", - "pytest-asyncio==0.21.1", - "ssort==0.11.6" + "black==24.1.1", + "httpx==0.26.0", + "isort==5.13.2", + "pylint==3.0.3", + "pytest==7.4.4", + "pytest-asyncio==0.23.4", + "ssort==0.11.6", ] [project.urls] @@ -43,13 +33,8 @@ Documentation = "https://github.com/hackgvl/slack-events-bot/blob/dev/README.md" Repository = "https://github.com/hackgvl/slack-events-bot.git" [tool.pytest.ini_options] -pythonpath = [ - ".", - "src" -] -norecursedirs = [ - "tests/helpers" -] +pythonpath = [".", "src"] +norecursedirs = ["tests/helpers"] [tool.isort] profile = "black" diff --git a/src/auth.py b/src/auth.py index 55786f2..1815268 100644 --- a/src/auth.py +++ b/src/auth.py @@ -2,6 +2,7 @@ Logic for restricting the use of Slack commands to specific parties and validating incoming requests. """ + import hashlib import hmac import logging diff --git a/src/database.py b/src/database.py index 9815a49..5b28222 100644 --- a/src/database.py +++ b/src/database.py @@ -1,4 +1,5 @@ """Contains all the functions that interact with the sqlite database""" + import datetime import os import sqlite3 diff --git a/src/server.py b/src/server.py index 246b179..b90a423 100644 --- a/src/server.py +++ b/src/server.py @@ -3,6 +3,7 @@ Visit the /docs route for more information on the routes contained within. """ + import asyncio import datetime import logging @@ -16,7 +17,6 @@ import uvicorn from fastapi import HTTPException, Request from fastapi.responses import PlainTextResponse -from starlette.types import Message import database from auth import validate_slack_command_source @@ -84,39 +84,12 @@ async def update_check_api_cooldown(team_domain: str | None) -> None: await database.create_cooldown(team_domain, "check_api", 15) -async def set_body(req: Request, body: bytes): - """ - Overrides the Request class's __receive method as a workaround to an issue - where accessing a request body in middleware causes it to become blocking. - - See https://github.com/tiangolo/fastapi/discussions/8187 for the discussion - and this post (https://github.com/tiangolo/fastapi/discussions/8187#discussioncomment-5148049) - for where this code originates. Thanks, https://github.com/liukelin! - """ - - async def receive() -> Message: - return {"type": "http.request", "body": body} - - # pylint: disable=protected-access - req._receive = receive - - -async def get_body(req: Request) -> bytes: - """ - Leans into the overriden Request.__receive method seen above in set_body - to workaround 'await req.body()' causing the application to hang. - """ - body = await req.body() - await set_body(req, body) - return body - - @API.middleware("http") async def rate_limit_check_api( req: Request, call_next: Callable[[Request], Awaitable[None]] ): """Looks to see if /check_api has been run recently, and returns an error if so.""" - req_body = await get_body(req) + req_body = await req.body() if await check_api_being_requested(req.scope["path"], req_body): team_domain = await identify_slack_team_domain(req_body)