Skip to content

Commit 6c9d7f5

Browse files
authored
Features/api generator (#122)
1 parent 9a10bde commit 6c9d7f5

File tree

174 files changed

+15833
-1208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+15833
-1208
lines changed

.github/workflows/ci-test.yml

+20-10
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ jobs:
1313
with:
1414
path: src/github.com/qiniu/go-sdk
1515
ref: ${{ github.ref }}
16+
submodules: recursive
1617
- name: Install Go
1718
uses: actions/setup-go@v2
1819
with:
1920
go-version: '1.10.x'
20-
- name: Format
21-
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
22-
working-directory: src/github.com/qiniu/go-sdk
2321
- name: Run unit cases
2422
run: |
2523
set -e
2624
rm -rf $GITHUB_WORKSPACE/src/github.com/qiniu/x && git clone -b v1.12.21 --depth 1 https://github.com/qiniu/x.git $GITHUB_WORKSPACE/src/github.com/qiniu/x
2725
GOPATH=$GITHUB_WORKSPACE go get golang.org/x/sync/singleflight
2826
GOPATH=$GITHUB_WORKSPACE go get github.com/qiniu/dyn
27+
GOPATH=$GITHUB_WORKSPACE go get github.com/gofrs/flock
2928
3029
# FIXME special package
3130
# github.com/go-playground/validator/v10
@@ -34,23 +33,28 @@ jobs:
3433
# new package name don't work in non-module mode
3534
rm -rf $GITHUB_WORKSPACE/src/github.com/go-playground/validator/v10 && git clone -b v10.9.0 --depth 1 https://github.com/go-playground/validator.git $GITHUB_WORKSPACE/src/github.com/go-playground/validator/v10
3635
rm -rf $GITHUB_WORKSPACE/src/github.com/universal-translator && git clone -b v0.18.0 --depth 1 https://github.com/go-playground/universal-translator.git $GITHUB_WORKSPACE/src/github.com/go-playground/universal-translator
37-
36+
3837
rm -rf $GITHUB_WORKSPACE/src/golang.org/x/crypto && git clone -b v0.10.0 --depth 1 https://go.googlesource.com/crypto $GITHUB_WORKSPACE/src/golang.org/x/crypto
3938
# GOPATH=$GITHUB_WORKSPACE go get golang.org/x/crypto/sha3
40-
39+
4140
rm -rf $GITHUB_WORKSPACE/src/golang.org/x/text && git clone -b v0.10.0 --depth 1 https://github.com/golang/text $GITHUB_WORKSPACE/src/golang.org/x/text
4241
# GOPATH=$GITHUB_WORKSPACE go get golang.org/x/text/language
43-
42+
4443
GOPATH=$GITHUB_WORKSPACE go get github.com/leodido/go-urn
4544
GOPATH=$GITHUB_WORKSPACE go get github.com/go-playground/locales
4645
46+
rm -rf $GITHUB_WORKSPACE/src/github.com/dave/jennifer && git clone -b v1.6.1 --depth 1 https://github.com/dave/jennifer $GITHUB_WORKSPACE/src/github.com/dave/jennifer
47+
# GOPATH=$GITHUB_WORKSPACE go get github.com/dave/jennifer
48+
49+
GOPATH=$GITHUB_WORKSPACE go get github.com/iancoleman/strcase
50+
4751
# github.com/stretchr/testify
4852
# newer version require newer go feature
4953
rm -rf $GITHUB_WORKSPACE/src/github.com/stretchr/testify && git clone -b v1.6.1 --depth 1 https://github.com/stretchr/testify.git $GITHUB_WORKSPACE/src/github.com/stretchr/testify
5054
GOPATH=$GITHUB_WORKSPACE go get github.com/davecgh/go-spew/spew
5155
GOPATH=$GITHUB_WORKSPACE go get github.com/pmezard/go-difflib/difflib
5256
GOPATH=$GITHUB_WORKSPACE go get gopkg.in/yaml.v3
53-
57+
5458
GOPATH=$GITHUB_WORKSPACE make unittest
5559
working-directory: src/github.com/qiniu/go-sdk
5660
go-mod-test:
@@ -59,7 +63,7 @@ jobs:
5963
fail-fast: false
6064
max-parallel: 1
6165
matrix:
62-
go_version: ['1.11.x', '1.12.x', '1.13.x', '1.14.x', '1.15.x', '1.16.x', '1.17.x', '1.18.x', '1.19.x', '1.20.x']
66+
go_version: ['1.11.x', '1.12.x', '1.13.x', '1.14.x', '1.15.x', '1.16.x', '1.17.x', '1.18.x', '1.19.x', '1.20.x', '1.21.x']
6367
runs-on: ubuntu-latest
6468
permissions:
6569
actions: read
@@ -70,15 +74,21 @@ jobs:
7074
uses: actions/checkout@v2
7175
with:
7276
ref: ${{ github.ref }}
77+
submodules: recursive
7378
- name: Install Go
7479
uses: actions/setup-go@v2
7580
with:
7681
go-version: ${{ matrix.go_version }}
7782
- name: Format
78-
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
83+
run: |
84+
if [ "${{ matrix.go_version }}" = "1.21.x" ]; then
85+
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
86+
exit 1
87+
fi
88+
fi
7989
- name: Golint
8090
run: |
81-
if [ "${{ matrix.go_version }}" = "1.20.x" ]; then
91+
if [ "${{ matrix.go_version }}" = "1.21.x" ]; then
8292
set -e
8393
go install honnef.co/go/tools/cmd/staticcheck@latest
8494
make staticcheck

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "api-specs"]
2+
path = api-specs
3+
url = https://github.com/qiniu/api-specs.git

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ unittest:
66

