yst.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "go-common/app/service/main/tv/internal/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. )
  13. const (
  14. ystUrl = "https://%s/%s"
  15. ystCreateOrderPath = "getBiliOrder"
  16. ystRenewOrderPath = "papPayApply"
  17. ystOrderStatePath = "getBiliOrderState"
  18. )
  19. // YstClient represents http client for sending requests to yst.
  20. type YstClient struct {
  21. cli *bm.Client
  22. }
  23. // NewYstClient news a yst client.
  24. func NewYstClient(cli *bm.Client) *YstClient {
  25. return &YstClient{cli: cli}
  26. }
  27. // Post sends one post request.
  28. func (yc *YstClient) Post(c context.Context, uri string, data interface{}, res interface{}) error {
  29. req, err := yc.NewRequest(uri, data)
  30. if err != nil {
  31. return err
  32. }
  33. return yc.cli.Do(c, req, res)
  34. }
  35. // NewRequest news a post request.
  36. func (yc *YstClient) NewRequest(uri string, data interface{}) (request *http.Request, err error) {
  37. var dataBytes []byte
  38. dataBytes, err = json.Marshal(data)
  39. if err != nil {
  40. return
  41. }
  42. request, err = http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(dataBytes))
  43. if err != nil {
  44. return
  45. }
  46. request.Header.Set("Content-Type", "application/json")
  47. return request, nil
  48. }
  49. func resultCode2err(code string) error {
  50. switch code {
  51. case model.YstResultFail:
  52. return ecode.TVIPYstRequestErr
  53. case model.YstResultSysErr:
  54. return ecode.TVIPYstSystemErr
  55. default:
  56. return ecode.TVIPYstUnknownErr
  57. }
  58. }
  59. // CreateYstOrder creates one yst order.
  60. func (d *Dao) CreateYstOrder(c context.Context, req *model.YstCreateOrderReq) (res *model.YstCreateOrderReply, err error) {
  61. req.Sign, err = d.signer.Sign(req)
  62. if err != nil {
  63. return
  64. }
  65. res = &model.YstCreateOrderReply{}
  66. uri := fmt.Sprintf(ystUrl, d.c.YST.Domain, ystCreateOrderPath)
  67. if err = d.ystCli.Post(c, uri, req, res); err != nil {
  68. log.Error("d.ystCli.Post(%s, %+v) err(%+v)", uri, req, err)
  69. return
  70. }
  71. if res.ResultCode != model.YstResultSuccess {
  72. log.Error("d.ystCli.Post(%s, %+v) res(%+v)", uri, req, res)
  73. return nil, resultCode2err(res.ResultCode)
  74. }
  75. log.Info("dao.CreateYstOrder(%+v) res(%+v)", req, res)
  76. return
  77. }
  78. // RenewYstOrder creates one renew order.
  79. func (d *Dao) RenewYstOrder(c context.Context, req *model.YstRenewOrderReq) (res *model.YstRenewOrderReply, err error) {
  80. req.Sign, err = d.signer.Sign(req)
  81. if err != nil {
  82. return
  83. }
  84. res = &model.YstRenewOrderReply{}
  85. uri := fmt.Sprintf(ystUrl, d.c.YST.Domain, ystRenewOrderPath)
  86. if err = d.ystCli.Post(c, uri, req, res); err != nil {
  87. log.Error("d.ystCli.Post(%s, %+v) err(%+v)", uri, req, err)
  88. return
  89. }
  90. if res.ResultCode != model.YstResultSuccess {
  91. log.Error("d.ystCli.Post(%s, %+v) res(%+v)", uri, req, res)
  92. return nil, resultCode2err(res.ResultCode)
  93. }
  94. log.Info("dao.RenewYstOrder(%+v) res(%+v)", req, res)
  95. return
  96. }
  97. // YstOrderState queries order details from yst.
  98. func (d *Dao) YstOrderState(c context.Context, req *model.YstOrderStateReq) (res *model.YstOrderStateReply, err error) {
  99. req.Sign, err = d.signer.Sign(req)
  100. if err != nil {
  101. return
  102. }
  103. res = &model.YstOrderStateReply{}
  104. uri := fmt.Sprintf(ystUrl, d.c.YST.Domain, ystOrderStatePath)
  105. if err = d.ystCli.Post(c, uri, req, res); err != nil {
  106. log.Error("d.ystCli.Post(%s, %+v) err(%+v)", uri, req, err)
  107. return
  108. }
  109. if res.Result != model.YstResultSuccess {
  110. log.Error("d.ystCli.Post(%s, %+v) res(%+v)", uri, req, res)
  111. return nil, resultCode2err(res.Result)
  112. }
  113. log.Info("dao.YstOrderState(%+v) res(%+v)", req, res)
  114. return
  115. }