Skip to content

Commit c7775c7

Browse files
authored
Merge pull request #182 from grillazz/179-reflect-latest-and-greatest-fastapi-improvements
179 reflect latest and greatest fastapi improvements
2 parents 48cbb8c + 34ab82f commit c7775c7

File tree

10 files changed

+26
-12
lines changed

10 files changed

+26
-12
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PYTHONUNBUFFERED=1
55
POSTGRES_HOST=db
66
POSTGRES_PORT=5432
77
POSTGRES_DB=devdb
8-
POSTGRES_USER=user
8+
POSTGRES_USER=devdb
99
POSTGRES_PASSWORD=secret
1010

1111
# Redis

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ docker-create-db-migration: ## Create new alembic database migration aka databa
2727

2828
.PHONY: docker-test
2929
docker-test: ## Run project tests
30-
docker compose -f compose.yml -f test-compose.yml run --rm app pytest tests
30+
docker compose -f compose.yml -f test-compose.yml run --rm app pytest tests --durations=0
3131

3232
.PHONY: docker-test-snapshot
3333
docker-test-snapshot: ## Run project tests with inline snapshot

app/api/user.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from fastapi import APIRouter, Depends, status, Request, HTTPException
1+
from typing import Annotated
2+
3+
from fastapi import APIRouter, Depends, status, Request, HTTPException, Form
24
from sqlalchemy.ext.asyncio import AsyncSession
35

46
from app.database import get_db
@@ -29,7 +31,9 @@ async def create_user(
2931
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
3032
)
3133
async def get_token_for_user(
32-
user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db)
34+
user: Annotated[UserLogin, Form()],
35+
request: Request,
36+
db_session: AsyncSession = Depends(get_db),
3337
):
3438
_user: User = await User.find(db_session, [User.email == user.email])
3539

app/database.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
async def get_db() -> AsyncGenerator:
2828
async with AsyncSessionFactory() as session:
2929
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
30-
yield session
30+
try:
31+
yield session
32+
except Exception as e:
33+
logger.error(f"Error getting database session: {e}")
34+
raise

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def lifespan(_app: FastAPI):
6767
dependencies=[Depends(AuthBearer())],
6868
)
6969

70-
_scheduler_data_store = SQLAlchemyDataStore(engine)
70+
_scheduler_data_store = SQLAlchemyDataStore(engine, schema="scheduler")
7171
_scheduler_event_broker = RedisEventBroker(
7272
client_or_url=global_settings.redis_url.unicode_string()
7373
)

app/services/auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from fastapi import Request, HTTPException
88
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
9+
from app.utils.logging import AppLogger
10+
11+
logger = AppLogger().get_logger()
912

1013

1114
async def get_from_redis(request: Request, key: str):
@@ -37,6 +40,7 @@ async def __call__(self, request: Request):
3740
raise HTTPException(
3841
status_code=403, detail="Invalid token or expired token."
3942
)
43+
logger.info(f"Token verified: {credentials.credentials}")
4044
return credentials.credentials
4145

4246

compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- "8080:8080"
1919
depends_on:
2020
- db
21-
- redis
21+
- inmemory
2222

2323
db:
2424
container_name: fsap_db

db/create.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
-- DROP DATABASE IF EXISTS devdb;
2-
-- CREATE DATABASE devdb;
31
\connect devdb;
42
CREATE SCHEMA shakespeare;
53
CREATE SCHEMA happy_hog;
4+
CREATE SCHEMA scheduler;
65

76
DROP DATABASE IF EXISTS testdb;
87
CREATE DATABASE testdb;
98
\connect testdb;
109
CREATE SCHEMA shakespeare;
1110
CREATE SCHEMA happy_hog;
11+
CREATE SCHEMA scheduler;

test-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
services:
42
app:
53
environment:

tests/api/test_auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ async def test_add_user(client: AsyncClient):
3838
# TODO: parametrize test with diff urls including 404 and 401
3939
async def test_get_token(client: AsyncClient):
4040
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
41-
response = await client.post("/user/token", json=payload)
41+
response = await client.post(
42+
"/user/token",
43+
data=payload,
44+
headers={"Content-Type": "application/x-www-form-urlencoded"},
45+
)
4246
assert response.status_code == status.HTTP_201_CREATED
4347
claimset = jwt.decode(
4448
response.json()["access_token"], options={"verify_signature": False}

0 commit comments

Comments
 (0)