Skip to content

Commit

Permalink
Features/api generator (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue authored Apr 16, 2024
1 parent 9a10bde commit 6c9d7f5
Show file tree
Hide file tree
Showing 174 changed files with 15,833 additions and 1,208 deletions.
30 changes: 20 additions & 10 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ jobs:
with:
path: src/github.com/qiniu/go-sdk
ref: ${{ github.ref }}
submodules: recursive
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '1.10.x'
- name: Format
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
working-directory: src/github.com/qiniu/go-sdk
- name: Run unit cases
run: |
set -e
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
GOPATH=$GITHUB_WORKSPACE go get golang.org/x/sync/singleflight
GOPATH=$GITHUB_WORKSPACE go get github.com/qiniu/dyn
GOPATH=$GITHUB_WORKSPACE go get github.com/gofrs/flock
# FIXME special package
# github.com/go-playground/validator/v10
Expand All @@ -34,23 +33,28 @@ jobs:
# new package name don't work in non-module mode
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
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
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
# GOPATH=$GITHUB_WORKSPACE go get golang.org/x/crypto/sha3
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
# GOPATH=$GITHUB_WORKSPACE go get golang.org/x/text/language
GOPATH=$GITHUB_WORKSPACE go get github.com/leodido/go-urn
GOPATH=$GITHUB_WORKSPACE go get github.com/go-playground/locales
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
# GOPATH=$GITHUB_WORKSPACE go get github.com/dave/jennifer
GOPATH=$GITHUB_WORKSPACE go get github.com/iancoleman/strcase
# github.com/stretchr/testify
# newer version require newer go feature
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
GOPATH=$GITHUB_WORKSPACE go get github.com/davecgh/go-spew/spew
GOPATH=$GITHUB_WORKSPACE go get github.com/pmezard/go-difflib/difflib
GOPATH=$GITHUB_WORKSPACE go get gopkg.in/yaml.v3
GOPATH=$GITHUB_WORKSPACE make unittest
working-directory: src/github.com/qiniu/go-sdk
go-mod-test:
Expand All @@ -59,7 +63,7 @@ jobs:
fail-fast: false
max-parallel: 1
matrix:
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']
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']
runs-on: ubuntu-latest
permissions:
actions: read
Expand All @@ -70,15 +74,21 @@ jobs:
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
submodules: recursive
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go_version }}
- name: Format
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
run: |
if [ "${{ matrix.go_version }}" = "1.21.x" ]; then
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
exit 1
fi
fi
- name: Golint
run: |
if [ "${{ matrix.go_version }}" = "1.20.x" ]; then
if [ "${{ matrix.go_version }}" = "1.21.x" ]; then
set -e
go install honnef.co/go/tools/cmd/staticcheck@latest
make staticcheck
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "api-specs"]
path = api-specs
url = https://github.com/qiniu/api-specs.git
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ unittest:

staticcheck:
staticcheck -go 1.10 `go list ./... | egrep -v 'examples|sms'`

generate:
go generate ./storagev2/
1 change: 1 addition & 0 deletions api-specs
Submodule api-specs added at a95755
13 changes: 9 additions & 4 deletions auth/credentials.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package auth

import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/textproto"
"sort"
"strings"

api "github.com/qiniu/go-sdk/v7"
"github.com/qiniu/go-sdk/v7/conf"
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
)

const (
Expand Down Expand Up @@ -94,7 +94,7 @@ func collectData(req *http.Request) (data []byte, err error) {
err = rErr
return
}
req.Body = ioutil.NopCloser(bytes.NewReader(s2))
req.Body = internal_io.NewBytesNopCloser(s2)
data = append(data, s2...)
}
return
Expand Down Expand Up @@ -173,7 +173,7 @@ func collectDataV2(req *http.Request) (data []byte, err error) {
err = rErr
return
}
req.Body = ioutil.NopCloser(bytes.NewReader(s2))
req.Body = internal_io.NewBytesNopCloser(s2)
data = append(data, s2...)
}
return
Expand Down Expand Up @@ -232,3 +232,8 @@ func (ath *Credentials) VerifyCallback(req *http.Request) (bool, error) {
return auth == AuthorizationPrefixQBox+token, nil
}
}

