m-api-server/framework/requests_dataclass.py

45 lines
1.6 KiB
Python
Raw Permalink Normal View History

2025-03-19 18:00:58 +08:00
from dataclasses import dataclass
import json as JSON
from flask import request
from framework.open_response import OpenABC
from framework.response_code import RequestMethods
@dataclass
class SampleRequests(OpenABC):
"""
基本数据请求体
login_username: str
login_password: str
self.login_username = self.check_string(self.json_data.get("login_username"))
self.login_password = self.check_string(self.json_data.get("login_token"))
"""
def __init__(self, methods: RequestMethods, json_data: bytes | str | dict):
"""
:param methods: 请求方式, 需要传入枚举类型的数据
:param json_data: 请求的数据, 如果是POST请求, 则为bytes|str类型
如果是GET请求, 则为dict类型
"""
super().__init__()
if methods != RequestMethods.POST and methods != RequestMethods.GET:
self.make_exception(self.response_code_enum.RESPONSE_405_METHODS_NOT_ALLOW)
if methods == RequestMethods.POST:
self.json_data: dict = JSON.loads(json_data)
elif methods == RequestMethods.GET:
self.json_data = json_data
def repr(self):
print(self.__repr__())
@staticmethod
def return_headers():
return request.headers
def return_token(self) -> str:
authorization = request.headers.get("Authorization")
if authorization and authorization.startswith("Bearer"):
return authorization.replace("Bearer ", "")
self.make_exception(self.response_code_enum.RESPONSE_401_TOKEN_INVALID)