htmgo/framework/util/httpjson/http.go

64 lines
1 KiB
Go
Raw Normal View History

2024-01-22 15:22:16 +00:00
package httpjson
import (
"encoding/json"
2024-09-11 00:52:18 +00:00
"errors"
"fmt"
2024-01-22 15:22:16 +00:00
"io"
"net/http"
"sync"
"time"
)
var client *http.Client
var once sync.Once
func getClient() *http.Client {
once.Do(func() {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 15 * time.Second,
DisableKeepAlives: false,
}
httpClient := &http.Client{
Transport: tr,
}
client = httpClient
})
return client
}
func Get[T any](url string) (T, error) {
resp, err := getClient().Get(url)
if err != nil {
return *new(T), err
}
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
2024-09-11 00:52:18 +00:00
if resp.StatusCode > 299 {
return *new(T), errors.New(fmt.Sprintf("get to %s failed with %d code", url, resp.StatusCode))
}
2024-01-22 15:22:16 +00:00
body, err := io.ReadAll(resp.Body)
if err != nil {
return *new(T), err
}
d := new(T)
err = json.Unmarshal(body, &d)
if err != nil {
return *new(T), err
}
2024-09-11 00:52:18 +00:00
if d == nil {
return *new(T), errors.New("failed to create T")
}
2024-01-22 15:22:16 +00:00
return *d, nil
}