endpoint_declaration.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package plugin_entities
  2. import (
  3. "encoding/json"
  4. "github.com/go-playground/validator/v10"
  5. "github.com/langgenius/dify-plugin-daemon/pkg/validators"
  6. "gopkg.in/yaml.v3"
  7. )
  8. type EndpointMethod string
  9. const (
  10. EndpointMethodHead EndpointMethod = "HEAD"
  11. EndpointMethodGet EndpointMethod = "GET"
  12. EndpointMethodPost EndpointMethod = "POST"
  13. EndpointMethodPut EndpointMethod = "PUT"
  14. EndpointMethodDelete EndpointMethod = "DELETE"
  15. EndpointMethodOptions EndpointMethod = "OPTIONS"
  16. )
  17. func isAvailableMethod(fl validator.FieldLevel) bool {
  18. method := fl.Field().String()
  19. switch method {
  20. case string(EndpointMethodHead),
  21. string(EndpointMethodGet),
  22. string(EndpointMethodPost),
  23. string(EndpointMethodPut),
  24. string(EndpointMethodDelete),
  25. string(EndpointMethodOptions):
  26. return true
  27. }
  28. return false
  29. }
  30. func init() {
  31. validators.GlobalEntitiesValidator.RegisterValidation("is_available_endpoint_method", isAvailableMethod)
  32. }
  33. type EndpointDeclaration struct {
  34. Path string `json:"path" yaml:"path" validate:"required"`
  35. Method EndpointMethod `json:"method" yaml:"method" validate:"required,is_available_endpoint_method"`
  36. Hidden bool `json:"hidden" yaml:"hidden" validate:"omitempty"`
  37. }
  38. type EndpointProviderDeclaration struct {
  39. Settings []ProviderConfig `json:"settings" yaml:"settings" validate:"omitempty,dive"`
  40. Endpoints []EndpointDeclaration `json:"endpoints" yaml:"endpoint_declarations" validate:"omitempty,dive"`
  41. EndpointFiles []string `json:"-" yaml:"-"`
  42. }
  43. func (e *EndpointProviderDeclaration) UnmarshalYAML(node *yaml.Node) error {
  44. type alias EndpointProviderDeclaration
  45. var temp struct {
  46. alias `yaml:",inline"`
  47. Endpoints yaml.Node `yaml:"endpoints"`
  48. }
  49. if err := node.Decode(&temp); err != nil {
  50. return err
  51. }
  52. e.Settings = temp.Settings
  53. if temp.Endpoints.Kind == yaml.SequenceNode {
  54. for _, node := range temp.Endpoints.Content {
  55. if node.Kind == yaml.ScalarNode {
  56. e.EndpointFiles = append(e.EndpointFiles, node.Value)
  57. } else {
  58. var declaration EndpointDeclaration
  59. if err := node.Decode(&declaration); err != nil {
  60. return nil
  61. }
  62. e.Endpoints = append(e.Endpoints, declaration)
  63. }
  64. }
  65. }
  66. return nil
  67. }
  68. func (e *EndpointProviderDeclaration) UnmarshalJSON(data []byte) error {
  69. type alias EndpointProviderDeclaration
  70. var temp struct {
  71. alias
  72. Endpoints json.RawMessage `json:"endpoints"`
  73. }
  74. if err := json.Unmarshal(data, &temp); err != nil {
  75. return err
  76. }
  77. *e = EndpointProviderDeclaration(temp.alias)
  78. if len(temp.Endpoints) == 0 {
  79. return nil
  80. }
  81. var raw_endpoints []json.RawMessage
  82. if err := json.Unmarshal(temp.Endpoints, &raw_endpoints); err != nil {
  83. return err
  84. }
  85. for _, raw_endpoint := range raw_endpoints {
  86. var declaration EndpointDeclaration
  87. if err := json.Unmarshal(raw_endpoint, &declaration); err != nil {
  88. e.EndpointFiles = append(e.EndpointFiles, string(raw_endpoint))
  89. } else {
  90. e.Endpoints = append(e.Endpoints, declaration)
  91. }
  92. }
  93. return nil
  94. }