query.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package v1_0
  4. import (
  5. "net/http"
  6. "adm-data-gateway/errors"
  7. param_v1 "adm-data-gateway/param/v1"
  8. "adm-data-gateway/pb"
  9. v1 "adm-data-gateway/pb/v1"
  10. // 这个加别名,区别其它目录的v1
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "git.getensh.com/common/gopkgsv2/tasker/httptasker"
  13. "git.getensh.com/common/gopkgsv2/util"
  14. "go.uber.org/zap"
  15. "github.com/gin-gonic/gin"
  16. )
  17. // query godoc
  18. // @Summary API查询
  19. // @Description API查询
  20. // @Tags query,v1.0
  21. // @Accept json
  22. // @Produce json
  23. // @Param appkey header string true "appkey"
  24. // @Param ts header string true "timestamp"
  25. // @Param sign header string true "签名"
  26. // @Param body body param_v1.QueryBody true "body"
  27. // @Success 200 {object} v1.QueryResponse "响应信息"
  28. // @Failure 500 {object} base.HTTPError
  29. // @Router /api/v1.0/query [post]
  30. func (c *Controller) Query(ctx *gin.Context) {
  31. // 解析参数
  32. req := &param_v1.QueryRequest{}
  33. parseParamTask := func() error {
  34. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.QueryBody)
  35. if err != nil {
  36. logger.Error("func",
  37. zap.String("call", "util.ShouldBind"),
  38. zap.String("error", err.Error()))
  39. return errors.ParamsError
  40. }
  41. return nil
  42. }
  43. // 业务处理
  44. handleServiceTask := func() error {
  45. // rpc调用
  46. req := &v1.QueryRequest{
  47. Code: req.Api,
  48. Params: req.Param,
  49. }
  50. res, err := pb.Data.Query(ctx.Request.Context(), req)
  51. if err != nil {
  52. s, _ := json.MarshalToString(req)
  53. logger.Error("func",
  54. zap.String("call", "pb.Data.Query"),
  55. zap.String("params", s),
  56. zap.String("error", err.Error()))
  57. return errors.ErrorTransForm(err)
  58. }
  59. // 响应数据
  60. resp := &param_v1.QyeryResponse{
  61. Data: res.Data,
  62. }
  63. ctx.JSON(http.StatusOK, resp)
  64. return nil
  65. }
  66. // 执行任务
  67. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  68. }