백엔드 프레임 설계
This commit is contained in:
1 parent
d264104077
commit
f7833ba466
62 files changed
+1397
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from common.enums import ErrorType
|
||||
|
||||
|
||||
class StructModel:
|
||||
"""프로토콜/구조체 식별용 마커 클래스."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class ErrorInfo(BaseModel, StructModel):
|
||||
"""모든 응답에 공통으로 실리는 결과 정보. result.success / code / desc 로 내려간다."""
|
||||
|
||||
success: Optional[bool] = True
|
||||
code: Optional[int] = ErrorType.SUCCESS.value
|
||||
desc: Optional[str] = ErrorType.SUCCESS.name
|
||||
|
||||
def SetResult(self, enum: ErrorType):
|
||||
if enum is not None:
|
||||
self.success = ErrorType.SUCCESS.value == enum.value
|
||||
self.code = enum.value
|
||||
self.desc = enum.name
|
||||
|
||||
|
||||
# ---- Protocol 규약 -------------------------------------------------------
|
||||
# 모든 통신 패킷은 WebPacketProtocol 을 상속한다.
|
||||
# 요청 : Req_xxx (WebPacketProtocol)
|
||||
# 응답 : Res_xxx (Res_WebPacketProtocol) - 항상 result 필드를 가진다.
|
||||
# 각 라우터 폴더의 protocol.py 에 Req_/Res_ 를 정의한다.
|
||||
class WebPacketProtocol(BaseModel, StructModel):
|
||||
pass
|
||||
|
||||
|
||||
class Req_WebPacketProtocol(WebPacketProtocol):
|
||||
pass
|
||||
|
||||
|
||||
class Res_WebPacketProtocol(WebPacketProtocol):
|
||||
# default_factory 로 인스턴스마다 새 ErrorInfo 를 생성한다 (mutable default 공유 방지).
|
||||
result: ErrorInfo = Field(default_factory=ErrorInfo)
|
||||
msg: Optional[str] = None
|
||||
|
||||
|
||||
class UserInfo(StructModel):
|
||||
"""JWT subject 로 인코딩되는 유저 식별 정보."""
|
||||
|
||||
uid: int
|
||||
id: str
|
||||
nickname: str
|
||||
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
super().__init__()
|
||||
for dictionary in args:
|
||||
for key in dictionary:
|
||||
setattr(self, key, dictionary[key])
|
||||
for key in kwargs:
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
def to_json(self) -> str:
|
||||
return json.dumps(self.__dict__)
|
||||
Reference in new issue
Block a user