Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 계좌 등록 API 추가 #85

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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