crud.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from collections.abc import Sequence
  2. from datetime import datetime
  3. from typing import Any
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.plugin.module_payment.apikey.model import TenantApiKeyModel, TenantApiLogModel
  6. from app.plugin.module_payment.apikey.schema import (
  7. TenantApiKeyCreate,
  8. TenantApiKeyUpdate,
  9. TenantApiLogCreate,
  10. )
  11. from app.core.base_crud import CRUDBase
  12. class TenantApiKeyCRUD(CRUDBase[TenantApiKeyModel, TenantApiKeyCreate, TenantApiKeyUpdate]):
  13. """租户API Key数据层"""
  14. def __init__(self, auth: AuthSchema) -> None:
  15. self.auth = auth
  16. super().__init__(model=TenantApiKeyModel, auth=auth)
  17. async def get_by_id_crud(
  18. self, id: int, preload: list[str | Any] | None = None
  19. ) -> TenantApiKeyModel | None:
  20. return await self.get(id=id, preload=preload)
  21. async def get_by_api_key(self, api_key: str) -> TenantApiKeyModel | None:
  22. return await self.get(
  23. preload=[],
  24. api_key=api_key,
  25. status="0",
  26. # expired_at=("None", None),
  27. )
  28. async def create_crud(
  29. self,
  30. api_key: str,
  31. api_secret: str,
  32. tenant_id: int,
  33. expired_at: datetime,
  34. return_url: str | None = None,
  35. description: str | None = None,
  36. ) -> TenantApiKeyModel:
  37. data = {
  38. "tenant_id": tenant_id,
  39. "description": description,
  40. "api_key": api_key,
  41. "api_secret": api_secret,
  42. "expired_at": expired_at,
  43. "return_url": return_url,
  44. }
  45. obj = await self.create(data=data, skip_tenant_id=True)
  46. return obj
  47. async def update_status_crud(self, api_key_id: int, status: str) -> TenantApiKeyModel | None:
  48. return await self.update(id=api_key_id, data={"status": status})
  49. async def update_last_used_crud(self, api_key_id: int) -> None:
  50. api_key_obj = await self.get_by_id_crud(api_key_id)
  51. if api_key_obj:
  52. api_key_obj.last_used_at = datetime.now()
  53. await self.auth.db.flush()
  54. async def delete_crud(self, api_key_id: int) -> None:
  55. await self.delete(ids=[api_key_id])
  56. class TenantApiLogCRUD(CRUDBase[TenantApiLogModel, TenantApiLogCreate, TenantApiLogCreate]):
  57. """租户API调用日志数据层"""
  58. def __init__(self, auth: AuthSchema) -> None:
  59. self.auth = auth
  60. super().__init__(model=TenantApiLogModel, auth=auth)
  61. async def create_crud(
  62. self,
  63. api_key_id: int | None,
  64. tenant_id: int,
  65. endpoint: str,
  66. method: str,
  67. request_ip: str,
  68. request_data: str | None,
  69. response_code: int,
  70. response_time: float,
  71. ) -> TenantApiLogModel:
  72. data = TenantApiLogCreate(
  73. api_key_id=api_key_id,
  74. tenant_id=tenant_id,
  75. endpoint=endpoint,
  76. method=method,
  77. request_ip=request_ip,
  78. request_data=request_data,
  79. response_code=response_code,
  80. response_time=response_time,
  81. )
  82. return await self.create(data=data, skip_tenant_id=True)