77
staticcheck:
88
staticcheck -go 1.10 `go list ./... | egrep -v 'examples|sms'`
9+
10+
generate:
11+
go generate ./storagev2/

api-specs

Submodule api-specs added at a957551

auth/credentials.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package auth
22

33
import (
4-
"bytes"
4+
"context"
55
"crypto/hmac"
66
"crypto/sha1"
77
"encoding/base64"
88
"fmt"
9-
"io/ioutil"
109
"net/http"
1110
"net/textproto"
1211
"sort"
1312
"strings"
1413

1514
api "github.com/qiniu/go-sdk/v7"
1615
"github.com/qiniu/go-sdk/v7/conf"
16+
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
1717
)
1818

1919
const (
@@ -94,7 +94,7 @@ func collectData(req *http.Request) (data []byte, err error) {
9494
err = rErr
9595
return
9696
}
97-
req.Body = ioutil.NopCloser(bytes.NewReader(s2))
97+
req.Body = internal_io.NewBytesNopCloser(s2)
9898
data = append(data, s2...)
9999
}
100100
return
@@ -173,7 +173,7 @@ func collectDataV2(req *http.Request) (data []byte, err error) {
173173
err = rErr
174174
return
175175
}
176-
req.Body = ioutil.NopCloser(bytes.NewReader(s2))
176+
req.Body = internal_io.NewBytesNopCloser(s2)
177177
data = append(data, s2...)
178178
}
179179
return
@@ -232,3 +232,8 @@ func (ath *Credentials) VerifyCallback(req *http.Request) (bool, error) {
232232
return auth == AuthorizationPrefixQBox+token, nil
233233
}
234234
}
235+
236+
// Get 实现 CredentialsProvider 接口
237+
func (c *Credentials) Get(ctx context.Context) (*Credentials, error) {
238+
return c, nil
239+
}

client/client.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ import (
1616

1717
"github.com/qiniu/go-sdk/v7/auth"
1818
"github.com/qiniu/go-sdk/v7/conf"
19+
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
1920
"github.com/qiniu/go-sdk/v7/internal/log"
2021
"github.com/qiniu/go-sdk/v7/reqid"
2122
)
2223

2324
var UserAgent = getUserAgentWithAppName("default")
24-
var DefaultClient = Client{&http.Client{Transport: http.DefaultTransport}}
25+
var DefaultClient = Client{
26+
&http.Client{
27+
Transport: http.DefaultTransport,
28+
},
29+
}
2530

