mask.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "go-common/library/log"
  9. )
  10. const _mask = "/dl/api/masks/v1"
  11. func (d *Dao) maskURI() string {
  12. return d.conf.Host.Mask + _mask
  13. }
  14. // GenerateMask ask AI to generate dm mask
  15. func (d *Dao) GenerateMask(c context.Context, cid, mid int64, plat int8, priority int32, aid int64, duration int64, typeID int32) (err error) {
  16. var (
  17. res struct {
  18. Code int64 `json:"errcode"`
  19. Message string `json:"errmsg"`
  20. }
  21. params = make(map[string]interface{})
  22. )
  23. params["cid"] = cid
  24. params["mask_platform"] = plat
  25. params["priority"] = priority
  26. params["mid"] = mid
  27. params["aid"] = aid
  28. params["duration"] = duration
  29. params["region_2"] = typeID
  30. data, err := json.Marshal(params)
  31. if err != nil {
  32. log.Error("json.Marshal(%v) error(%v)", params, err)
  33. return
  34. }
  35. reader := bytes.NewReader(data)
  36. req, err := http.NewRequest("POST", d.maskURI(), reader)
  37. if err != nil {
  38. log.Error("http.NewRequest error(%v)", err)
  39. return
  40. }
  41. req.Header.Set("Content-Type", "application/json")
  42. for i := 0; i < 3; i++ {
  43. if err = d.httpCli.Do(c, req, &res); err != nil {
  44. log.Error("d.httpCli.DO(%v) error(%v)", req, err)
  45. continue
  46. }
  47. if res.Code != 200 {
  48. err = fmt.Errorf("uri:%s,code:%d", d.maskURI(), res.Code)
  49. log.Error("http code error(%v)", err)
  50. continue
  51. }
  52. log.Info("send genarate mask request succeed(cid:%d)", cid)
  53. break
  54. }
  55. return
  56. }