feat: initial project setup

This commit is contained in:
Felix Spöttel
2022-12-27 11:35:08 +01:00
parent 8be010f434
commit 4f7cd063f1
27 changed files with 801 additions and 0 deletions

0
app/__init__.py Normal file
View File

1
app/alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

80
app/alembic/env.py Normal file
View File

@@ -0,0 +1,80 @@
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from app.config import settings
from app.db.models import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
config.set_main_option("sqlalchemy.url", settings.DATABASE_URI)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,46 @@
"""add_account_table
Revision ID: 54824f17a11d
Revises:
Create Date: 2022-12-18 17:51:09.172531
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = "54824f17a11d"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"accounts",
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column(
"created_at",
sa.DateTime(),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.Column("api_key", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("name", sa.String(length=256), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_index(op.f("ix_accounts_api_key"), "accounts", ["api_key"], unique=False)
op.create_index(op.f("ix_accounts_id"), "accounts", ["id"], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_accounts_id"), table_name="accounts")
op.drop_index(op.f("ix_accounts_api_key"), table_name="accounts")
op.drop_table("accounts")
# ### end Alembic commands ###

18
app/config.py Normal file
View File

@@ -0,0 +1,18 @@
import os
from pydantic import BaseSettings
class Settings(BaseSettings):
DATABASE_URI: str
ENVIRONMENT: str
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
if "ENVIRONMENT" in os.environ and os.environ["ENVIRONMENT"] == "test":
settings = Settings(_env_file=".env.test") # type: ignore
else:
settings = Settings()

0
app/db/__init__.py Normal file
View File

18
app/db/base.py Normal file
View File

@@ -0,0 +1,18 @@
from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from app.config import settings
engine = create_engine(settings.DATABASE_URI)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db() -> Generator[Session, None, None]:
db = SessionLocal()
try:
yield db
finally:
db.close()

23
app/db/dtos.py Normal file
View File

@@ -0,0 +1,23 @@
from datetime import datetime
from typing import Optional
from uuid import UUID
from pydantic import BaseModel
class WithStandardFields(BaseModel):
id: UUID
created_at: datetime
updated_at: Optional[datetime]
class Config:
orm_mode = True
class AccountBase(BaseModel):
api_key: UUID
name: str
class Account(AccountBase, WithStandardFields):
pass

31
app/db/models.py Normal file
View File

@@ -0,0 +1,31 @@
from typing import Optional
import uuid
from sqlalchemy import Column, DateTime, String, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import declarative_mixin, declared_attr, Mapped
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
@declarative_mixin
class WithStandardFields:
@declared_attr
def created_at(cls) -> Mapped[DateTime]:
return Column(DateTime, server_default=func.now(), nullable=False)
@declared_attr
def updated_at(cls) -> Mapped[Optional[DateTime]]:
return Column(DateTime, onupdate=func.now())
@declared_attr
def id(cls) -> Mapped[UUID]:
return Column(UUID(as_uuid=True), primary_key=True, index=True, default=uuid.uuid4)
class Account(Base, WithStandardFields):
__tablename__ = "accounts"
api_key = Column(UUID(as_uuid=True), index=True, default=uuid.uuid4)
name = Column(String(length=256), unique=True)

37
app/main.py Normal file
View File

@@ -0,0 +1,37 @@
from typing import Dict, List
from fastapi import APIRouter, Depends, FastAPI
from .security import authenticate_api_key
app = FastAPI()
api_router = APIRouter(prefix="/api/v1", dependencies=[Depends(authenticate_api_key)])
@api_router.get("/")
def api_root() -> Dict:
return {}
@api_router.post("/transcripts")
def create_transcript() -> None:
return None
@api_router.get("/transcripts")
def get_transcripts() -> List:
return []
@api_router.get("/transcripts/{id}")
def get_transcript() -> None:
return None
@api_router.delete("/transcripts/{id}")
def delete_transcript() -> None:
return None
app.include_router(api_router)

26
app/security.py Normal file
View File

@@ -0,0 +1,26 @@
from uuid import UUID
from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.orm import Session
from sqlalchemy.orm.exc import NoResultFound
from .db.base import get_db
from .db.models import Account
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def authenticate_api_key(
db: Session = Depends(get_db),
api_key: str = Depends(oauth2_scheme),
) -> Account:
try:
account = db.query(Account).filter(Account.api_key == UUID(api_key)).one()
except NoResultFound:
raise HTTPException(status_code=401)
except Exception as e:
print(e)
raise HTTPException(status_code=422)
return account

0
app/tests/__init__.py Normal file
View File

42
app/tests/conftest.py Normal file
View File

@@ -0,0 +1,42 @@
from typing import Generator
import pytest
from sqlalchemy.orm import Session
from sqlalchemy_utils import create_database, database_exists, drop_database
from app.db.base import SessionLocal, engine, get_db
from app.db.models import Account, Base
from app.main import app
def pytest_configure() -> None:
if not database_exists(engine.url):
create_database(engine.url)
Base.metadata.create_all(engine)
def pytest_unconfigure() -> None:
if database_exists(engine.url):
drop_database(engine.url)
@pytest.fixture(name="db_session", scope="function", autouse=True)
def db_session() -> Generator[Session, None, None]:
connection = engine.connect()
transaction = connection.begin()
with SessionLocal(bind=connection) as session:
app.dependency_overrides[get_db] = lambda: session
yield session
app.dependency_overrides.clear()
transaction.rollback()
connection.close()
@pytest.fixture(scope="function")
def test_account(db_session: Session) -> Account:
account = Account(name="test_account")
db_session.add(account)
db_session.commit()
db_session.refresh(account)
return account

32
app/tests/test_auth.py Normal file
View File

@@ -0,0 +1,32 @@
from typing import Dict
from fastapi.testclient import TestClient
from app.db.models import Account
from app.main import app
client = TestClient(app)
def auth_header(s: str) -> Dict[str, str]:
return {"Authorization": f"Bearer {s}"}
def test_authorization_header_missing() -> None:
res = client.get("/api/v1")
assert res.status_code == 401
def test_authorization_header_malformed() -> None:
res = client.get("/api/v1", headers=auth_header("not_a_uuid"))
assert res.status_code == 422
def test_inexistent_api_key(test_account: Account) -> None:
res = client.get("/api/v1", headers=auth_header(str(test_account.id)))
assert res.status_code == 401
def test_existing_api_key(test_account: Account) -> None:
res = client.get("/api/v1", headers=auth_header(str(test_account.api_key)))
assert res.status_code == 200