creative.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package datadao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. )
  11. // HTTPDataHandle .
  12. func (d *Dao) HTTPDataHandle(c context.Context, params url.Values, key string) (data interface{}, err error) {
  13. var (
  14. uri string
  15. res struct {
  16. Code int `json:"code"`
  17. Data json.RawMessage `json:"data"`
  18. Message string `json:"message"`
  19. }
  20. )
  21. if uri, err = d.getURI(key); err != nil {
  22. return
  23. }
  24. if err = d.bmClient.Get(c, uri, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  25. return
  26. }
  27. if res.Code != 0 {
  28. log.Error("d.bmClient.Get(%s,%d)", uri+"?"+params.Encode(), res.Code)
  29. err = ecode.Error(ecode.Int(res.Code), res.Message)
  30. return
  31. }
  32. data = res.Data
  33. return
  34. }
  35. // getURI .
  36. func (d *Dao) getURI(key string) (uri string, err error) {
  37. var (
  38. ok bool
  39. url struct {
  40. host string
  41. uri string
  42. }
  43. _url = map[string]struct {
  44. host string
  45. uri string
  46. }{
  47. "archives": {
  48. uri: "/x/internal/creative/archives",
  49. host: d.Conf.Host.API,
  50. },
  51. "archiveHistoryList": {
  52. uri: "/x/internal/creative/archive/history/list",
  53. host: d.Conf.Host.API,
  54. },
  55. "archiveVideos": {
  56. uri: "/x/internal/creative/archive/videos",
  57. host: d.Conf.Host.API,
  58. },
  59. "dataArchive": {
  60. uri: "/x/internal/creative/data/archive",
  61. host: d.Conf.Host.API,
  62. },
  63. "dataVideoQuit": {
  64. uri: "/x/internal/creative/data/videoquit",
  65. host: d.Conf.Host.API,
  66. },
  67. "danmuDistri": {
  68. uri: "/x/internal/creative/danmu/distri",
  69. host: d.Conf.Host.API,
  70. },
  71. "dataBase": {
  72. uri: "/x/internal/creative/data/base",
  73. host: d.Conf.Host.API,
  74. },
  75. "dataTrend": {
  76. uri: "/x/internal/creative/data/trend",
  77. host: d.Conf.Host.API,
  78. },
  79. "dataAction": {
  80. uri: "/x/internal/creative/data/action",
  81. host: d.Conf.Host.API,
  82. },
  83. "dataFan": {
  84. uri: "/x/internal/creative/data/fan",
  85. host: d.Conf.Host.API,
  86. },
  87. "dataPandect": {
  88. uri: "/x/internal/creative/data/pandect",
  89. host: d.Conf.Host.API,
  90. },
  91. "dataSurvey": {
  92. uri: "/x/internal/creative/data/survey",
  93. host: d.Conf.Host.API,
  94. },
  95. "dataPlaySource": {
  96. uri: "/x/internal/creative/data/playsource",
  97. host: d.Conf.Host.API,
  98. },
  99. "dataPlayAnalysis": {
  100. uri: "/x/internal/creative/data/playanalysis",
  101. host: d.Conf.Host.API,
  102. },
  103. "dataArticleRank": {
  104. uri: "/x/internal/creative/data/article/rank",
  105. host: d.Conf.Host.API,
  106. },
  107. }
  108. )
  109. if url, ok = _url[key]; !ok {
  110. return uri, fmt.Errorf("url(%s) not exist", key)
  111. }
  112. uri = url.host + url.uri
  113. return uri, err
  114. }