request_builder_params.go 1.2 KB

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