Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
|
b4ebbba30c | |
|
ac427e9134 |
|
@ -4,11 +4,12 @@ import (
|
|||
"context"
|
||||
"embed"
|
||||
"flag"
|
||||
"git.nobla.cn/golang/kos/entry/cli"
|
||||
"git.nobla.cn/golang/kos/entry/http"
|
||||
httpkg "net/http"
|
||||
"time"
|
||||
|
||||
"git.nobla.cn/golang/kos/entry/cli"
|
||||
"git.nobla.cn/golang/kos/entry/http"
|
||||
|
||||
"git.nobla.cn/golang/kos"
|
||||
)
|
||||
|
||||
|
@ -32,8 +33,8 @@ func (s *subServer) Start(ctx context.Context) (err error) {
|
|||
|
||||
kos.Command().Handle("/test", "test command", func(ctx *cli.Context) (err error) {
|
||||
return ctx.Success([][]string{
|
||||
[]string{"NAME", "AGE"},
|
||||
[]string{"SSS", "aaa"},
|
||||
{"NAME", "AGE"},
|
||||
{"SSS", "aaa"},
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
package http
|
||||
|
||||
import "git.nobla.cn/golang/kos/pkg/types"
|
||||
|
||||
const (
|
||||
ErrAccessDenied = 4003 //拒绝访问
|
||||
ErrPermissionDenied = 4004 //没有权限
|
||||
ErrIllegalRequest = 4005 //非法请求
|
||||
ErrInvalidPayload = 4006 //请求数据无效
|
||||
ErrResourceCreate = 4101 //资源创建失败
|
||||
ErrResourceUpdate = 4102 //资源更新失败
|
||||
ErrResourceDelete = 4103 //资源删除失败
|
||||
ErrResourceNotFound = 4104 //资源未找到
|
||||
ErrResourceEmpty = 4105 //资源为空
|
||||
ErrResourceExpired = 4107 //资源已失效
|
||||
ErrResourceUnavailable = 4108 //资源无法使用
|
||||
ErrResourceLocked = 4109 //资源已被锁定
|
||||
ErrServerUnreachable = 4201 //服务不可用
|
||||
ErrTemporaryUnavailable = 4202 //临时性失败
|
||||
ErrFatal = 4204 //致命错误
|
||||
ErrAccessDenied = types.ErrAccessDenied //拒绝访问
|
||||
ErrPermissionDenied = types.ErrPermissionDenied //没有权限
|
||||
ErrIllegalRequest = types.ErrIllegalRequest //非法请求
|
||||
ErrInvalidPayload = types.ErrInvalidPayload //请求数据无效
|
||||
ErrResourceCreate = types.ErrResourceCreate //资源创建失败
|
||||
ErrResourceUpdate = types.ErrResourceUpdate //资源更新失败
|
||||
ErrResourceDelete = types.ErrResourceDelete //资源删除失败
|
||||
ErrResourceNotFound = types.ErrResourceNotFound //资源未找到
|
||||
ErrResourceEmpty = types.ErrResourceEmpty //资源为空
|
||||
ErrResourceExpired = types.ErrResourceExpired //资源已失效
|
||||
ErrResourceUnavailable = types.ErrResourceUnavailable //资源无法使用
|
||||
ErrResourceLocked = types.ErrResourceLocked //资源已被锁定
|
||||
ErrServerUnreachable = types.ErrServerUnreachable //服务不可用
|
||||
ErrTemporaryUnavailable = types.ErrTemporaryUnavailable //临时性失败
|
||||
ErrFatal = types.ErrFatal //致命错误
|
||||
)
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package types
|
||||
|
||||
const (
|
||||
ErrAccessDenied = 4003 //拒绝访问
|
||||
ErrPermissionDenied = 4004 //没有权限
|
||||
ErrIllegalRequest = 4005 //非法请求
|
||||
ErrInvalidPayload = 4006 //请求数据无效
|
||||
ErrResourceCreate = 4101 //资源创建失败
|
||||
ErrResourceUpdate = 4102 //资源更新失败
|
||||
ErrResourceDelete = 4103 //资源删除失败
|
||||
ErrResourceNotFound = 4104 //资源未找到
|
||||
ErrResourceEmpty = 4105 //资源为空
|
||||
ErrResourceExpired = 4107 //资源已失效
|
||||
ErrResourceUnavailable = 4108 //资源无法使用
|
||||
ErrResourceLocked = 4109 //资源已被锁定
|
||||
ErrServerUnreachable = 4201 //服务不可用
|
||||
ErrTemporaryUnavailable = 4202 //临时性失败
|
||||
ErrFatal = 4204 //致命错误
|
||||
)
|
|
@ -7,7 +7,6 @@ import (
|
|||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"git.nobla.cn/golang/kos/util/env"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -15,6 +14,8 @@ import (
|
|||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.nobla.cn/golang/kos/util/env"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -67,6 +68,7 @@ func encode(data any) (r io.Reader, contentType string, err error) {
|
|||
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) {
|
||||
var (
|
||||
uri *url.URL
|
||||
|
@ -86,7 +88,7 @@ func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Respon
|
|||
}
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
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) {
|
||||
var (
|
||||
uri *url.URL
|
||||
|
@ -123,7 +126,7 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
|
|||
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
|
||||
}
|
||||
if opts.Header != nil {
|
||||
|
@ -134,28 +137,23 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
|
|||
if 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) {
|
||||
cbs = append(cbs, WithMethod(method))
|
||||
return Request(ctx, uri, response, cbs...)
|
||||
}
|
||||
|
||||
func Request(ctx context.Context, u string, response any, cbs ...Option) (err error) {
|
||||
// Request is a generic HTTP request function that can handle GET, POST, PUT, DELETE, etc.
|
||||
func Request(ctx context.Context, urlString string, result any, cbs ...Option) (err error) {
|
||||
var (
|
||||
buf []byte
|
||||
contentType string
|
||||
reader io.Reader
|
||||
uri *url.URL
|
||||
res *http.Response
|
||||
req *http.Request
|
||||
contentType string
|
||||
reader io.Reader
|
||||
)
|
||||
opts := newOptions()
|
||||
for _, cb := range cbs {
|
||||
cb(opts)
|
||||
}
|
||||
if uri, err = url.Parse(u); err != nil {
|
||||
if uri, err = url.Parse(urlString); err != nil {
|
||||
return
|
||||
}
|
||||
if opts.Params != nil {
|
||||
|
@ -170,7 +168,7 @@ func Request(ctx context.Context, u string, response any, cbs ...Option) (err er
|
|||
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
|
||||
}
|
||||
if opts.Header != nil {
|
||||
|
@ -181,45 +179,41 @@ func Request(ctx context.Context, u string, response any, cbs ...Option) (err er
|
|||
if 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
|
||||
}
|
||||
defer func() {
|
||||
_ = res.Body.Close()
|
||||
}()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
if buf, err = io.ReadAll(res.Body); err == nil && len(buf) > 0 {
|
||||
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)
|
||||
}
|
||||
err = fmt.Errorf("ubexpected HTTP status code: %d", res.StatusCode)
|
||||
return
|
||||
}
|
||||
//don't care response
|
||||
if response == nil {
|
||||
return
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
contentType = strings.ToLower(res.Header.Get("Content-Type"))
|
||||
extName := path.Ext(req.URL.String())
|
||||
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" {
|
||||
err = xml.NewDecoder(res.Body).Decode(response)
|
||||
err = xml.NewDecoder(res.Body).Decode(result)
|
||||
} else {
|
||||
err = fmt.Errorf("unsupported content type: %s", contentType)
|
||||
}
|
||||
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()
|
||||
for _, cb := range cbs {
|
||||
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 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")
|
||||
|
@ -227,9 +221,6 @@ func do(ctx context.Context, req *http.Request, opts *Options) (res *http.Respon
|
|||
if req.Header.Get("Referer") == "" {
|
||||
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