diff options
author | dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> | 2022-01-28 23:48:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-28 23:48:40 +0100 |
commit | 5a1fd7daddbb07b93e3611d023ea48e1991fc0c6 (patch) | |
tree | e592a99163702c4c6214c9519a7983fa58fab13c /vendor/github.com/SevereCloud/vksdk/v2/api/api.go | |
parent | ac06a268096285a9f795bd19df950075a930c9d6 (diff) | |
download | matterbridge-msglm-5a1fd7daddbb07b93e3611d023ea48e1991fc0c6.tar.gz matterbridge-msglm-5a1fd7daddbb07b93e3611d023ea48e1991fc0c6.tar.bz2 matterbridge-msglm-5a1fd7daddbb07b93e3611d023ea48e1991fc0c6.zip |
Bump github.com/SevereCloud/vksdk/v2 from 2.11.0 to 2.13.0 (#1698)
Bumps [github.com/SevereCloud/vksdk/v2](https://github.com/SevereCloud/vksdk) from 2.11.0 to 2.13.0.
- [Release notes](https://github.com/SevereCloud/vksdk/releases)
- [Commits](https://github.com/SevereCloud/vksdk/compare/v2.11.0...v2.13.0)
---
updated-dependencies:
- dependency-name: github.com/SevereCloud/vksdk/v2
dependency-type: direct:production
update-type: version-update:semver-minor
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/SevereCloud/vksdk/v2/api/api.go')
-rw-r--r-- | vendor/github.com/SevereCloud/vksdk/v2/api/api.go | 92 |
1 files changed, 78 insertions, 14 deletions
diff --git a/vendor/github.com/SevereCloud/vksdk/v2/api/api.go b/vendor/github.com/SevereCloud/vksdk/v2/api/api.go index 56da7784..760bc7e0 100644 --- a/vendor/github.com/SevereCloud/vksdk/v2/api/api.go +++ b/vendor/github.com/SevereCloud/vksdk/v2/api/api.go @@ -7,9 +7,11 @@ package api // import "github.com/SevereCloud/vksdk/v2/api" import ( "bytes" + "compress/gzip" "context" "encoding/json" "fmt" + "io" "mime" "net/http" "net/url" @@ -21,6 +23,8 @@ import ( "github.com/SevereCloud/vksdk/v2" "github.com/SevereCloud/vksdk/v2/internal" "github.com/SevereCloud/vksdk/v2/object" + "github.com/klauspost/compress/zstd" + "github.com/vmihailenco/msgpack/v5" ) // Api constants. @@ -91,6 +95,9 @@ type VK struct { UserAgent string Handler func(method string, params ...Params) (Response, error) + msgpack bool + zstd bool + mux sync.Mutex lastTime time.Time rps int @@ -98,9 +105,9 @@ type VK struct { // Response struct. type Response struct { - Response json.RawMessage `json:"response"` - Error Error `json:"error"` - ExecuteErrors ExecuteErrors `json:"execute_errors"` + Response object.RawMessage `json:"response"` + Error Error `json:"error"` + ExecuteErrors ExecuteErrors `json:"execute_errors"` } // NewVK returns a new VK. @@ -121,7 +128,7 @@ func NewVK(tokens ...string) *VK { vk.accessTokens = tokens vk.Version = Version - vk.Handler = vk.defaultHandler + vk.Handler = vk.DefaultHandler vk.MethodURL = MethodURL vk.Client = http.DefaultClient @@ -207,8 +214,8 @@ func buildQuery(sliceParams ...Params) (context.Context, url.Values) { return ctx, query } -// defaultHandler provides access to VK API methods. -func (vk *VK) defaultHandler(method string, sliceParams ...Params) (Response, error) { +// DefaultHandler provides access to VK API methods. +func (vk *VK) DefaultHandler(method string, sliceParams ...Params) (Response, error) { u := vk.MethodURL + method ctx, query := buildQuery(sliceParams...) attempt := 0 @@ -243,24 +250,52 @@ func (vk *VK) defaultHandler(method string, sliceParams ...Params) (Response, er return response, err } + acceptEncoding := "gzip" + if vk.zstd { + acceptEncoding = "zstd" + } + req.Header.Set("User-Agent", vk.UserAgent) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Accept-Encoding", acceptEncoding) + + var reader io.Reader + resp, err := vk.Client.Do(req) if err != nil { return response, err } - mediatype, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type")) - if mediatype != "application/json" { - _ = resp.Body.Close() - return response, &InvalidContentType{mediatype} + switch resp.Header.Get("Content-Encoding") { + case "zstd": + reader, _ = zstd.NewReader(resp.Body) + case "gzip": + reader, _ = gzip.NewReader(resp.Body) + default: + reader = resp.Body } - err = json.NewDecoder(resp.Body).Decode(&response) - if err != nil { + mediatype, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type")) + switch mediatype { + case "application/json": + err = json.NewDecoder(reader).Decode(&response) + if err != nil { + _ = resp.Body.Close() + return response, err + } + case "application/x-msgpack": + dec := msgpack.NewDecoder(reader) + dec.SetCustomStructTag("json") + + err = dec.Decode(&response) + if err != nil { + _ = resp.Body.Close() + return response, err + } + default: _ = resp.Body.Close() - return response, err + return response, &InvalidContentType{mediatype} } _ = resp.Body.Close() @@ -291,6 +326,10 @@ func (vk *VK) Request(method string, sliceParams ...Params) ([]byte, error) { sliceParams = append(sliceParams, reqParams) + if vk.msgpack { + method += ".msgpack" + } + resp, err := vk.Handler(method, sliceParams...) return resp.Response, err @@ -303,7 +342,32 @@ func (vk *VK) RequestUnmarshal(method string, obj interface{}, sliceParams ...Pa return err } - return json.Unmarshal(rawResponse, &obj) + if vk.msgpack { + dec := msgpack.NewDecoder(bytes.NewReader(rawResponse)) + dec.SetCustomStructTag("json") + + err = dec.Decode(&obj) + } else { + err = json.Unmarshal(rawResponse, &obj) + } + + return err +} + +// EnableMessagePack enable using MessagePack instead of JSON. +// +// THIS IS EXPERIMENTAL FUNCTION! Broken encoding returned in some methods. +// +// See https://msgpack.org +func (vk *VK) EnableMessagePack() { + vk.msgpack = true +} + +// EnableZstd enable using zstd instead of gzip. +// +// This not use dict. +func (vk *VK) EnableZstd() { + vk.zstd = true } func fmtReflectValue(value reflect.Value, depth int) string { |