Skip to content

Commit 1a6b89b

Browse files
committed
移除qiniu/x依赖, authorization -> credentials
1 parent ed91f23 commit 1a6b89b

File tree

15 files changed

+237
-62
lines changed

15 files changed

+237
-62
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ test:
44
go test -v ./cdn/...
55
go test -v ./storage/...
66
go test -v ./rtc/...
7+
go test -v ./internal/...
78

89

910
unittest:
1011
go test -v ./auth/...
1112
go test -v ./conf/...
13+
go test -v ./internal/...

auth/context.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
)
6+
7+
// MacContextKey 是用户的密钥信息
8+
// context.Context中的键值不应该使用普通的字符串, 有可能导致命名冲突
9+
type macContextKey struct{}
10+
11+
// WithCredentials 返回一个包含密钥信息的context
12+
func WithCredentials(ctx context.Context, cred *Credentials) context.Context {
13+
if ctx == nil {
14+
ctx = context.Background()
15+
}
16+
return context.WithValue(ctx, macContextKey{}, cred)
17+
}
18+
19+
// CredentialsFromContext 从context获取密钥信息
20+
func CredentialsFromContext(ctx context.Context) (cred *Credentials, ok bool) {
21+
cred, ok = ctx.Value(macContextKey{}).(*Credentials)
22+
return
23+
}

auth/authorization.go renamed to auth/credentials.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package auth
22

33
import (
4+
"bytes"
45
"crypto/hmac"
56
"crypto/sha1"
67
"encoding/base64"
78
"fmt"
9+
"io/ioutil"
810
"net/http"
911

12+
"github.com/qiniu/api.v7"
1013
"github.com/qiniu/api.v7/conf"
11-
"github.com/qiniu/x/bytes.v7/seekable"
1214
)
1315

