archive.go 877 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "time"
  8. "github.com/pkg/errors"
  9. )
  10. // PvData get binary data from pvdata url
  11. func (d *Dao) PvData(c context.Context, pvURL string) (res []byte, err error) {
  12. var (
  13. req *http.Request
  14. resp *http.Response
  15. cancel func()
  16. )
  17. if req, err = http.NewRequest("GET", pvURL, nil); err != nil {
  18. err = errors.Wrapf(err, "PvData http.NewRequest(%s)", pvURL)
  19. return
  20. }
  21. c, cancel = context.WithTimeout(c, time.Duration(d.c.Rule.VsTimeout))
  22. defer cancel()
  23. req = req.WithContext(c)
  24. if resp, err = d.vsClient.Do(req); err != nil {
  25. err = errors.Wrapf(err, "httpClient.Do(%s)", pvURL)
  26. return
  27. }
  28. defer resp.Body.Close()
  29. if resp.StatusCode >= http.StatusBadRequest {
  30. err = fmt.Errorf("PvData url(%s) resp.StatusCode(%v)", pvURL, resp.StatusCode)
  31. return
  32. }
  33. res, err = ioutil.ReadAll(resp.Body)
  34. return
  35. }