open.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "go-common/app/admin/main/apm/conf"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. // OpenProxy proxy.
  11. func (s *Service) OpenProxy(c context.Context, method, loc string, params url.Values) (data interface{}, err error) {
  12. uri := conf.Conf.Host.MANAGERCo + "/" + loc
  13. data, err = s.ocurl(c, method, uri, loc, params)
  14. return
  15. }
  16. func (s *Service) ocurl(c context.Context, method, uri, loc string, params url.Values) (data json.RawMessage, err error) {
  17. var res struct {
  18. Code int `json:"code"`
  19. Data json.RawMessage `json:"data"`
  20. Message string `json:"message"`
  21. }
  22. if method == "GET" {
  23. if err = s.client.Get(c, uri, "", params, &res); err != nil {
  24. log.Error("apmSvc.OpenProxy get url:"+uri+" params:(%v) error(%v)", params.Encode(), err)
  25. return
  26. }
  27. } else {
  28. if err = s.client.Post(c, uri, "", params, &res); err != nil {
  29. log.Error("apmSvc.OpenProxy post url:"+uri+" params:(%v) error(%v)", params.Encode(), err)
  30. return
  31. }
  32. }
  33. if res.Code != 0 {
  34. err = ecode.Int(res.Code)
  35. log.Error("apmSvc.OpenProxy url:"+uri+" params:(%v) ecode(%v)", params.Encode(), err)
  36. return
  37. }
  38. data = res.Data
  39. return
  40. }