Feat: cancel archive action / request with context

This commit is contained in:
HFO4
2020-01-02 15:36:13 +08:00
parent 1393659668
commit aeca161186
4 changed files with 58 additions and 5 deletions

View File

@@ -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}
}