Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
|
b4ebbba30c | |
|
ac427e9134 |
|
@ -4,11 +4,12 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
"git.nobla.cn/golang/kos/entry/cli"
|
|
||||||
"git.nobla.cn/golang/kos/entry/http"
|
|
||||||
httpkg "net/http"
|
httpkg "net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.nobla.cn/golang/kos/entry/cli"
|
||||||
|
"git.nobla.cn/golang/kos/entry/http"
|
||||||
|
|
||||||
"git.nobla.cn/golang/kos"
|
"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) {
|
kos.Command().Handle("/test", "test command", func(ctx *cli.Context) (err error) {
|
||||||
return ctx.Success([][]string{
|
return ctx.Success([][]string{
|
||||||
[]string{"NAME", "AGE"},
|
{"NAME", "AGE"},
|
||||||
[]string{"SSS", "aaa"},
|
{"SSS", "aaa"},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
|
import "git.nobla.cn/golang/kos/pkg/types"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ErrAccessDenied = 4003 //拒绝访问
|
ErrAccessDenied = types.ErrAccessDenied //拒绝访问
|
||||||
ErrPermissionDenied = 4004 //没有权限
|
ErrPermissionDenied = types.ErrPermissionDenied //没有权限
|
||||||
ErrIllegalRequest = 4005 //非法请求
|
ErrIllegalRequest = types.ErrIllegalRequest //非法请求
|
||||||
ErrInvalidPayload = 4006 //请求数据无效
|
ErrInvalidPayload = types.ErrInvalidPayload //请求数据无效
|
||||||
ErrResourceCreate = 4101 //资源创建失败
|
ErrResourceCreate = types.ErrResourceCreate //资源创建失败
|
||||||
ErrResourceUpdate = 4102 //资源更新失败
|
ErrResourceUpdate = types.ErrResourceUpdate //资源更新失败
|
||||||
ErrResourceDelete = 4103 //资源删除失败
|
ErrResourceDelete = types.ErrResourceDelete //资源删除失败
|
||||||
ErrResourceNotFound = 4104 //资源未找到
|
ErrResourceNotFound = types.ErrResourceNotFound //资源未找到
|
||||||
ErrResourceEmpty = 4105 //资源为空
|
ErrResourceEmpty = types.ErrResourceEmpty //资源为空
|
||||||
ErrResourceExpired = 4107 //资源已失效
|
ErrResourceExpired = types.ErrResourceExpired //资源已失效
|
||||||
ErrResourceUnavailable = 4108 //资源无法使用
|
ErrResourceUnavailable = types.ErrResourceUnavailable //资源无法使用
|
||||||
ErrResourceLocked = 4109 //资源已被锁定
|
ErrResourceLocked = types.ErrResourceLocked //资源已被锁定
|
||||||
ErrServerUnreachable = 4201 //服务不可用
|
ErrServerUnreachable = types.ErrServerUnreachable //服务不可用
|
||||||
ErrTemporaryUnavailable = 4202 //临时性失败
|
ErrTemporaryUnavailable = types.ErrTemporaryUnavailable //临时性失败
|
||||||
ErrFatal = 4204 //致命错误
|
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/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)
|
||||||
return httpClient.Do(req.WithContext(ctx))
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue