summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/SevereCloud/vksdk/v2/api/execute.go
blob: 1ee04cee64179c236ed660e83a5da4cba417b579 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package api

import (
	"bytes"
	"encoding/json"

	"github.com/vmihailenco/msgpack/v5"
)

// ExecuteWithArgs a universal method for calling a sequence of other methods
// while saving and filtering interim results.
//
// The Args map variable allows you to retrieve the parameters passed during
// the request and avoids code formatting.
//
// 	return Args.code; // return parameter "code"
// 	return Args.v; // return parameter "v"
//
// https://vk.com/dev/execute
func (vk *VK) ExecuteWithArgs(code string, params Params, obj interface{}) error {
	token := vk.getToken()

	reqParams := Params{
		"code":         code,
		"access_token": token,
		"v":            vk.Version,
	}

	resp, err := vk.Handler("execute", params, reqParams)
	if err != nil {
		return err
	}

	var decoderErr error

	if vk.msgpack {
		dec := msgpack.NewDecoder(bytes.NewReader(resp.Response))
		dec.SetCustomStructTag("json")

		decoderErr = dec.Decode(&obj)
	} else {
		decoderErr = json.Unmarshal(resp.Response, &obj)
	}

	if decoderErr != nil {
		return decoderErr
	}

	if resp.ExecuteErrors != nil {
		return &resp.ExecuteErrors
	}

	return err
}

// Execute a universal method for calling a sequence of other methods while
// saving and filtering interim results.
//
// https://vk.com/dev/execute
func (vk *VK) Execute(code string, obj interface{}) error {
	return vk.ExecuteWithArgs(code, Params{}, obj)
}

func fmtBool(value bool) string {
	if value {
		return "1"
	}

	return "0"
}