mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-03-11 11:06:53 +08:00
Feat: cancel archive action / request with context
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/HFO4/cloudreve/pkg/auth"
|
||||
@@ -38,6 +39,7 @@ type options struct {
|
||||
header http.Header
|
||||
sign auth.Auth
|
||||
signTTL int64
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type optionFunc func(*options)
|
||||
@@ -60,6 +62,14 @@ func WithTimeout(t time.Duration) Option {
|
||||
})
|
||||
}
|
||||
|
||||
// WithContext 设置请求上下文
|
||||
// TODO 测试
|
||||
func WithContext(c context.Context) Option {
|
||||
return optionFunc(func(o *options) {
|
||||
o.ctx = c
|
||||
})
|
||||
}
|
||||
|
||||
// WithCredential 对请求进行签名
|
||||
func WithCredential(instance auth.Auth, ttl int64) Option {
|
||||
return optionFunc(func(o *options) {
|
||||
@@ -87,7 +97,15 @@ func (c HTTPClient) Request(method, target string, body io.Reader, opts ...Optio
|
||||
client := &http.Client{Timeout: options.timeout}
|
||||
|
||||
// 创建请求
|
||||
req, err := http.NewRequest(method, target, body)
|
||||
var (
|
||||
req *http.Request
|
||||
err error
|
||||
)
|
||||
if options.ctx != nil {
|
||||
req, err = http.NewRequestWithContext(options.ctx, method, target, body)
|
||||
} else {
|
||||
req, err = http.NewRequest(method, target, body)
|
||||
}
|
||||
if err != nil {
|
||||
return Response{Err: err}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user