dao.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package dynamic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/interface/main/app-feed/conf"
  6. httpx "go-common/library/net/http/blademaster"
  7. "go-common/library/net/metadata"
  8. "github.com/pkg/errors"
  9. )
  10. var (
  11. _dynamicGet = "/dynamic_svr/v1/dynamic_svr/dynamic_new"
  12. _dynamicHistory = "/dynamic_svr/v1/dynamic_svr/dynamic_history"
  13. )
  14. // Dao .
  15. type Dao struct {
  16. client *httpx.Client
  17. host string
  18. }
  19. // New .
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. client: httpx.NewClient(c.HTTPDynamic),
  23. host: c.Host.Dynamic,
  24. }
  25. return
  26. }
  27. func (d *Dao) dynamicSrv(c context.Context, reqPath string, params string) (res json.RawMessage, err error) {
  28. ip := metadata.String(c, metadata.RemoteIP)
  29. uri := d.host + reqPath + "?" + params
  30. if err = d.client.Get(c, uri, ip, nil, &res); err != nil {
  31. err = errors.Wrap(err, uri)
  32. }
  33. return
  34. }
  35. // DynamicHistory .
  36. func (d *Dao) DynamicHistory(c context.Context, params string) (res json.RawMessage, err error) {
  37. res, err = d.dynamicSrv(c, _dynamicHistory, params)
  38. return
  39. }
  40. // DynamicCount .
  41. func (d *Dao) DynamicCount(c context.Context, params string) (res json.RawMessage, err error) {
  42. res, err = d.dynamicSrv(c, _dynamicGet, params)
  43. return
  44. }
  45. // DynamicNew .
  46. func (d *Dao) DynamicNew(c context.Context, params string) (res json.RawMessage, err error) {
  47. res, err = d.dynamicSrv(c, _dynamicGet, params)
  48. return
  49. }