1416
// 七牛鉴权类,用于生成Qbox, Qiniu, Upload签名
@@ -50,12 +52,13 @@ func collectData(req *http.Request) (data []byte, err error) {
5052

5153
data = []byte(s)
5254
if incBody(req) {
53-
s2, err2 := seekable.New(req)
54-
if err2 != nil {
55-
err = err2
55+
s2, rErr := api.BytesFromRequest(req)
56+
if rErr != nil {
57+
err = rErr
5658
return
5759
}
58-
data = append(data, s2.Bytes()...)
60+
req.Body = ioutil.NopCloser(bytes.NewReader(s2))
61+
data = append(data, s2...)
5962
}
6063
return
6164
}
@@ -85,12 +88,13 @@ func collectDataV2(req *http.Request) (data []byte, err error) {
8588
data = []byte(s)
8689
//write body
8790
if incBodyV2(req) {
88-
s2, err2 := seekable.New(req)
89-
if err2 != nil {
90-
err = err2
91+
s2, rErr := api.BytesFromRequest(req)
92+
if rErr != nil {
93+
err = rErr
9194
return
9295
}
93-
data = append(data, s2.Bytes()...)
96+
req.Body = ioutil.NopCloser(bytes.NewReader(s2))
97+
data = append(data, s2...)
9498
}
9599
return
96100
}
File renamed without changes.

client/client.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package client
22

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
63
import (
7-
// "bufio"
84
"bytes"
5+
"context"
96
"encoding/json"
107
"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"
158
"io"
169
"io/ioutil"
1710
"net/http"
1811
"net/url"
1912
"runtime"
2013
"strings"
14+
15+
"github.com/qiniu/api.v7/auth"
16+
"github.com/qiniu/api.v7/conf"
17+
"github.com/qiniu/api.v7/reqid"
2118
)
2219

2320
var UserAgent = "Golang qiniu/client package"
@@ -38,7 +35,7 @@ func SetAppName(userApp string) error {
3835

3936
// --------------------------------------------------------------------
4037

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) {
4239
req, err = http.NewRequest(method, reqUrl, body)
4340
if err != nil {
4441
return
@@ -51,7 +48,7 @@ func newRequest(ctx Context, method, reqUrl string, headers http.Header, body io
5148
req.Header = headers
5249

5350
//check access token
54-
mac, ok := ctx.Value("mac").(*auth.Credentials)
51+
mac, ok := auth.CredentialsFromContext(ctx)
5552
if ok {
5653
token, signErr := mac.SignRequest(req)
5754
if signErr != nil {
@@ -64,15 +61,15 @@ func newRequest(ctx Context, method, reqUrl string, headers http.Header, body io
6461
return
6562
}
6663

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) {
6865
req, err := newRequest(ctx, method, reqUrl, headers, nil)
6966
if err != nil {
7067
return
7168
}
7269
return r.Do(ctx, req)
7370
}
7471

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,
7673
bodyLength int) (resp *http.Response, err error) {
7774

7875
req, err := newRequest(ctx, method, reqUrl, headers, body)
@@ -83,7 +80,7 @@ func (r Client) DoRequestWith(ctx Context, method, reqUrl string, headers http.H
8380
return r.Do(ctx, req)
8481
}
8582

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,
8784
bodyLength int64) (resp *http.Response, err error) {
8885

8986
req, err := newRequest(ctx, method, reqUrl, headers, body)
@@ -94,7 +91,7 @@ func (r Client) DoRequestWith64(ctx Context, method, reqUrl string, headers http
9491
return r.Do(ctx, req)
9592
}
9693

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,
9895
data map[string][]string) (resp *http.Response, err error) {
9996

10097
if headers == nil {
@@ -115,7 +112,7 @@ func (r Client) DoRequestWithForm(ctx Context, method, reqUrl string, headers ht
115112
return r.DoRequestWith(ctx, method, reqUrl, headers, strings.NewReader(requestData), len(requestData))
116113
}
117114

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,
119116
data interface{}) (resp *http.Response, err error) {
120117

121118
reqBody, err := json.Marshal(data)
@@ -130,13 +127,13 @@ func (r Client) DoRequestWithJson(ctx Context, method, reqUrl string, headers ht
130127
return r.DoRequestWith(ctx, method, reqUrl, headers, bytes.NewReader(reqBody), len(reqBody))
131128
}
132129

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) {
134131

135132
if ctx == nil {
136-
ctx = Background()
133+
ctx = context.Background()
137134
}
138135

139-
if reqId, ok := reqid.FromContext(ctx); ok {
136+
if reqId, ok := reqid.ReqidFromContext(ctx); ok {
140137
req.Header.Set("X-Reqid", reqId)
141138
}
142139

@@ -248,7 +245,7 @@ func ResponseError(resp *http.Response) (err error) {
248245
return e
249246
}
250247

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) {
252249

253250
defer func() {
254251
io.Copy(ioutil.Discard, resp.Body)
@@ -269,7 +266,7 @@ func CallRet(ctx Context, ret interface{}, resp *http.Response) (err error) {
269266
return ResponseError(resp)
270267
}
271268

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,
273270
param map[string][]string) (err error) {
274271

275272
resp, err := r.DoRequestWithForm(ctx, method, reqUrl, headers, param)
@@ -279,7 +276,7 @@ func (r Client) CallWithForm(ctx Context, ret interface{}, method, reqUrl string
279276
return CallRet(ctx, ret, resp)
280277
}
281278

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,
283280
param interface{}) (err error) {
284281

285282
resp, err := r.DoRequestWithJson(ctx, method, reqUrl, headers, param)
@@ -289,7 +286,7 @@ func (r Client) CallWithJson(ctx Context, ret interface{}, method, reqUrl string
289286
return CallRet(ctx, ret, resp)
290287
}
291288

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,
293290
bodyLength int) (err error) {
294291

295292
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
299296
return CallRet(ctx, ret, resp)
300297
}
301298

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,
303300
bodyLength int64) (err error) {
304301

305302
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,
309306
return CallRet(ctx, ret, resp)
310307
}
311308

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) {
313310

314311
resp, err := r.DoRequestWith(ctx, method, reqUrl, headers, nil, 0)
315312
if err != nil {

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/qiniu/api.v7
2+
3+
require (
4+
github.com/qiniu/x v7.0.8+incompatible
5+
github.com/stretchr/testify v1.3.0 // indirect
6+
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 // indirect
7+
qiniupkg.com/x v7.0.8+incompatible // indirect
8+
)

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/qiniu/x v7.0.8+incompatible h1:P4LASsfwJY7SoZ13dwqBwGhZh7HKU8cdFVCUkmz0gZ8=
6+
github.com/qiniu/x v7.0.8+incompatible/go.mod h1:KpRKWYG/GaidPQVpoQ2Cvuvtts3gYnoo2PftgdmAiU4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
9+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
10+
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 h1:fY7Dsw114eJN4boqzVSbpVHO6rTdhq6/GnXeu+PKnzU=
11+
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
12+
qiniupkg.com/x v7.0.8+incompatible h1:Ek0ZVi5IyaWUAFkJbPRiqlh34xDM4uoKw7KqdpankvU=
13+
qiniupkg.com/x v7.0.8+incompatible/go.mod h1:6sLxR5IZ03vMaRAQAY/5MvzofeoBIjO4XE0Njv6V1ms=

internal/log/logger.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Package log只是SDK本身自己使用,用来调试代码使用,比如输出HTTP请求和响应信息
2+
package log
3+
4+
import (
5+
"io"
6+
"log"
7+
"os"
8+
)
9+
10+
type Logger struct {
11+
*log.Logger
12+
level LogLevel
13+
}
14+
15+
// New 返回一个Logger 指针
16+
func New(out io.Writer, prefix string, flag int, level LogLevel) *Logger {
17+
return &Logger{
18+
Logger: log.New(out, prefix, flag),
19+
level: level,
20+
}
21+
}
22+
23+
var std = New(os.Stdout, InfoPrefix, log.LstdFlags, LogInfo)
24+
25+
type LogLevel int
26+
27+
const (
28+
// LogDebug 调试模式
29+
LogDebug LogLevel = iota
30+
31+
// Info
32+
LogInfo
33+
34+
// Warn
35+
LogWarn
36+
)
37+
38+
const (
39+
InfoPrefix = "[I] "
40+
DebugPrefix = "[D] "
41+
WarnPrefix = "[W] "
42+
)
43+
44+
func (l *Logger) Info(v ...interface{}) {
45+
l.output(LogInfo, v...)
46+
}
47+
48+
func (l *Logger) output(level LogLevel, v ...interface{}) {
49+
if l.level <= level {
50+
l.Logger.Println(v...)
51+
}
52+
}
53+
54+
func (l *Logger) Debug(v ...interface{}) {
55+
l.output(LogDebug, v...)
56+
}
57+
58+
func (l *Logger) Warn(v ...interface{}) {
59+
l.output(LogWarn, v...)
60+
}
61+
62+
func Info(v ...interface{}) {
63+
std.Info(v...)
64+
}
65+
66+
func Debug(v ...interface{}) {
67+
std.Debug(v...)
68+
}
69+
70+
func Warn(v ...interface{}) {
71+
std.Warn(v...)
72+
}

internal/log/logger_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package log
2+
3+
import (
4+
"bytes"
5+
"log"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestLogger(t *testing.T) {
11+
b := bytes.Buffer{}
12+
logger := New(&b, InfoPrefix, log.LstdFlags, LogInfo)
13+
14+
logger.Info("hello world")
15+
16+
splits := strings.Split(b.String(), " ")
17+
if splits[0] != strings.Trim(InfoPrefix, " ") {
18+
t.Errorf("got prefix: %q, want: %q\n", splits[0], InfoPrefix)
19+
}
20+
}

reqid/reqid.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package reqid
2+
3+
import (
4+
"context"
5+
)
6+
7+
type reqidKey struct{}
8+
9+
// WithReqid 把reqid加入context中
10+
func WithReqid(ctx context.Context, reqid string) context.Context {
11+
return context.WithValue(ctx, reqidKey{}, reqid)
12+
}
13+
14+
// ReqidFromContext 从context中获取reqid
15+
func ReqidFromContext(ctx context.Context) (reqid string, ok bool) {
16+
reqid, ok = ctx.Value(reqidKey{}).(string)
17+
return
18+
}
19+
20+
// --------------------------------------------------------------------

0 commit comments

Comments
 (0)