// Get 实现 CredentialsProvider 接口
func (c *Credentials) Get(ctx context.Context) (*Credentials, error) {
return c, nil
}
37 changes: 22 additions & 15 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import (

"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/conf"
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
"github.com/qiniu/go-sdk/v7/internal/log"
"github.com/qiniu/go-sdk/v7/reqid"
)

var UserAgent = getUserAgentWithAppName("default")
var DefaultClient = Client{&http.Client{Transport: http.DefaultTransport}}
var DefaultClient = Client{
&http.Client{
Transport: http.DefaultTransport,
},
}

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

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

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

type ErrorInfo struct {
Err string `json:"error,omitempty"`
Key string `json:"key,omitempty"`
Reqid string `json:"reqid,omitempty"`
Errno int `json:"errno,omitempty"`
Code int `json:"code"`
Err string `json:"error,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Key string `json:"key,omitempty"`
Reqid string `json:"reqid,omitempty"`
Errno int `json:"errno,omitempty"`
Code int `json:"code"`
}

func (r *ErrorInfo) ErrorDetail() string {
Expand Down Expand Up @@ -226,20 +232,21 @@ func (r *ErrorInfo) HttpCode() int {

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

body, err1 := ioutil.ReadAll(r)
body, err1 := internal_io.ReadAll(r)
if err1 != nil {
e.Err = err1.Error()
return
}

var ret struct {
Err string `json:"error"`
Key string `json:"key"`
Errno int `json:"errno"`
Err string `json:"error"`
Key string `json:"key"`
Errno int `json:"errno"`
ErrorCode string `json:"error_code,omitempty"`
}
if decodeJsonFromData(body, &ret) == nil && ret.Err != "" {
// qiniu error msg style returns here
e.Err, e.Key, e.Errno = ret.Err, ret.Key, ret.Errno
e.Err, e.Key, e.Errno, e.ErrorCode = ret.Err, ret.Key, ret.Errno, ret.ErrorCode
return
}
e.Err = string(body)
Expand All @@ -264,7 +271,7 @@ func ResponseError(resp *http.Response) error {
if ok && strings.HasPrefix(ct[0], "application/json") {
parseError(e, resp.Body)
} else {
bs, err := ioutil.ReadAll(resp.Body)
bs, err := internal_io.ReadAll(resp.Body)
if err != nil {
e.Err = fmt.Sprintf("failed to read from response body: %s", err)
} else {
Expand All @@ -279,7 +286,7 @@ func ResponseError(resp *http.Response) error {
func CallRet(ctx context.Context, ret interface{}, resp *http.Response) (err error) {

defer func() {
io.Copy(ioutil.Discard, resp.Body)
_ = internal_io.SinkAll(resp.Body)
resp.Body.Close()
}()

Expand Down
4 changes: 3 additions & 1 deletion examples/resume_upload_advanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"sync"

"context"

"github.com/qiniu/go-sdk/v7/auth"
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
"github.com/qiniu/go-sdk/v7/storage"
)

Expand Down Expand Up @@ -76,7 +78,7 @@ func main() {
// 尝试从旧的进度文件中读取进度
recordFp, openErr := os.Open(recordPath)
if openErr == nil {
progressBytes, readErr := ioutil.ReadAll(recordFp)
progressBytes, readErr := internal_io.ReadAll(recordFp)
if readErr == nil {
mErr := json.Unmarshal(progressBytes, &progressRecord)
if mErr == nil {
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ module github.com/qiniu/go-sdk/v7
go 1.14

require (
github.com/dave/jennifer v1.6.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.8.0
github.com/gofrs/flock v0.8.1
github.com/iancoleman/strcase v0.3.0
github.com/kr/pretty v0.3.0 // indirect
github.com/qiniu/dyn v1.3.0
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/sys v0.12.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
9 changes: 8 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/dave/jennifer v1.6.1 h1:T4T/67t6RAA5AIV6+NP8Uk/BIsXgDoqEowgycdQQLuk=
github.com/dave/jennifer v1.6.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -12,6 +14,10 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.8.0 h1:1kAa0fCrnpv+QYdkdcRzrRM7AyYs5o8+jZdJCz9xj6k=
github.com/go-playground/validator/v10 v10.8.0/go.mod h1:9JhgTzTaE31GZDpH/HSvHiRJrJ3iKAgqqH0Bl/Ocjdk=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
Expand Down Expand Up @@ -55,8 +61,9 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
Loading

0 comments on commit 6c9d7f5

Please sign in to comment.