ysf
This commit is contained in:
parent
6be34832c2
commit
d188d5d1e4
|
@ -18,43 +18,7 @@ type Options struct {
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option 是一个函数类型,用于设置RequestOptions的各个字段
|
func NewOptions(options ...Option) *Options {
|
||||||
type Option func(*Options)
|
|
||||||
|
|
||||||
// WithTimeout 设置请求超时时间的选项函数
|
|
||||||
func WithTimeout(timeout time.Duration) Option {
|
|
||||||
return func(options *Options) {
|
|
||||||
options.Timeout = timeout
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithHeaders 设置请求头的选项函数
|
|
||||||
func WithHeaders(headers http.Header) Option {
|
|
||||||
return func(options *Options) {
|
|
||||||
options.Headers = headers
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithStatusCodeFunc 设置自定义状态码处理函数的选项函数
|
|
||||||
func WithStatusCodeFunc(statusCodeFunc func(int) bool) Option {
|
|
||||||
return func(options *Options) {
|
|
||||||
options.StatusCodeFunc = statusCodeFunc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Post(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) {
|
|
||||||
return Request(ctx, http.MethodPost, url, body, options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Get(ctx context.Context, url string, options ...Option) (http.Header, []byte, error) {
|
|
||||||
return Request(ctx, http.MethodGet, url, nil, options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Put(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) {
|
|
||||||
return Request(ctx, http.MethodPut, url, body, options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Request(_ context.Context, method, url string, body []byte, options ...Option) (http.Header, []byte, error) {
|
|
||||||
o := &Options{
|
o := &Options{
|
||||||
Headers: http.Header{
|
Headers: http.Header{
|
||||||
"Content-Type": []string{"application/json"},
|
"Content-Type": []string{"application/json"},
|
||||||
|
@ -71,6 +35,42 @@ func Request(_ context.Context, method, url string, body []byte, options ...Opti
|
||||||
option(o)
|
option(o)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func WithTimeout(timeout time.Duration) Option {
|
||||||
|
return func(options *Options) {
|
||||||
|
options.Timeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithHeaders(headers http.Header) Option {
|
||||||
|
return func(options *Options) {
|
||||||
|
options.Headers = headers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithStatusCodeFunc(statusCodeFunc func(int) bool) Option {
|
||||||
|
return func(options *Options) {
|
||||||
|
options.StatusCodeFunc = statusCodeFunc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Post(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) {
|
||||||
|
return Request(ctx, http.MethodPost, url, body, NewOptions(options...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(ctx context.Context, url string, options ...Option) (http.Header, []byte, error) {
|
||||||
|
return Request(ctx, http.MethodGet, url, nil, NewOptions(options...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Put(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) {
|
||||||
|
return Request(ctx, http.MethodPut, url, body, NewOptions(options...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Request(_ context.Context, method, url string, body []byte, o *Options) (http.Header, []byte, error) {
|
||||||
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("创建HTTP请求失败: %w", err)
|
return nil, nil, fmt.Errorf("创建HTTP请求失败: %w", err)
|
||||||
|
|
Loading…
Reference in New Issue