Skip to content

Commit

Permalink
feat: add slowapi throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohulan committed Jul 5, 2024
1 parent 976583d commit 5ed1f7b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
25 changes: 23 additions & 2 deletions app/routers/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
from typing import Literal

import selfies as sf
from fastapi import FastAPI
from fastapi import APIRouter
from fastapi import HTTPException
from fastapi import Query
from fastapi import status
from fastapi import Request
from fastapi.responses import Response
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from rdkit import Chem
from STOUT import translate_forward
from STOUT import translate_reverse
Expand Down Expand Up @@ -39,6 +44,16 @@
from app.schemas.error import ErrorResponse
from app.schemas.error import NotFoundModel

# Create the Limiter instance
limiter = Limiter(key_func=get_remote_address)

# Initialize FastAPI app
app = FastAPI()

# Add the middleware to handle rate limit exceeded errors
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

router = APIRouter(
prefix="/convert",
tags=["convert"],
Expand Down Expand Up @@ -88,7 +103,9 @@ def get_health() -> HealthCheck:
422: {"description": "Unprocessable Entity", "model": ErrorResponse},
},
)
@limiter.limit("20/minute")
async def create2d_coordinates(
request: Request,
smiles: str = Query(
title="SMILES",
description="SMILES representation of the molecule",
Expand Down Expand Up @@ -157,7 +174,9 @@ async def create2d_coordinates(
422: {"description": "Unprocessable Entity", "model": ErrorResponse},
},
)
@limiter.limit("20/minute")
async def create3d_coordinates(
request: Request,
smiles: str = Query(
title="SMILES",
description="SMILES representation of the molecule",
Expand All @@ -173,7 +192,7 @@ async def create3d_coordinates(
},
),
toolkit: Literal["rdkit", "openbabel"] = Query(
default="rdkit",
default="openbabel",
description="Cheminformatics toolkit used in the backend",
),
):
Expand All @@ -184,7 +203,7 @@ async def create3d_coordinates(
Parameters:
- **SMILES**: required (str): The SMILES representation of the molecule.
- **toolkit**: optional (str): The molecule toolkit to use.
- Supported values: "rdkit" (default) & "openbabel".
- Supported values: "rdkit" & "openbabel" (default).
Returns:
- molblock (str): The generated mol block with 3D coordinates as a plain text response.
Expand Down Expand Up @@ -221,7 +240,9 @@ async def create3d_coordinates(
422: {"description": "Unprocessable Entity", "model": ErrorResponse},
},
)
@limiter.limit("10/minute")
async def iupac_name_or_selfies_to_smiles(
request: Request,
input_text: str = Query(
title="Input IUPAC name or SELFIES",
description="IUPAC name or SELFIES representation of the molecule",
Expand Down
14 changes: 14 additions & 0 deletions app/routers/depict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from typing import Literal
from typing import Optional

from fastapi import FastAPI
from fastapi import APIRouter
from fastapi import HTTPException
from fastapi import Query
from fastapi import Request
from fastapi import status
from fastapi.responses import Response
from fastapi.templating import Jinja2Templates
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

from app.modules.depiction import get_cdk_depiction
from app.modules.depiction import get_rdkit_depiction
Expand All @@ -24,6 +28,15 @@
from app.schemas.error import NotFoundModel

templates = Jinja2Templates(directory="app/templates")
# Create the Limiter instance
limiter = Limiter(key_func=get_remote_address)

# Initialize FastAPI app
app = FastAPI()

# Add the middleware to handle rate limit exceeded errors
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

router = APIRouter(
prefix="/depict",
Expand Down Expand Up @@ -193,6 +206,7 @@ async def depict_2d_molecule(
422: {"description": "Unprocessable Entity", "model": ErrorResponse},
},
)
@limiter.limit("25/minute")
async def depict_3d_molecule(
request: Request,
smiles: str = Query(
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ unicodedata2==15.0.0
uvicorn>=0.15.0,<0.16.0
websockets==10.4
mapchiral
slowapi
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ tensorflow==2.12.0
unicodedata2==15.0.0
websockets==10.4
mapchiral
slowapi

0 comments on commit 5ed1f7b

Please sign in to comment.