package utils

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
)

func DoSimply(cli *http.Client, req *http.Request, resp any) error {
	log.Println("the webhook req is", req)
	rawResp, err := cli.Do(req)
	if err != nil {
		return err
	}
	defer rawResp.Body.Close()

	body, err := io.ReadAll(rawResp.Body)
	if err != nil {
		return err
	}

	if rawResp.StatusCode/100 != 2 {
		return fmt.Errorf("(code: %d, body: %s)", rawResp.StatusCode, string(body))
	}

	if resp != nil {
		err = json.Unmarshal(body, resp)
		if err != nil {
			return err
		}
	}

	return nil
}
