http_utils.go 952 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package webutils
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/sirupsen/logrus"
  6. )
  7. func GetResp[T interface{}](reqUrl string, timeout time.Duration) (*ResponseGen[T], error) {
  8. logrus.Info("request, url:", reqUrl)
  9. resp, err := Get(reqUrl, timeout)
  10. if err != nil {
  11. return nil, err
  12. }
  13. respData := ResponseGen[T]{}
  14. err = json.Unmarshal([]byte(resp), &respData)
  15. if err != nil {
  16. return nil, err
  17. }
  18. logrus.Info("response: url: ", reqUrl, " body:", respData)
  19. return &respData, nil
  20. }
  21. func PostResp[T interface{}](reqUrl string, payload string, timeout time.Duration) (*ResponseGen[T], error) {
  22. logrus.Info("request, url:", reqUrl, " payload:", payload)
  23. resp, err := Post(reqUrl, payload, timeout)
  24. if err != nil {
  25. return nil, err
  26. }
  27. respData := ResponseGen[T]{}
  28. err = json.Unmarshal([]byte(resp), &respData)
  29. if err != nil {
  30. return nil, err
  31. }
  32. logrus.Info("response: url: ", reqUrl, " body:", respData)
  33. return &respData, nil
  34. }