Skip to content

Commit

Permalink
Fix some typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
sultaniman committed Mar 16, 2023
1 parent 432ddc5 commit f5496b9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 52 deletions.
4 changes: 2 additions & 2 deletions observer/api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Any, Optional
from typing import Any, Dict, Optional

from starlette import status

Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(
self.message = message or self.default_message
self.data = data or self.default_data

def to_dict(self) -> dict:
def to_dict(self) -> Dict[Any, Any]:
result = dict(
code=self.code.value,
status_code=self.status,
Expand Down
4 changes: 3 additions & 1 deletion observer/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from typing import Any, AsyncContextManager, Callable, Optional

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
Expand Down Expand Up @@ -82,7 +83,7 @@ def init_exception_handlers(env: Environment) -> Environment:
return env


def create_app(settings: Settings) -> FastAPI:
def create_app(settings: Settings, lifespan: Optional[Callable[[FastAPI], AsyncContextManager[Any]]]) -> FastAPI:
env: Environment = pipe(
[
init_integrations,
Expand All @@ -95,6 +96,7 @@ def create_app(settings: Settings) -> FastAPI:
app=FastAPI(
debug=settings.debug,
title=settings.title,
lifespan=lifespan,
description=settings.description,
version=settings.version,
docs_url=None,
Expand Down
9 changes: 8 additions & 1 deletion observer/cmd/cleanup.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# TODO: implement cleanup command
from typer import Typer

cleanup = Typer()


@cleanup.command()
def start():
pass
2 changes: 1 addition & 1 deletion observer/cmd/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def generate(
output_file: Path = Option(settings.swagger_output_file, help="Output file to save OpenAPI spec"),
):
"""Generate OpenAPI spec"""
app = create_app(settings)
app = create_app(settings, None)
if not output_file.parent.exists():
output_file.parent.mkdir(exist_ok=True)

Expand Down
77 changes: 38 additions & 39 deletions observer/context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from typing import Optional

from observer.db import Database
from observer.repositories.audit_logs import IAuditRepository
Expand Down Expand Up @@ -41,48 +40,48 @@

@dataclass
class Repositories:
audit: Optional[IAuditRepository] = None
users: Optional[IUsersRepository] = None
projects: Optional[IProjectsRepository] = None
offices: Optional[IOfficesRepository] = None
permissions: Optional[IPermissionsRepository] = None
world: Optional[IWorldRepository] = None
category: Optional[ICategoryRepository] = None
people: Optional[IPeopleRepository] = None
family: Optional[IFamilyRepository] = None
pets: Optional[IPetsRepository] = None
documents: Optional[IDocumentsRepository] = None
support: Optional[ISupportRecordsRepository] = None
migrations: Optional[IMigrationRepository] = None
audit: IAuditRepository = None
users: IUsersRepository = None
projects: IProjectsRepository = None
offices: IOfficesRepository = None
permissions: IPermissionsRepository = None
world: IWorldRepository = None
category: ICategoryRepository = None
people: IPeopleRepository = None
family: IFamilyRepository = None
pets: IPetsRepository = None
documents: IDocumentsRepository = None
support: ISupportRecordsRepository = None
migrations: IMigrationRepository = None


@dataclass
class Context:
db: Optional[Database] = None
uploads: Optional[UploadHandler] = None
downloads: Optional[DownloadHandler] = None
repos: Optional[Repositories] = None
storage: Optional[IStorage] = None
keychain: Optional[IKeychain] = None
mailer: Optional[IMailer] = None
audit_service: Optional[IAuditService] = None
jwt_service: Optional[JWTService] = None
auth_service: Optional[IAuthService] = None
crypto_service: Optional[ICryptoService] = None
mfa_service: Optional[IMFAService] = None
users_service: Optional[IUsersService] = None
offices_service: Optional[IOfficesService] = None
projects_service: Optional[IProjectsService] = None
permissions_service: Optional[IPermissionsService] = None
world_service: Optional[IWorldService] = None
category_service: Optional[ICategoryService] = None
people_service: Optional[IPeopleService] = None
family_service: Optional[IFamilyService] = None
pets_service: Optional[IPetsService] = None
documents_service: Optional[IDocumentsService] = None
support_service: Optional[ISupportRecordsService] = None
migrations_service: Optional[IMigrationService] = None
secrets_service: Optional[ISecretsService] = None
db: Database = None
uploads: UploadHandler = None
downloads: DownloadHandler = None
repos: Repositories = None
storage: IStorage = None
keychain: IKeychain = None
mailer: IMailer = None
audit_service: IAuditService = None
jwt_service: JWTService = None
auth_service: IAuthService = None
crypto_service: ICryptoService = None
mfa_service: IMFAService = None
users_service: IUsersService = None
offices_service: IOfficesService = None
projects_service: IProjectsService = None
permissions_service: IPermissionsService = None
world_service: IWorldService = None
category_service: ICategoryService = None
people_service: IPeopleService = None
family_service: IFamilyService = None
pets_service: IPetsService = None
documents_service: IDocumentsService = None
support_service: ISupportRecordsService = None
migrations_service: IMigrationService = None
secrets_service: ISecretsService = None


ctx = Context()
15 changes: 9 additions & 6 deletions observer/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import sys
from contextlib import asynccontextmanager

from fastapi import FastAPI

from observer.app import create_app
from observer.context import Repositories, ctx
Expand Down Expand Up @@ -41,11 +44,9 @@
from observer.services.world import WorldService
from observer.settings import db_settings, settings

app = create_app(settings)


@app.on_event("startup")
async def on_startup():
@asynccontextmanager
async def lifespan(_: FastAPI):
ctx.db = await connect(
db_settings.db_uri,
PoolOptions(
Expand Down Expand Up @@ -124,7 +125,9 @@ async def on_startup():
ctx.uploads = UploadHandler(ctx.storage, ctx.crypto_service)
ctx.downloads = DownloadHandler(ctx.storage, ctx.crypto_service)

yield

@app.on_event("shutdown")
async def on_shutdown():
await disconnect(ctx.db.engine)


app = create_app(settings, lifespan)
2 changes: 1 addition & 1 deletion observer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Settings(SettingsBase):
storage_kind: str = StorageKind.fs
storage_root: str = str(here / "uploads")
# Maximum upload file size is 5Mb
max_upload_size = 1024 * 1024 * 5
max_upload_size: int = 1024 * 1024 * 5
# Local storage uses the same path
documents_path: str = "documents"
# Block storage
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ async def ensure_db(env_settings, db_engine):

@pytest.fixture(scope="session")
def test_app(env_settings) -> FastAPI:
return create_app(env_settings)
return create_app(env_settings, None)


@pytest.fixture(scope="session")
Expand Down

0 comments on commit f5496b9

Please sign in to comment.