request_builder_params.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. httpMethod string
  9. relativePath string
  10. requestItems []*builderRequestItem
  11. responseSuccessCallback ResponseSuccessCallback
  12. responseErrorCallback ResponseErrorCallback
  13. }
  14. func newRequestBuilderParams() *requestBuilderParams {
  15. return &requestBuilderParams{
  16. httpMethod: "",
  17. relativePath: "",
  18. requestItems: make([]*builderRequestItem, 0),
  19. responseSuccessCallback: func(requestBuilder *RequestBuilder) {
  20. requestBuilder.c.Status(http.StatusOK)
  21. if requestBuilder.historyRequests != nil && len(requestBuilder.historyRequests) != 0 {
  22. _, err := requestBuilder.c.Writer.Write(requestBuilder.historyRequests[len(requestBuilder.historyRequests)-1].Response().Body())
  23. if err != nil {
  24. logger.GetInstance().Error(errors.New(err.Error()))
  25. requestBuilder.c.AbortWithStatus(http.StatusInternalServerError)
  26. return
  27. }
  28. }
  29. requestBuilder.c.Writer.Flush()
  30. },
  31. responseErrorCallback: func(requestBuilder *RequestBuilder, err error) {
  32. if err == nil {
  33. return
  34. }
  35. resp := make(map[string]any)
  36. logger.GetInstance().Error(err)
  37. resp["success"] = false
  38. resp["errCode"] = http.StatusOK
  39. resp["msg"] = err.Error()
  40. requestBuilder.c.JSON(http.StatusOK, resp)
  41. },
  42. }
  43. }