Skip to content

Commit

Permalink
Update dependencies and remove middleware workaround
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ThorntonMatthewD committed Feb 5, 2024
1 parent caf1f90 commit b9799c7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 61 deletions.
49 changes: 17 additions & 32 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" },
]
keywords = [
"automation",
"black",
"bolt",
"bot",
"pylint",
"pytest",
"slack"
]
version = "0.1.0"
authors = [{ name = "Olivia Sculley", email = "[email protected]" }]
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]
Expand All @@ -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"
1 change: 1 addition & 0 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Contains all the functions that interact with the sqlite database"""

import datetime
import os
import sqlite3
Expand Down
31 changes: 2 additions & 29 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Visit the /docs route for more information on the routes contained within.
"""

import asyncio
import datetime
import logging
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b9799c7

Please sign in to comment.