request_builder_params.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  5. "github.com/pkg/errors"
  6. "net/http"
  7. )
  8. type requestBuilderParams struct {
  9. requestItems []*builderRequestItem
  10. responseSuccessCallback ResponseSuccessCallback
  11. responseErrorCallback ResponseErrorCallback
  12. }
  13. func newRequestBuilderParams() *requestBuilderParams {
  14. return &requestBuilderParams{
  15. requestItems: make([]*builderRequestItem, 0),
  16. responseSuccessCallback: func(c *api.Context, historyRequests []Request, resultMap map[string]any) {
  17. c.Status(http.StatusOK)
  18. if historyRequests != nil && len(historyRequests) != 0 {
  19. _, err := c.Writer.Write(historyRequests[len(historyRequests)-1].Response().Body())
  20. if err != nil {
  21. logger.GetInstance().Error(errors.New(err.Error()))
  22. c.AbortWithStatus(http.StatusInternalServerError)
  23. return
  24. }
  25. }
  26. c.Writer.Flush()
  27. },
  28. responseErrorCallback: func(c *api.Context, historyRequests []Request, resultMap map[string]any, err error) {
  29. if err == nil {
  30. return
  31. }
  32. resp := make(map[string]any)
  33. logger.GetInstance().Error(err)
  34. resp["success"] = false
  35. resp["errCode"] = http.StatusOK
  36. resp["msg"] = err.Error()
  37. c.JSON(http.StatusOK, resp)
  38. },
  39. }
  40. }