endpoint.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from datetime import datetime
  2. from typing import Optional
  3. from pydantic import BaseModel, Field, model_validator
  4. from configs import dify_config
  5. from core.entities.provider_entities import ProviderConfig
  6. from core.plugin.entities.base import BasePluginEntity
  7. class EndpointDeclaration(BaseModel):
  8. """
  9. declaration of an endpoint
  10. """
  11. path: str
  12. method: str
  13. class EndpointProviderDeclaration(BaseModel):
  14. """
  15. declaration of an endpoint group
  16. """
  17. settings: list[ProviderConfig] = Field(default_factory=list)
  18. endpoints: Optional[list[EndpointDeclaration]] = Field(default_factory=list)
  19. class EndpointEntity(BasePluginEntity):
  20. """
  21. entity of an endpoint
  22. """
  23. settings: dict
  24. tenant_id: str
  25. plugin_id: str
  26. expired_at: datetime
  27. declaration: EndpointProviderDeclaration = Field(default_factory=EndpointProviderDeclaration)
  28. class EndpointEntityWithInstance(EndpointEntity):
  29. name: str
  30. enabled: bool
  31. url: str
  32. hook_id: str
  33. @model_validator(mode="before")
  34. @classmethod
  35. def render_url_template(cls, values):
  36. if "url" not in values:
  37. url_template = dify_config.ENDPOINT_URL_TEMPLATE
  38. values["url"] = url_template.replace("{hook_id}", values["hook_id"])
  39. return values