2631
// 用来打印调试信息
2732
var DebugMode = false
@@ -144,7 +149,7 @@ func (r Client) DoRequestWithForm(ctx context.Context, method, reqUrl string, he
144149
if headers == nil {
145150
headers = http.Header{}
146151
}
147-
headers.Add("Content-Type", "application/x-www-form-urlencoded")
152+
headers.Set("Content-Type", conf.CONTENT_TYPE_FORM)
148153

149154
requestData := url.Values(data).Encode()
150155
if method == "GET" || method == "HEAD" || method == "DELETE" {
@@ -170,7 +175,7 @@ func (r Client) DoRequestWithJson(ctx context.Context, method, reqUrl string, he
170175
if headers == nil {
171176
headers = http.Header{}
172177
}
173-
headers.Add("Content-Type", "application/json")
178+
headers.Set("Content-Type", conf.CONTENT_TYPE_JSON)
174179
return r.DoRequestWith(ctx, method, reqUrl, headers, bytes.NewReader(reqBody), len(reqBody))
175180
}
176181

@@ -194,11 +199,12 @@ func (r Client) Do(ctx context.Context, req *http.Request) (resp *http.Response,
194199
// --------------------------------------------------------------------
195200

196201
type ErrorInfo struct {
197-
Err string `json:"error,omitempty"`
198-
Key string `json:"key,omitempty"`
199-
Reqid string `json:"reqid,omitempty"`
200-
Errno int `json:"errno,omitempty"`
201-
Code int `json:"code"`
202+
Err string `json:"error,omitempty"`
203+
ErrorCode string `json:"error_code,omitempty"`
204+
Key string `json:"key,omitempty"`
205+
Reqid string `json:"reqid,omitempty"`
206+
Errno int `json:"errno,omitempty"`
207+
Code int `json:"code"`
202208
}
203209

204210
func (r *ErrorInfo) ErrorDetail() string {
@@ -226,20 +232,21 @@ func (r *ErrorInfo) HttpCode() int {
226232

227233
func parseError(e *ErrorInfo, r io.Reader) {
228234

229-
body, err1 := ioutil.ReadAll(r)
235+
body, err1 := internal_io.ReadAll(r)
230236
if err1 != nil {
231237
e.Err = err1.Error()
232238
return
233239
}
234240

235241
var ret struct {
236-
Err string `json:"error"`
237-
Key string `json:"key"`
238-
Errno int `json:"errno"`
242+
Err string `json:"error"`
243+
Key string `json:"key"`
244+
Errno int `json:"errno"`
245+
ErrorCode string `json:"error_code,omitempty"`
239246
}
240247
if decodeJsonFromData(body, &ret) == nil && ret.Err != "" {
241248
// qiniu error msg style returns here
242-
e.Err, e.Key, e.Errno = ret.Err, ret.Key, ret.Errno
249+
e.Err, e.Key, e.Errno, e.ErrorCode = ret.Err, ret.Key, ret.Errno, ret.ErrorCode
243250
return
244251
}
245252
e.Err = string(body)
@@ -264,7 +271,7 @@ func ResponseError(resp *http.Response) error {
264271
if ok && strings.HasPrefix(ct[0], "application/json") {
265272
parseError(e, resp.Body)
266273
} else {
267-
bs, err := ioutil.ReadAll(resp.Body)
274+
bs, err := internal_io.ReadAll(resp.Body)
268275
if err != nil {
269276
e.Err = fmt.Sprintf("failed to read from response body: %s", err)
270277
} else {
@@ -279,7 +286,7 @@ func ResponseError(resp *http.Response) error {
279286
func CallRet(ctx context.Context, ret interface{}, resp *http.Response) (err error) {
280287

281288
defer func() {
282-
io.Copy(ioutil.Discard, resp.Body)
289+
_ = internal_io.SinkAll(resp.Body)
283290
resp.Body.Close()
284291
}()
285292

examples/resume_upload_advanced.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"sync"
1212

1313
"context"
14+
1415
"github.com/qiniu/go-sdk/v7/auth"
16+
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
1517
"github.com/qiniu/go-sdk/v7/storage"
1618
)
1719

@@ -76,7 +78,7 @@ func main() {
7678
// 尝试从旧的进度文件中读取进度
7779
recordFp, openErr := os.Open(recordPath)
7880
if openErr == nil {
79-
progressBytes, readErr := ioutil.ReadAll(recordFp)
81+
progressBytes, readErr := internal_io.ReadAll(recordFp)
8082
if readErr == nil {
8183
mErr := json.Unmarshal(progressBytes, &progressRecord)
8284
if mErr == nil {

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ module github.com/qiniu/go-sdk/v7
33
go 1.14
44

55
require (
6+
github.com/dave/jennifer v1.6.1
67
github.com/davecgh/go-spew v1.1.1 // indirect
78
github.com/go-playground/universal-translator v0.18.0 // indirect
89
github.com/go-playground/validator/v10 v10.8.0
10+
github.com/gofrs/flock v0.8.1
11+
github.com/iancoleman/strcase v0.3.0
912
github.com/kr/pretty v0.3.0 // indirect
1013
github.com/qiniu/dyn v1.3.0
1114
github.com/rogpeppe/go-internal v1.8.0 // indirect
1215
github.com/stretchr/testify v1.6.1
1316
golang.org/x/crypto v0.1.0 // indirect
1417
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
18+
golang.org/x/sys v0.12.0 // indirect
1519
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
16-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
20+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
1721
)

go.sum

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/dave/jennifer v1.6.1 h1:T4T/67t6RAA5AIV6+NP8Uk/BIsXgDoqEowgycdQQLuk=
3+
github.com/dave/jennifer v1.6.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
24
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
46
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -12,6 +14,10 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
1214
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
1315
github.com/go-playground/validator/v10 v10.8.0 h1:1kAa0fCrnpv+QYdkdcRzrRM7AyYs5o8+jZdJCz9xj6k=
1416
github.com/go-playground/validator/v10 v10.8.0/go.mod h1:9JhgTzTaE31GZDpH/HSvHiRJrJ3iKAgqqH0Bl/Ocjdk=
17+
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
18+
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
19+
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
20+
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
1521
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1622
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1723
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -55,8 +61,9 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
5561
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5662
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5763
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
58-
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
5964
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
65+
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
66+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6067
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
6168
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
6269
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

0 commit comments

Comments
 (0)