diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e4145d3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,27 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +# 2 space indentation for every file +[*] +indent_style = space +indent_size = 2 + +# 4 space indentation for python +[*.py] +indent_size = 4 + +# allow trailing whitespace in markdown files +[*.md] +trim_trailing_whitespace = false + +[Makefile] +indent_style = tab diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..57a4c47 --- /dev/null +++ b/.env.test @@ -0,0 +1,2 @@ +DATABASE_URI="postgresql://felix@localhost:5432/whisper_api_test" +ENVIRONMENT="development" diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..9218e20 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 88 +extend-ignore = E203 +exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..cb61cf3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +name: CI +on: push + +jobs: + fmt: + runs-on: ubuntu-latest + name: Fmt + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.11' + cache: 'pip' + - pip install -e .[dev] + - black --check app + - isort --check app + - mypy app + - flake8 app + lint: + runs-on: ubuntu-latest + name: Lint + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.11' + cache: 'pip' + - pip install -e .[dev] + - mypy app + - flake8 app + test: + runs-on: ubuntu-latest + name: Test + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.11' + cache: 'pip' + - pip install -e .[dev] + - pytest diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d1c340c --- /dev/null +++ b/.gitignore @@ -0,0 +1,163 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# VS Code +.vscode diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3c492e8 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ + +dev: + uvicorn app.main:app --reload + +fmt: + black app --check + isort app + +test: + ENVIRONMENT=test pytest + +lint: + mypy app + flake8 app + +create_account: + python -m scripts.create_account ${name} diff --git a/README.md b/README.md new file mode 100644 index 0000000..f819fd6 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# whisper-api + +### TODO + +- [ ] run alembic migrations before startup +- [ ] dockerize +- [ ] add celery queue diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..2874da8 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,104 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = app/alembic + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python-dateutil library that can be +# installed by adding `alembic[tz]` to the pip requirements +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to src/alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:src/alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +# sqlalchemy.url = driver://user:pass@localhost/dbname + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +hooks = black +black.type = console_scripts +black.entrypoint = black +black.options = -l 79 REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/alembic/README b/app/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/app/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/app/alembic/env.py b/app/alembic/env.py new file mode 100644 index 0000000..59c21c4 --- /dev/null +++ b/app/alembic/env.py @@ -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() diff --git a/app/alembic/script.py.mako b/app/alembic/script.py.mako new file mode 100644 index 0000000..55df286 --- /dev/null +++ b/app/alembic/script.py.mako @@ -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"} diff --git a/app/alembic/versions/54824f17a11d_add_account_table.py b/app/alembic/versions/54824f17a11d_add_account_table.py new file mode 100644 index 0000000..40a5c6c --- /dev/null +++ b/app/alembic/versions/54824f17a11d_add_account_table.py @@ -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 ### diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..82db499 --- /dev/null +++ b/app/config.py @@ -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() diff --git a/app/db/__init__.py b/app/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/db/base.py b/app/db/base.py new file mode 100644 index 0000000..5c6f3e9 --- /dev/null +++ b/app/db/base.py @@ -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() diff --git a/app/db/dtos.py b/app/db/dtos.py new file mode 100644 index 0000000..cb2fce3 --- /dev/null +++ b/app/db/dtos.py @@ -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 diff --git a/app/db/models.py b/app/db/models.py new file mode 100644 index 0000000..818e3dc --- /dev/null +++ b/app/db/models.py @@ -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) diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..0434d61 --- /dev/null +++ b/app/main.py @@ -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) diff --git a/app/security.py b/app/security.py new file mode 100644 index 0000000..bfe0ae4 --- /dev/null +++ b/app/security.py @@ -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 diff --git a/app/tests/__init__.py b/app/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/tests/conftest.py b/app/tests/conftest.py new file mode 100644 index 0000000..295d405 --- /dev/null +++ b/app/tests/conftest.py @@ -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 diff --git a/app/tests/test_auth.py b/app/tests/test_auth.py new file mode 100644 index 0000000..72cdff0 --- /dev/null +++ b/app/tests/test_auth.py @@ -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 diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..4249051 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,4 @@ +[mypy] +plugins = pydantic.mypy, sqlmypy, sqlalchemy.ext.mypy.plugin +ignore_missing_imports = True +disallow_untyped_defs = True diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..251bc16 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,33 @@ +[project] +name = "whisper-api" +description = "" +version = "0.0.1" + +dependencies=[ + "alembic ==1.9.0", + "fastapi ==0.88.0", + "psycopg2 ==2.9.5", + "python-dotenv ==0.21.0", + "sqlalchemy[mypy] == 1.4.45", + "uvicorn[standard] ==0.20.0" +] + +[project.optional-dependencies] +dev = [ + # code formatting + "black", + "isort", + + # linting + "flake8", + "mypy", + + # tests + "httpx", + "sqlalchemy-stubs", + "sqlalchemy-utils", + "pytest" +] + +[tool.isort] +profile = "black" diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripts/create_account.py b/scripts/create_account.py new file mode 100644 index 0000000..ec7d661 --- /dev/null +++ b/scripts/create_account.py @@ -0,0 +1,21 @@ +import argparse +from dotenv import load_dotenv +from app.db.base import get_db +from app.db.models import Account + +load_dotenv() + +def create_account(name: str) -> Account: + db = get_db().__next__() + account = Account(name=name) + db.add(account) + db.commit() + db.refresh(account) + return account + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("name", type=str, nargs=1) + args = parser.parse_args() + create_account(args.name[0])