Skip to content

Commit

Permalink
✨ controllers layer에 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
mingi3314 committed Feb 18, 2024
1 parent b5122f1 commit 43039e4
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 7 deletions.
11 changes: 10 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ repos:
hooks:
- id: "mypy"
additional_dependencies:
[click, pytest, pydantic, types-requests, typer, types-PyYAML, types-toml]
[
click,
pytest,
pydantic,
types-requests,
typer,
types-PyYAML,
types-toml,
fastapi,
]
299 changes: 298 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typer = {extras = ["all"], version = "^0.9.0"}
toml = "^0.10.2"
pyyaml = "^6.0.1"
fastapi = "^0.109.2"
uvicorn = {extras = ["standard"], version = "^0.27.1"}

[tool.poetry.group.dev.dependencies]
ruff = "0.0.291"
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions pyrb/controllers/api/deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Annotated

from fastapi import Depends

from pyrb.controllers.constants import ACCOUNTS_CONFIG_PATH
from pyrb.repositories.account import AccountRepository, LocalConfigAccountRepository
from pyrb.services.account import AccountService


def account_repo_dep() -> AccountRepository:
return LocalConfigAccountRepository(config_path=ACCOUNTS_CONFIG_PATH)


AccountRepoDep = Annotated[AccountRepository, Depends(account_repo_dep)]


def account_service_dep(account_repo: AccountRepoDep) -> AccountService:
return AccountService(account_repo)


AccountServiceDep = Annotated[AccountService, Depends(account_service_dep)]
22 changes: 22 additions & 0 deletions pyrb/controllers/api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

from pyrb.controllers.api.deps import AccountServiceDep
from pyrb.exceptions import InitializationError
from pyrb.models.account import Account

app = FastAPI()


class AccountResponse(BaseModel):
account: Account


@app.get("/accounts/default", response_model=AccountResponse)
async def get_default_account(account_service: AccountServiceDep) -> AccountResponse:
try:
account = account_service.get()
except InitializationError:
raise HTTPException(status_code=404, detail="No accounts registered")

return AccountResponse(account=account)
7 changes: 2 additions & 5 deletions pyrb/controllers/cli/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import typer

from pyrb.controllers.constants import APP_DIR
from pyrb.controllers.constants import ACCOUNTS_CONFIG_PATH
from pyrb.enums import BrokerageType
from pyrb.models.account import AccountFactory
from pyrb.repositories.account import LocalConfigAccountRepository
Expand All @@ -25,11 +25,8 @@ def set(


def create_account_service() -> AccountService:
app_dir = APP_DIR
accounts_config_path = app_dir / "accounts"

account_service = AccountService(
account_repo=LocalConfigAccountRepository(accounts_config_path)
account_repo=LocalConfigAccountRepository(ACCOUNTS_CONFIG_PATH)
)

return account_service
1 change: 1 addition & 0 deletions pyrb/controllers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

APP_NAME = "pyrb" # TODO: parse from pyproject.toml and move to constants.py
APP_DIR = Path(typer.get_app_dir(APP_NAME))
ACCOUNTS_CONFIG_PATH = APP_DIR / "accounts"

0 comments on commit 43039e4

Please sign in to comment.