Skip to content

Commit 4f4880b

Browse files
committed
style: fixed naming conventions and renamed functions and classes
1 parent cc37dba commit 4f4880b

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.3.2"
1+
__version__ = "1.3.5"

api/errors.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
from fastapi.responses import JSONResponse
44
from pydantic import BaseModel
55

6+
__all__ = (
7+
"APIException",
8+
"CategoryNotFound",
9+
"BookNotFound",
10+
"RateLimited",
11+
"rate_limit_handler"
12+
)
13+
614
class APIException(Exception):
715
def __init__(self, msg) -> None:
816
self.msg = msg
@@ -39,7 +47,7 @@ class BookNotFound(BaseModel):
3947
}
4048
}
4149

42-
class RateLimitedClass(BaseModel):
50+
class RateLimited(BaseModel):
4351
error: str
4452
message: str
4553

@@ -54,9 +62,14 @@ class RateLimitedClass(BaseModel):
5462
}
5563
}
5664

57-
def RateLimited(request: Request, exc: RateLimitExceeded):
65+
66+
def rate_limit_handler(request: Request, exc: RateLimitExceeded):
5867
response = JSONResponse(
59-
{"error": f"RateLimited", "message": f"Rate Limit exceeded: {exc.detail}"}, status_code=429
68+
status_code = 429,
69+
content = {
70+
"error": "RateLimited",
71+
"message": f"Rate limit exceeded: {exc.detail} (Follow the rates: )" # TODO: Add link to git wiki page.
72+
}
6073
)
6174

6275
response = request.app.state.limiter._inject_headers(

api/main.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import annotations
21
from typing import TYPE_CHECKING, List # DON'T YOU DARE PUT THIS UNDER TYPE_CHECKING!!! I'm warning you!
32

43
if TYPE_CHECKING:
@@ -9,10 +8,10 @@
98
from . import errors, __version__
109
from .anime_girls import AGHPB, CategoryNotFound, Book, BookDict
1110

12-
from fastapi import FastAPI, Query, Request, Response
11+
from fastapi import FastAPI, Query, Request
1312
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
1413

15-
from slowapi import Limiter, _rate_limit_exceeded_handler
14+
from slowapi import Limiter
1615
from slowapi.util import get_remote_address
1716
from slowapi.errors import RateLimitExceeded
1817

@@ -52,7 +51,7 @@
5251
root_path = ROOT_PATH
5352
)
5453
app.state.limiter = limiter
55-
app.add_exception_handler(RateLimitExceeded, errors.RateLimited)
54+
app.add_exception_handler(RateLimitExceeded, errors.rate_limit_handler)
5655

5756
@app.get(
5857
"/",
@@ -84,13 +83,13 @@ async def root():
8483
"description": "The category was not Found."
8584
},
8685
429: {
87-
"model": errors.RateLimitedClass,
88-
"description": "Rate Limit exceeded"
86+
"model": errors.RateLimited,
87+
"description": "Rate limit exceeded!"
8988
}
9089
},
9190
)
9291
@limiter.limit("3/second")
93-
async def random(request: Request, category: str = None):
92+
async def random(request: Request, category: str = None) -> FileResponse:
9493
"""Returns a random book."""
9594
if category is None:
9695
category = aghpb.random_category()
@@ -169,13 +168,13 @@ async def search(
169168
"description": "The book was not Found."
170169
},
171170
429: {
172-
"model": errors.RateLimitedClass,
171+
"model": errors.RateLimited,
173172
"description": "Rate Limit exceeded"
174173
}
175174
},
176175
)
177176
@limiter.limit("3/second")
178-
async def get_id(request: Request, search_id: str):
177+
async def get_id(request: Request, search_id: str) -> FileResponse:
179178
"""Returns the book found."""
180179
for book in aghpb.books:
181180

0 commit comments

Comments
 (0)