Files
2025-12-25 18:04:10 +08:00

119 lines
3.9 KiB
Python

"""
数据库模型定义
包含请求历史、收藏夹、环境变量等模型
@author huazm
"""
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class RequestHistory(db.Model):
"""请求历史记录模型"""
__tablename__ = 'request_history'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
method = db.Column(db.String(10), nullable=False)
url = db.Column(db.Text, nullable=False)
headers = db.Column(db.Text) # JSON 格式
params = db.Column(db.Text) # JSON 格式
body = db.Column(db.Text)
body_type = db.Column(db.String(20)) # json, form, xml, raw
auth_type = db.Column(db.String(20)) # bearer, basic, apikey, oauth2
auth_config = db.Column(db.Text) # JSON 格式
# 响应信息
response_body = db.Column(db.Text)
response_headers = db.Column(db.Text) # JSON 格式
status_code = db.Column(db.Integer)
duration = db.Column(db.Float) # 请求耗时(毫秒)
# 元信息
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def to_dict(self):
"""转换为字典"""
return {
'id': self.id,
'method': self.method,
'url': self.url,
'headers': self.headers,
'params': self.params,
'body': self.body,
'bodyType': self.body_type,
'authType': self.auth_type,
'authConfig': self.auth_config,
'responseBody': self.response_body,
'responseHeaders': self.response_headers,
'statusCode': self.status_code,
'duration': self.duration,
'createdAt': self.created_at.isoformat() if self.created_at else None
}
class Collection(db.Model):
"""收藏夹模型"""
__tablename__ = 'collections'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
folder = db.Column(db.String(100)) # 文件夹分组
method = db.Column(db.String(10), nullable=False)
url = db.Column(db.Text, nullable=False)
headers = db.Column(db.Text)
params = db.Column(db.Text)
body = db.Column(db.Text)
body_type = db.Column(db.String(20))
auth_type = db.Column(db.String(20))
auth_config = db.Column(db.Text)
tags = db.Column(db.Text) # JSON 数组
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
def to_dict(self):
"""转换为字典"""
return {
'id': self.id,
'name': self.name,
'description': self.description,
'folder': self.folder,
'method': self.method,
'url': self.url,
'headers': self.headers,
'params': self.params,
'body': self.body,
'bodyType': self.body_type,
'authType': self.auth_type,
'authConfig': self.auth_config,
'tags': self.tags,
'createdAt': self.created_at.isoformat() if self.created_at else None,
'updatedAt': self.updated_at.isoformat() if self.updated_at else None
}
class Environment(db.Model):
"""环境变量模型"""
__tablename__ = 'environments'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50), nullable=False, unique=True)
variables = db.Column(db.Text) # JSON 格式 {"key": "value"}
is_active = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def to_dict(self):
"""转换为字典"""
return {
'id': self.id,
'name': self.name,
'variables': self.variables,
'isActive': self.is_active,
'createdAt': self.created_at.isoformat() if self.created_at else None
}