Skip to content

Commit

Permalink
Update fastapi code
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekrana committed Aug 18, 2023
1 parent 9d1c011 commit 6bf023d
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [ ] [Helm](https://helm.sh)
- [ ] [Tilt](https://tilt.dev)
- [x] Postgresql
- [ ] MongoDB
- [ ] Redis
- [ ] Rabbitmq
- [ ] Github Actions
Expand Down
4 changes: 2 additions & 2 deletions k8s/deployments/local/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ vars:
REDIS_VERSION: 17.15.4 # app version: 7.2.0

tasks:
k3d-create:
create-k3d:
desc: Create k3d cluster
cmds:
- k3d cluster create tech-stack --config k3d-cluster.yml

k3d-delete:
delete-k3d:
desc: Delete k3d cluster
cmds:
- k3d cluster delete tech-stack
Expand Down
2 changes: 1 addition & 1 deletion k8s/deployments/local/values.postgresql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ global:
postgresPassword: 'adminAdmin123!'
username: 'admin'
password: 'adminAdmin123!'
database: 'tech_stack'
database: 'tech_stack_db'
4 changes: 2 additions & 2 deletions migrations/00001.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE
IF NOT EXISTS users(
-- Active: 1692299907002@@127.0.0.1@5432@tech_stack_db
CREATE TABLE IF NOT EXISTS users(
id UUID PRIMARY KEY,
name VARCHAR(32) NOT NULL,
fullname VARCHAR(64) DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion service-1/src/app_1/internal/configs/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class PostgresqlConfig(BaseSettings):
host: str = Field("127.0.0.1", alias="POSTGRESQL_HOST")
port: str = Field("5432", alias="POSTGRESQL_PORT")
database: str = Field("tech_stack", alias="POSTGRESQL_DATABASE")
database: str = Field("tech_stack_db", alias="POSTGRESQL_DATABASE")
user: str = Field("admin", alias="POSTGRESQL_USER")
password: str = Field("adminAdmin123!", alias="POSTGRESQL_PASSWORD") # TODO: use SecretStr
echo: bool = Field(True, alias="POSTGRESQL_ECHO") # TODO: str not casted to bool
Expand Down
15 changes: 4 additions & 11 deletions service-1/src/app_1/internal/handlers/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Any

from fastapi import Body
from fastapi import status
from fastapi.param_functions import Depends
from fastapi.routing import APIRouter
from sqlalchemy.orm.session import Session
Expand All @@ -12,18 +10,12 @@

router: APIRouter = APIRouter()

example: list[dict[str, Any]] = [
{
"name": "mock-name",
"fullname": "mock-fullname",
}
]


@router.get(
"/v1/users/",
summary="Find users.",
response_model=list[UserFindResponse],
status_code=status.HTTP_200_OK,
)
def find(
session: Session = Depends(get_session),
Expand All @@ -38,9 +30,10 @@ def find(
"/v1/users/",
summary="Create users.",
response_model=list[UserCreateResponse],
status_code=status.HTTP_201_CREATED,
)
def create(
body: list[UserCreateRequest] = Body(example=example),
body: list[UserCreateRequest],
session: Session = Depends(get_session),
) -> list[UserCreateResponse]:
"""Create users."""
Expand Down
4 changes: 2 additions & 2 deletions service-1/src/app_1/internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

logging.basicConfig(level=logging.DEBUG)

app: FastAPI = FastAPI()
app: FastAPI = FastAPI(title="Tech Stack", description="Technology stack")

app.include_router(health.router, tags=["Health"])
app.include_router(users.router, tags=["Users"])
Expand All @@ -17,7 +17,7 @@
config: App1Config = load_app_1_config()
print(f"{config=}")

uvicorn.run(
uvicorn.run( # pyright: ignore
"main:app",
host=config.host,
port=config.port,
Expand Down
24 changes: 21 additions & 3 deletions service-1/src/app_1/internal/models/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from uuid import UUID

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from sqlalchemy import func
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column

Expand All @@ -14,8 +14,8 @@ class Base(MappedAsDataclass, DeclarativeBase):
class UserDB(Base):
__tablename__ = "users"
id: Mapped[UUID] = mapped_column(primary_key=True, init=False)
name: Mapped[str]
fullname: Mapped[str | None]
name: Mapped[str] = mapped_column(nullable=False)
fullname: Mapped[str | None] = mapped_column(nullable=True)
created_at: Mapped[datetime] = mapped_column(
init=False, server_default=func.now(), default=func.now()
) # TODO: server_default not working
Expand All @@ -30,6 +30,16 @@ class UserCreateRequest(BaseModel):
name: str
fullname: str

model_config = ConfigDict(
from_attributes=True,
json_schema_extra={
"example": {
"name": "mock-name",
"fullname": "mock-fullname",
}
},
)


class UserCreateResponse(BaseModel):
id: UUID
Expand All @@ -38,10 +48,18 @@ class UserCreateResponse(BaseModel):
created_at: datetime
updated_at: datetime

model_config = ConfigDict(
from_attributes=True,
)


class UserFindResponse(BaseModel):
id: UUID
name: str
fullname: str
created_at: datetime
updated_at: datetime

model_config = ConfigDict(
from_attributes=True,
)
8 changes: 4 additions & 4 deletions service-1/src/app_1/internal/repos/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_engine(
config: PostgresqlConfig,
) -> Engine:
scheme: str = "postgresql+pg8000"
password_encoded: str = urllib.parse.quote_plus(config.password) # pyright: ignore reportPrivateUsage
password_encoded: str = urllib.parse.quote_plus(config.password) # pyright: ignore
url: str = f"{scheme}://{config.user}:{password_encoded}@{config.host}:{config.port}/{config.database}"
logging.info(f"Connecting to {scheme}://{config.user}:*****@{config.host}:{config.port}/{config.database}")
return create_engine(url, echo=config.echo)
Expand All @@ -24,11 +24,11 @@ def get_engine(


def get_session() -> Generator[Session, None, None]:
session: Session = session_factory()
session: Session = session_factory() # pyright: ignore
try:
yield session
except:
session.rollback()
session.rollback() # pyright: ignore
raise
finally:
session.close()
session.close() # pyright: ignore
16 changes: 9 additions & 7 deletions service-1/src/app_1/internal/repos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

from fastapi import status
from sqlalchemy import select
from sqlalchemy.engine.result import ScalarResult
from sqlalchemy.exc import DBAPIError
from sqlalchemy.orm.session import Session
from sqlalchemy.sql.selectable import Select

from app_1.internal.helpers.app_errors import AppError, AppErrorType
from app_1.internal.helpers.utils import get_uuid
Expand All @@ -17,9 +19,9 @@ def __init__(self: Self, session: Session) -> None:

def find(self: Self) -> list[UserDB]:
try:
stmt = select(UserDB).where(UserDB.deleted_at.is_(None))
items: list[UserDB] = self.session.scalars(stmt) # type: ignore
return items
statement: Select[tuple[UserDB]] = select(UserDB).where(UserDB.deleted_at.is_(None))
items: ScalarResult[UserDB] = self.session.execute(statement).scalars()
return list(items)

except Exception as e:
logging.error(f"{e=!r}")
Expand All @@ -28,7 +30,7 @@ def find(self: Self) -> list[UserDB]:
def create(self: Self, items: list[UserDB]) -> list[UserDB]:
try:
for item in items:
if item.id is None:
if item.id is None: # pyright: ignore
item.id = get_uuid()
self.session.add(item)
self.session.commit()
Expand All @@ -44,8 +46,8 @@ def create(self: Self, items: list[UserDB]) -> list[UserDB]:
logging.error(f"{e=!r}")
raise e

except Exception as exc:
logging.error(f"Exception: {exc}")
raise exc
except Exception as e:
logging.error(f"{e=!r}")
raise e

return items
5 changes: 2 additions & 3 deletions service-1/src/app_1/internal/users/service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from dataclasses import asdict
from typing import Self

from app_1.internal.models.users import UserCreateRequest, UserCreateResponse, UserDB, UserFindResponse
Expand All @@ -11,7 +10,7 @@ def __init__(self: Self, repo: UserRepo) -> None:

def find(self: Self) -> list[UserFindResponse]:
user_db: list[UserDB] = self.repo.find()
user: list[UserFindResponse] = [UserFindResponse.model_validate(asdict(x)) for x in user_db]
user: list[UserFindResponse] = [UserFindResponse.model_validate(x) for x in user_db]
return user

def create(self: Self, body: list[UserCreateRequest]) -> list[UserCreateResponse]:
Expand All @@ -24,5 +23,5 @@ def create(self: Self, body: list[UserCreateRequest]) -> list[UserCreateResponse
)
)
user_db: list[UserDB] = self.repo.create(items)
user: list[UserCreateResponse] = [UserCreateResponse.model_validate(asdict(x)) for x in user_db]
user: list[UserCreateResponse] = [UserCreateResponse.model_validate(x) for x in user_db]
return user
19 changes: 19 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

# Install dev dependencies
task install:helm
task install:k3d
task install:k9s
task install:kubectl
task install:tilt

# Create k8s cluster
cd k8s/deployments/local
task delete-k3d
task create-k3d
task install-postgresql

# Run migrations
# Port forward postgresql

0 comments on commit 6bf023d

Please sign in to comment.