repositories.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from datetime import datetime
  2. from sqlalchemy import select
  3. from sqlalchemy.orm import Session
  4. from app.db.models import ApiKey, GatewayRequestAudit
  5. class GatewayRequestAuditRepository:
  6. def __init__(self, db: Session) -> None:
  7. self.db = db
  8. def create(
  9. self,
  10. *,
  11. tenant_id: str,
  12. request_id: str,
  13. method: str,
  14. path: str,
  15. query_string: str | None,
  16. target_service: str | None,
  17. target_url: str | None,
  18. status_code: int | None,
  19. duration_ms: int,
  20. client_host: str | None,
  21. user_agent: str | None,
  22. error_message: str | None,
  23. ) -> GatewayRequestAudit:
  24. entity = GatewayRequestAudit(
  25. tenant_id=tenant_id,
  26. request_id=request_id,
  27. method=method,
  28. path=path,
  29. query_string=query_string,
  30. target_service=target_service,
  31. target_url=target_url,
  32. status_code=status_code,
  33. duration_ms=duration_ms,
  34. client_host=client_host,
  35. user_agent=user_agent,
  36. error_message=error_message,
  37. )
  38. self.db.add(entity)
  39. self.db.commit()
  40. self.db.refresh(entity)
  41. return entity
  42. def list_by_scope(
  43. self,
  44. *,
  45. tenant_id: str,
  46. request_id: str | None = None,
  47. target_service: str | None = None,
  48. limit: int = 100,
  49. ) -> list[GatewayRequestAudit]:
  50. stmt = select(GatewayRequestAudit).where(GatewayRequestAudit.tenant_id == tenant_id)
  51. if request_id is not None:
  52. stmt = stmt.where(GatewayRequestAudit.request_id == request_id)
  53. if target_service is not None:
  54. stmt = stmt.where(GatewayRequestAudit.target_service == target_service)
  55. stmt = stmt.order_by(GatewayRequestAudit.created_time.desc()).limit(limit)
  56. return list(self.db.scalars(stmt))
  57. class ApiKeyRepository:
  58. def __init__(self, db: Session) -> None:
  59. self.db = db
  60. def create(
  61. self,
  62. *,
  63. tenant_id: str,
  64. name: str,
  65. key_prefix: str,
  66. key_hash: str,
  67. scopes: str | None,
  68. expires_time: datetime | None,
  69. ) -> ApiKey:
  70. entity = ApiKey(
  71. tenant_id=tenant_id,
  72. name=name,
  73. key_prefix=key_prefix,
  74. key_hash=key_hash,
  75. status="active",
  76. scopes=scopes,
  77. expires_time=expires_time,
  78. )
  79. self.db.add(entity)
  80. self.db.commit()
  81. self.db.refresh(entity)
  82. return entity
  83. def list_by_tenant(self, *, tenant_id: str) -> list[ApiKey]:
  84. stmt = (
  85. select(ApiKey)
  86. .where(ApiKey.tenant_id == tenant_id)
  87. .order_by(ApiKey.created_time.desc())
  88. )
  89. return list(self.db.scalars(stmt))
  90. def has_any(self) -> bool:
  91. stmt = select(ApiKey.id).limit(1)
  92. return self.db.scalar(stmt) is not None
  93. def get_by_id(self, *, tenant_id: str, api_key_id: str) -> ApiKey | None:
  94. stmt = (
  95. select(ApiKey)
  96. .where(ApiKey.tenant_id == tenant_id)
  97. .where(ApiKey.id == api_key_id)
  98. .limit(1)
  99. )
  100. return self.db.scalar(stmt)
  101. def get_active_by_hash(self, *, key_hash: str) -> ApiKey | None:
  102. stmt = (
  103. select(ApiKey)
  104. .where(ApiKey.key_hash == key_hash)
  105. .where(ApiKey.status == "active")
  106. .limit(1)
  107. )
  108. return self.db.scalar(stmt)
  109. def touch_last_used_time(self, *, api_key_id: str) -> None:
  110. entity = self.db.get(ApiKey, api_key_id)
  111. if entity is None:
  112. return
  113. entity.last_used_time = datetime.utcnow()
  114. self.db.commit()
  115. def update_status(
  116. self,
  117. *,
  118. tenant_id: str,
  119. api_key_id: str,
  120. status: str,
  121. ) -> ApiKey | None:
  122. entity = self.get_by_id(tenant_id=tenant_id, api_key_id=api_key_id)
  123. if entity is None:
  124. return None
  125. entity.status = status
  126. self.db.commit()
  127. self.db.refresh(entity)
  128. return entity