12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package webutils
- import (
- "encoding/json"
- "time"
- "github.com/sirupsen/logrus"
- )
- func GetResp[T interface{}](reqUrl string, timeout time.Duration) (*ResponseGen[T], error) {
- logrus.Info("request, url:", reqUrl)
- resp, err := Get(reqUrl, timeout)
- if err != nil {
- return nil, err
- }
- respData := ResponseGen[T]{}
- err = json.Unmarshal([]byte(resp), &respData)
- if err != nil {
- return nil, err
- }
- logrus.Info("response: url: ", reqUrl, " body:", respData)
- return &respData, nil
- }
- func PostResp[T interface{}](reqUrl string, payload string, timeout time.Duration) (*ResponseGen[T], error) {
- logrus.Info("request, url:", reqUrl, " payload:", payload)
- resp, err := Post(reqUrl, payload, timeout)
- if err != nil {
- return nil, err
- }
- respData := ResponseGen[T]{}
- err = json.Unmarshal([]byte(resp), &respData)
- if err != nil {
- return nil, err
- }
- logrus.Info("response: url: ", reqUrl, " body:", respData)
- return &respData, nil
- }
|