mask.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // GenerateMask ask AI to generate dm mask
  12. func (d *Dao) GenerateMask(c context.Context, cid, mid int64, plat int8, force int8, aid int64, duration int64, typeID int32) (err error) {
  13. var (
  14. res struct {
  15. Code int64 `json:"errcode"`
  16. Message string `json:"errmsg"`
  17. }
  18. params = make(map[string]interface{})
  19. )
  20. params["cid"] = cid
  21. params["mask_platform"] = plat
  22. params["force"] = force
  23. params["mid"] = mid
  24. params["aid"] = aid
  25. params["duration"] = duration
  26. params["region_2"] = typeID
  27. data, err := json.Marshal(params)
  28. if err != nil {
  29. log.Error("json.Marshal(%v) error(%v)", params, err)
  30. return
  31. }
  32. reader := bytes.NewReader(data)
  33. req, err := http.NewRequest("POST", d.maskURI, reader)
  34. if err != nil {
  35. log.Error("http.NewRequest error(%v)", err)
  36. return
  37. }
  38. req.Header.Set("Content-Type", "application/json")
  39. for i := 0; i < 3; i++ {
  40. if err = d.httpCli.Do(c, req, &res); err != nil {
  41. continue
  42. }
  43. if res.Code != 200 {
  44. err = fmt.Errorf("uri:%s,code:%d", d.maskURI, res.Code)
  45. continue
  46. }
  47. break
  48. }
  49. if err != nil {
  50. log.Error("d.GenerateMask(cid:%d, plat:%d) error(%v)", cid, plat, err)
  51. }
  52. return
  53. }