rename fetch
This commit is contained in:
parent
ffac331e3d
commit
b73084c1b5
util/fetch
|
@ -7,6 +7,7 @@ import (
|
|||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"git.nspix.com/golang/kos/util/env"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -18,7 +19,7 @@ import (
|
|||
|
||||
var (
|
||||
httpClient = http.Client{
|
||||
Timeout: time.Second * 15,
|
||||
Timeout: time.Second * 30,
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: &tls.Config{
|
||||
|
@ -29,7 +30,7 @@ var (
|
|||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: false,
|
||||
MaxIdleConns: 10,
|
||||
MaxIdleConns: 48,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
|
@ -37,6 +38,15 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpDefaultTimeout := env.Get("HTTP_CLIENT_TIMEOUT", "30s")
|
||||
if httpDefaultTimeout != "" {
|
||||
if duration, err := time.ParseDuration(httpDefaultTimeout); err == nil {
|
||||
httpClient.Timeout = duration
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func encode(data any) (r io.Reader, contentType string, err error) {
|
||||
var (
|
||||
buf []byte
|
||||
|
@ -127,7 +137,12 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
|
|||
return do(ctx, req, opts)
|
||||
}
|
||||
|
||||
func Request(ctx context.Context, urlString string, response any, cbs ...Option) (err error) {
|
||||
func Echo(ctx context.Context, method, uri string, response any, cbs ...Option) (err error) {
|
||||
cbs = append(cbs, WithMethod(method))
|
||||
return Request(ctx, uri, response, cbs...)
|
||||
}
|
||||
|
||||
func Request(ctx context.Context, u string, response any, cbs ...Option) (err error) {
|
||||
var (
|
||||
buf []byte
|
||||
uri *url.URL
|
||||
|
@ -140,7 +155,7 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
|
|||
for _, cb := range cbs {
|
||||
cb(opts)
|
||||
}
|
||||
if uri, err = url.Parse(urlString); err != nil {
|
||||
if uri, err = url.Parse(u); err != nil {
|
||||
return
|
||||
}
|
||||
if opts.Params != nil {
|
||||
|
@ -180,6 +195,10 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
|
|||
}
|
||||
return
|
||||
}
|
||||
//don't care response
|
||||
if response == nil {
|
||||
return
|
||||
}
|
||||
contentType = strings.ToLower(res.Header.Get("Content-Type"))
|
||||
extName := path.Ext(req.URL.String())
|
||||
if strings.Contains(contentType, JSON) || extName == ".json" {
|
||||
|
|
Loading…
Reference in New Issue