-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvk.go
56 lines (44 loc) · 1008 Bytes
/
vk.go
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
package vk
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
const api = `https://api.vk.com/method/`
type js struct {
Error vkError `json:"error"`
Response json.RawMessage `json:"response"`
}
type vkError struct {
Code int `json:"error_code"`
Message string `json:"error_msg"`
}
type Api struct {
Token string
}
func (this Api) Request(method string, params map[string]string) (result json.RawMessage, e error) {
request := api + method + "?"
for key, value := range params {
request += key + "=" + value + "&"
}
request += "access_token=" + this.Token
response, e := http.Get(request)
if e != nil {
return nil, e
}
defer response.Body.Close()
result, e = ioutil.ReadAll(response.Body)
if e != nil {
return nil, e
}
var j js
if e := json.Unmarshal(result, &j); e != nil {
return nil, e
}
if j.Error.Code != 0 {
return nil, errors.New(fmt.Sprint("vk: ", j.Error.Code, ", \"", j.Error.Message, "\""))
}
return j.Response, nil
}