http.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "go-common/library/conf/env"
  8. "net/http"
  9. "net/url"
  10. )
  11. // NewRequst http 请求
  12. func (d *Dao) NewRequst(c context.Context, method string, url string, query url.Values, body []byte, headers map[string]string, resp interface{}) error {
  13. var req *http.Request
  14. if body != nil && len(body) > 0 {
  15. req, _ = http.NewRequest(method, url, bytes.NewBuffer(body))
  16. } else {
  17. req, _ = http.NewRequest(method, url, nil)
  18. }
  19. if query != nil {
  20. req.URL.RawQuery = query.Encode()
  21. }
  22. if headers != nil && len(headers) > 0 {
  23. for k, v := range headers {
  24. req.Header.Set(k, v)
  25. }
  26. }
  27. if err := d.httpClient.Do(c, req, &resp); err != nil {
  28. err = errors.WithStack(err)
  29. return err
  30. }
  31. return nil
  32. }
  33. // getLiveStreamUrl 对接live-stream.bilibili.co的相关业务
  34. func (d *Dao) getLiveStreamUrl(path string) string {
  35. url := ""
  36. if env.DeployEnv == env.DeployEnvProd {
  37. url = fmt.Sprintf("%s%s", "http://prod-live-stream.bilibili.co", path)
  38. } else {
  39. url = fmt.Sprintf("%s%s", "http://live-stream.bilibili.co", path)
  40. }
  41. return url
  42. }