Skip to content

Commit

Permalink
✨ 계좌 등록 API 추가 (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
mingi3314 authored Feb 24, 2024
1 parent acc15b8 commit 7ed3ef9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pyrb/controllers/api/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from uuid import UUID

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from starlette.status import HTTP_201_CREATED

from pyrb.controllers.api.deps import AccountServiceDep
from pyrb.enums import BrokerageType
from pyrb.exceptions import InitializationError
from pyrb.models.account import Account
from pyrb.models.account import Account, AccountFactory

app = FastAPI()

Expand All @@ -12,6 +16,16 @@ class AccountResponse(BaseModel):
account: Account


class AccountCreateRequest(BaseModel):
brokerage: BrokerageType
app_key: str
secret_key: str


class AccountCreateResponse(BaseModel):
account_id: UUID


@app.get("/accounts/default", response_model=AccountResponse)
async def get_default_account(account_service: AccountServiceDep) -> AccountResponse:
try:
Expand All @@ -20,3 +34,15 @@ async def get_default_account(account_service: AccountServiceDep) -> AccountResp
raise HTTPException(status_code=404, detail="No accounts registered") from e

return AccountResponse(account=account)


@app.post("/accounts", response_model=AccountCreateResponse, status_code=HTTP_201_CREATED)
async def create_account(
account_service: AccountServiceDep, body: AccountCreateRequest
) -> AccountCreateResponse:
account = AccountFactory.create(
brokerage=body.brokerage, app_key=body.app_key, app_secret=body.secret_key
)
account_service.set(account)

return AccountCreateResponse(account_id=account.id)
2 changes: 2 additions & 0 deletions pyrb/models/account.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from abc import ABC
from typing import Annotated, Any

Expand All @@ -9,6 +10,7 @@

class Account(BaseModel, ABC):
brokerage: Annotated[BrokerageType, Field(...)]
id: uuid.UUID = Field(default_factory=uuid.uuid4)

def to_toml(self) -> str:
model_dict = self.model_dump()
Expand Down

0 comments on commit 7ed3ef9

Please sign in to comment.