optimize code for fetch components
This commit is contained in:
parent
77ee2d8712
commit
ac427e9134
|
@ -7,7 +7,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.nobla.cn/golang/kos/util/env"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -15,6 +14,8 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.nobla.cn/golang/kos/util/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -67,6 +68,7 @@ func encode(data any) (r io.Reader, contentType string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get performs a GET request to the specified URL with optional parameters and headers.
|
||||||
func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
|
func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
|
||||||
var (
|
var (
|
||||||
uri *url.URL
|
uri *url.URL
|
||||||
|
@ -86,7 +88,7 @@ func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Respon
|
||||||
}
|
}
|
||||||
uri.RawQuery = qs.Encode()
|
uri.RawQuery = qs.Encode()
|
||||||
}
|
}
|
||||||
if req, err = http.NewRequest(http.MethodGet, uri.String(), nil); err != nil {
|
if req, err = http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if opts.Header != nil {
|
if opts.Header != nil {
|
||||||
|
@ -94,9 +96,10 @@ func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Respon
|
||||||
req.Header.Set(k, v)
|
req.Header.Set(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return do(ctx, req, opts)
|
return do(req, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Post performs a POST request to the specified URL with optional parameters, headers, and data.
|
||||||
func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
|
func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
|
||||||
var (
|
var (
|
||||||
uri *url.URL
|
uri *url.URL
|
||||||
|
@ -123,7 +126,7 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if req, err = http.NewRequest(http.MethodPost, uri.String(), reader); err != nil {
|
if req, err = http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), reader); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if opts.Header != nil {
|
if opts.Header != nil {
|
||||||
|
@ -134,28 +137,23 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
|
||||||
if contentType != "" {
|
if contentType != "" {
|
||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
return do(ctx, req, opts)
|
return do(req, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Echo(ctx context.Context, method, uri string, response any, cbs ...Option) (err error) {
|
// Request is a generic HTTP request function that can handle GET, POST, PUT, DELETE, etc.
|
||||||
cbs = append(cbs, WithMethod(method))
|
func Request(ctx context.Context, urlString string, result any, cbs ...Option) (err error) {
|
||||||
return Request(ctx, uri, response, cbs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Request(ctx context.Context, u string, response any, cbs ...Option) (err error) {
|
|
||||||
var (
|
var (
|
||||||
buf []byte
|
contentType string
|
||||||
|
reader io.Reader
|
||||||
uri *url.URL
|
uri *url.URL
|
||||||
res *http.Response
|
res *http.Response
|
||||||
req *http.Request
|
req *http.Request
|
||||||
contentType string
|
|
||||||
reader io.Reader
|
|
||||||
)
|
)
|
||||||
opts := newOptions()
|
opts := newOptions()
|
||||||
for _, cb := range cbs {
|
for _, cb := range cbs {
|
||||||
cb(opts)
|
cb(opts)
|
||||||
}
|
}
|
||||||
if uri, err = url.Parse(u); err != nil {
|
if uri, err = url.Parse(urlString); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if opts.Params != nil {
|
if opts.Params != nil {
|
||||||
|
@ -170,7 +168,7 @@ func Request(ctx context.Context, u string, response any, cbs ...Option) (err er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if req, err = http.NewRequest(opts.Method, uri.String(), reader); err != nil {
|
if req, err = http.NewRequestWithContext(ctx, opts.Method, uri.String(), reader); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if opts.Header != nil {
|
if opts.Header != nil {
|
||||||
|
@ -181,45 +179,41 @@ func Request(ctx context.Context, u string, response any, cbs ...Option) (err er
|
||||||
if contentType != "" {
|
if contentType != "" {
|
||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
if res, err = do(ctx, req, opts); err != nil {
|
if res, err = do(req, opts); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = res.Body.Close()
|
_ = res.Body.Close()
|
||||||
}()
|
}()
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
if buf, err = io.ReadAll(res.Body); err == nil && len(buf) > 0 {
|
err = fmt.Errorf("ubexpected HTTP status code: %d", res.StatusCode)
|
||||||
err = fmt.Errorf("remote server response %s(%d): %s", res.Status, res.StatusCode, string(buf))
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("remote server response %d: %s", res.StatusCode, res.Status)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//don't care response
|
//don't care response
|
||||||
if response == nil {
|
if result == nil {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
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" {
|
||||||
err = json.NewDecoder(res.Body).Decode(response)
|
err = json.NewDecoder(res.Body).Decode(result)
|
||||||
} else if strings.Contains(contentType, XML) || extName == ".xml" {
|
} else if strings.Contains(contentType, XML) || extName == ".xml" {
|
||||||
err = xml.NewDecoder(res.Body).Decode(response)
|
err = xml.NewDecoder(res.Body).Decode(result)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("unsupported content type: %s", contentType)
|
err = fmt.Errorf("unsupported content type: %s", contentType)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Do(ctx context.Context, req *http.Request, cbs ...Option) (res *http.Response, err error) {
|
func Do(req *http.Request, cbs ...Option) (res *http.Response, err error) {
|
||||||
opts := newOptions()
|
opts := newOptions()
|
||||||
for _, cb := range cbs {
|
for _, cb := range cbs {
|
||||||
cb(opts)
|
cb(opts)
|
||||||
}
|
}
|
||||||
return do(ctx, req, opts)
|
return do(req, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func do(ctx context.Context, req *http.Request, opts *Options) (res *http.Response, err error) {
|
func do(req *http.Request, opts *Options) (res *http.Response, err error) {
|
||||||
if opts.Human {
|
if opts.Human {
|
||||||
if req.Header.Get("User-Agent") == "" {
|
if req.Header.Get("User-Agent") == "" {
|
||||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54")
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54")
|
||||||
|
@ -227,9 +221,6 @@ func do(ctx context.Context, req *http.Request, opts *Options) (res *http.Respon
|
||||||
if req.Header.Get("Referer") == "" {
|
if req.Header.Get("Referer") == "" {
|
||||||
req.Header.Set("Referer", req.URL.String())
|
req.Header.Set("Referer", req.URL.String())
|
||||||
}
|
}
|
||||||
if req.Header.Get("Accept") == "" {
|
|
||||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return httpClient.Do(req.WithContext(ctx))
|
return httpClient.Do(req)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue