1
1
package client
2
2
3
- // The original library rpc.v7 logic in github.com/qiniu/x has its own bugs
4
- // under the concurrent http calls, we make a fork of the library and fix
5
- // the bug
6
3
import (
7
- // "bufio"
8
4
"bytes"
5
+ "context"
9
6
"encoding/json"
10
7
"fmt"
11
- "github.com/qiniu/api.v7/auth"
12
- "github.com/qiniu/api.v7/conf"
13
- "github.com/qiniu/x/reqid.v7"
14
- . "golang.org/x/net/context"
15
8
"io"
16
9
"io/ioutil"
17
10
"net/http"
18
11
"net/url"
19
12
"runtime"
20
13
"strings"
14
+
15
+ "github.com/qiniu/api.v7/auth"
16
+ "github.com/qiniu/api.v7/conf"
17
+ "github.com/qiniu/api.v7/reqid"
21
18
)
22
19
23
20
var UserAgent = "Golang qiniu/client package"
@@ -38,7 +35,7 @@ func SetAppName(userApp string) error {
38
35
39
36
// --------------------------------------------------------------------
40
37
41
- func newRequest (ctx Context , method , reqUrl string , headers http.Header , body io.Reader ) (req * http.Request , err error ) {
38
+ func newRequest (ctx context. Context , method , reqUrl string , headers http.Header , body io.Reader ) (req * http.Request , err error ) {
42
39
req , err = http .NewRequest (method , reqUrl , body )
43
40
if err != nil {
44
41
return
@@ -51,7 +48,7 @@ func newRequest(ctx Context, method, reqUrl string, headers http.Header, body io
51
48
req .Header = headers
52
49
53
50
//check access token
54
- mac , ok := ctx . Value ( "mac" ).( * auth.Credentials )
51
+ mac , ok := auth .CredentialsFromContext ( ctx )
55
52
if ok {
56
53
token , signErr := mac .SignRequest (req )
57
54
if signErr != nil {
@@ -64,15 +61,15 @@ func newRequest(ctx Context, method, reqUrl string, headers http.Header, body io
64
61
return
65
62
}
66
63
67
- func (r Client ) DoRequest (ctx Context , method , reqUrl string , headers http.Header ) (resp * http.Response , err error ) {
64
+ func (r Client ) DoRequest (ctx context. Context , method , reqUrl string , headers http.Header ) (resp * http.Response , err error ) {
68
65
req , err := newRequest (ctx , method , reqUrl , headers , nil )
69
66
if err != nil {
70
67
return
71
68
}
72
69
return r .Do (ctx , req )
73
70
}
74
71
75
- func (r Client ) DoRequestWith (ctx Context , method , reqUrl string , headers http.Header , body io.Reader ,
72
+ func (r Client ) DoRequestWith (ctx context. Context , method , reqUrl string , headers http.Header , body io.Reader ,
76
73
bodyLength int ) (resp * http.Response , err error ) {
77
74
78
75
req , err := newRequest (ctx , method , reqUrl , headers , body )
@@ -83,7 +80,7 @@ func (r Client) DoRequestWith(ctx Context, method, reqUrl string, headers http.H
83
80
return r .Do (ctx , req )
84
81
}
85
82
86
- func (r Client ) DoRequestWith64 (ctx Context , method , reqUrl string , headers http.Header , body io.Reader ,
83
+ func (r Client ) DoRequestWith64 (ctx context. Context , method , reqUrl string , headers http.Header , body io.Reader ,
87
84
bodyLength int64 ) (resp * http.Response , err error ) {
88
85
89
86
req , err := newRequest (ctx , method , reqUrl , headers , body )
@@ -94,7 +91,7 @@ func (r Client) DoRequestWith64(ctx Context, method, reqUrl string, headers http
94
91
return r .Do (ctx , req )
95
92
}
96
93
97
- func (r Client ) DoRequestWithForm (ctx Context , method , reqUrl string , headers http.Header ,
94
+ func (r Client ) DoRequestWithForm (ctx context. Context , method , reqUrl string , headers http.Header ,
98
95
data map [string ][]string ) (resp * http.Response , err error ) {
99
96
100
97
if headers == nil {
@@ -115,7 +112,7 @@ func (r Client) DoRequestWithForm(ctx Context, method, reqUrl string, headers ht
115
112
return r .DoRequestWith (ctx , method , reqUrl , headers , strings .NewReader (requestData ), len (requestData ))
116
113
}
117
114
118
- func (r Client ) DoRequestWithJson (ctx Context , method , reqUrl string , headers http.Header ,
115
+ func (r Client ) DoRequestWithJson (ctx context. Context , method , reqUrl string , headers http.Header ,
119
116
data interface {}) (resp * http.Response , err error ) {
120
117
121
118
reqBody , err := json .Marshal (data )
@@ -130,13 +127,13 @@ func (r Client) DoRequestWithJson(ctx Context, method, reqUrl string, headers ht
130
127
return r .DoRequestWith (ctx , method , reqUrl , headers , bytes .NewReader (reqBody ), len (reqBody ))
131
128
}
132
129
133
- func (r Client ) Do (ctx Context , req * http.Request ) (resp * http.Response , err error ) {
130
+ func (r Client ) Do (ctx context. Context , req * http.Request ) (resp * http.Response , err error ) {
134
131
135
132
if ctx == nil {
136
- ctx = Background ()
133
+ ctx = context . Background ()
137
134
}
138
135
139
- if reqId , ok := reqid .FromContext (ctx ); ok {
136
+ if reqId , ok := reqid .ReqidFromContext (ctx ); ok {
140
137
req .Header .Set ("X-Reqid" , reqId )
141
138
}
142
139
@@ -248,7 +245,7 @@ func ResponseError(resp *http.Response) (err error) {
248
245
return e
249
246
}
250
247
251
- func CallRet (ctx Context , ret interface {}, resp * http.Response ) (err error ) {
248
+ func CallRet (ctx context. Context , ret interface {}, resp * http.Response ) (err error ) {
252
249
253
250
defer func () {
254
251
io .Copy (ioutil .Discard , resp .Body )
@@ -269,7 +266,7 @@ func CallRet(ctx Context, ret interface{}, resp *http.Response) (err error) {
269
266
return ResponseError (resp )
270
267
}
271
268
272
- func (r Client ) CallWithForm (ctx Context , ret interface {}, method , reqUrl string , headers http.Header ,
269
+ func (r Client ) CallWithForm (ctx context. Context , ret interface {}, method , reqUrl string , headers http.Header ,
273
270
param map [string ][]string ) (err error ) {
274
271
275
272
resp , err := r .DoRequestWithForm (ctx , method , reqUrl , headers , param )
@@ -279,7 +276,7 @@ func (r Client) CallWithForm(ctx Context, ret interface{}, method, reqUrl string
279
276
return CallRet (ctx , ret , resp )
280
277
}
281
278
282
- func (r Client ) CallWithJson (ctx Context , ret interface {}, method , reqUrl string , headers http.Header ,
279
+ func (r Client ) CallWithJson (ctx context. Context , ret interface {}, method , reqUrl string , headers http.Header ,
283
280
param interface {}) (err error ) {
284
281
285
282
resp , err := r .DoRequestWithJson (ctx , method , reqUrl , headers , param )
@@ -289,7 +286,7 @@ func (r Client) CallWithJson(ctx Context, ret interface{}, method, reqUrl string
289
286
return CallRet (ctx , ret , resp )
290
287
}
291
288
292
- func (r Client ) CallWith (ctx Context , ret interface {}, method , reqUrl string , headers http.Header , body io.Reader ,
289
+ func (r Client ) CallWith (ctx context. Context , ret interface {}, method , reqUrl string , headers http.Header , body io.Reader ,
293
290
bodyLength int ) (err error ) {
294
291
295
292
resp , err := r .DoRequestWith (ctx , method , reqUrl , headers , body , bodyLength )
@@ -299,7 +296,7 @@ func (r Client) CallWith(ctx Context, ret interface{}, method, reqUrl string, he
299
296
return CallRet (ctx , ret , resp )
300
297
}
301
298
302
- func (r Client ) CallWith64 (ctx Context , ret interface {}, method , reqUrl string , headers http.Header , body io.Reader ,
299
+ func (r Client ) CallWith64 (ctx context. Context , ret interface {}, method , reqUrl string , headers http.Header , body io.Reader ,
303
300
bodyLength int64 ) (err error ) {
304
301
305
302
resp , err := r .DoRequestWith64 (ctx , method , reqUrl , headers , body , bodyLength )
@@ -309,7 +306,7 @@ func (r Client) CallWith64(ctx Context, ret interface{}, method, reqUrl string,
309
306
return CallRet (ctx , ret , resp )
310
307
}
311
308
312
- func (r Client ) Call (ctx Context , ret interface {}, method , reqUrl string , headers http.Header ) (err error ) {
309
+ func (r Client ) Call (ctx context. Context , ret interface {}, method , reqUrl string , headers http.Header ) (err error ) {
313
310
314
311
resp , err := r .DoRequestWith (ctx , method , reqUrl , headers , nil , 0 )
315
312
if err != nil {
0 commit comments