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