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
}
