Skip to content

Commit

Permalink
feat: use header for msgpack
Browse files Browse the repository at this point in the history
  • Loading branch information
SevereCloud committed Jan 17, 2025
1 parent b01a28d commit da2d282
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
74 changes: 50 additions & 24 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,55 @@ 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) {
const (
mediaTypeJSON = "application/json"
mediaTypeMessagePack = "application/vnd.msgpack"
mediaTypeXMessagePack = "application/x-msgpack"
)

func (vk *VK) setRequestHeaders(req *http.Request, token string) {
acceptEncoding := "gzip"
accept := mediaTypeJSON
xResponseFormat := "json"

if vk.zstd {
acceptEncoding = "zstd"
}

if vk.msgpack {
accept = mediaTypeMessagePack
xResponseFormat = "msgpack"
}

req.Header.Set("Authorization", "Bearer "+token)

req.Header.Set("User-Agent", vk.UserAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

req.Header.Set("Accept-Encoding", acceptEncoding)
req.Header.Set("Accept", accept)
req.Header.Set("X-Response-Format", xResponseFormat)
}

func (vk *VK) buildRequest(method string, sliceParams ...Params) (*http.Request, error) {
u := vk.MethodURL + method
ctx, query := buildQuery(sliceParams...)
token := sliceParams[len(sliceParams)-1]["access_token"].(string)

rawBody := bytes.NewBufferString(query.Encode())

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, rawBody)
if err != nil {
return req, fmt.Errorf("api.DefaultHandler: %w", err)
}

vk.setRequestHeaders(req, token)

return req, nil
}

// DefaultHandler provides access to VK API methods.
func (vk *VK) DefaultHandler(method string, sliceParams ...Params) (Response, error) {
attempt := 0

for {
Expand All @@ -248,26 +293,11 @@ func (vk *VK) DefaultHandler(method string, sliceParams ...Params) (Response, er
vk.mux.Unlock()
}

rawBody := bytes.NewBufferString(query.Encode())

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, rawBody)
req, err := vk.buildRequest(method, sliceParams...)
if err != nil {
return response, fmt.Errorf("api.DefaultHandler: %w", err)
}

acceptEncoding := "gzip"
if vk.zstd {
acceptEncoding = "zstd"
}

token := sliceParams[len(sliceParams)-1]["access_token"].(string)
req.Header.Set("Authorization", "Bearer "+token)

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)
Expand All @@ -292,13 +322,13 @@ func (vk *VK) DefaultHandler(method string, sliceParams ...Params) (Response, er

mediatype, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
switch mediatype {
case "application/json":
case mediaTypeJSON:
err = json.NewDecoder(reader).Decode(&response)
if err != nil {
_ = resp.Body.Close()
return response, fmt.Errorf("api.DefaultHandler: %w", err)
}
case "application/x-msgpack":
case mediaTypeXMessagePack, mediaTypeMessagePack:
dec := msgpack.NewDecoder(reader)
dec.SetCustomStructTag("json")

Expand Down Expand Up @@ -340,10 +370,6 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestVK_InvalidContentType(t *testing.T) {
vk.MethodURL = "https://api.vk.com"

err := vk.RequestUnmarshal("", testObj, nil)
if err == nil || err.Error() != "api: invalid content-type" {
if err == nil || err.Error() != "api: invalid content-type(text/html)" {
t.Errorf("VK.RequestUnmarshal() error = %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ type InvalidContentType struct {

// Error returns the message of a InvalidContentType.
func (e InvalidContentType) Error() string {
return "api: invalid content-type"
return fmt.Sprintf("api: invalid content-type(%s)", e.ContentType)
}

// UploadError type.
Expand Down

0 comments on commit da2d282

Please sign in to comment.