2021-01-29 00:25:14 +01:00
|
|
|
package api
|
|
|
|
|
2022-01-28 23:48:40 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
2021-01-29 00:25:14 +01:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
2023-03-09 22:48:00 +01:00
|
|
|
// return Args.code; // return parameter "code"
|
|
|
|
// return Args.v; // return parameter "v"
|
2021-01-29 00:25:14 +01:00
|
|
|
//
|
2024-08-27 19:04:05 +02:00
|
|
|
// https://dev.vk.com/ru/method/execute
|
2021-01-29 00:25:14 +01:00
|
|
|
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)
|
2021-12-12 00:05:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 00:25:14 +01:00
|
|
|
|
2022-01-28 23:48:40 +01:00
|
|
|
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
|
2021-01-29 00:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
//
|
2024-08-27 19:04:05 +02:00
|
|
|
// https://dev.vk.com/ru/method/execute
|
2021-01-29 00:25:14 +01:00
|
|
|
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"
|
|
|
|
}
|