pointdata.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "time"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. // Leida get leidata.
  12. func (d *Dao) Leida(c context.Context, url string) (rs []byte, err error) {
  13. if rs, err = d.ThirdGet(c, url); err != nil {
  14. log.Error("d.ThirdGet url(%s) error(%+v)", url, err)
  15. }
  16. return
  17. }
  18. // ThirdGet get.
  19. func (d *Dao) ThirdGet(c context.Context, url string) (res []byte, err error) {
  20. var (
  21. req *http.Request
  22. resp *http.Response
  23. )
  24. if req, err = http.NewRequest("GET", url, nil); err != nil {
  25. err = errors.Wrapf(err, "ThirdGet http.NewRequest(%s)", url)
  26. return
  27. }
  28. ctx, cancel := context.WithTimeout(c, time.Duration(d.c.Leidata.Timeout))
  29. defer cancel()
  30. req = req.WithContext(ctx)
  31. if resp, err = d.ldClient.Do(req); err != nil {
  32. err = errors.Wrapf(err, "ThirdGet d.ldClient.Do(%s)", url)
  33. return
  34. }
  35. defer resp.Body.Close()
  36. if resp.StatusCode >= http.StatusBadRequest {
  37. err = fmt.Errorf("ThirdGet url(%s) resp.StatusCode(%v)", url, resp.StatusCode)
  38. return
  39. }
  40. res, err = ioutil.ReadAll(resp.Body)
  41. return
  42. }