-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk.go
144 lines (106 loc) · 2.68 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package vk_sdk
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
)
type ApiError interface {
// Code returns error code.
Code() int
// Subcode returns error subcode.
Subcode() *int
// Msg returns error message.
Msg() string
// Text returns error text
Text() string
// RequestParams returns error request parameters.
RequestParams() []RequestParam
// RedirectURI returns redirect URI
RedirectURI() *string
// ConfirmationText returns confirmation text
ConfirmationText() *string
// Captcha returns Captcha, if exist
Captcha() Captcha
// Is checks if the error code matches input ErrorCode
Is(code ErrorCode) bool
}
type Captcha interface {
SID() string
Img() string
}
// VK the main structure for calling requests to the API
type VK struct {
client *http.Client
token string
}
// NewVK create and return new VK
func NewVK(client *http.Client, token ...string) *VK {
var vk VK
vk.client = client
if len(token) > 0 {
vk.token = token[0]
}
return &vk
}
// SetToken set access token
func (vk *VK) SetToken(token string) {
vk.token = token
}
func (vk *VK) doReq(methodName string, ctx context.Context, values url.Values, dst interface{}) (ApiError, error) {
req, err := vk.buildRequest(methodName, ctx, values)
if err != nil {
return nil, err
}
resp, err := vk.client.Do(req)
if err != nil {
return nil, err
}
return vk.parseResponse(resp, dst)
}
const (
apiScheme = "https"
apiHost = "api.vk.com"
apiPath = "method"
versionKey = "v"
tokenKey = "access_token"
)
// buildRequest build request to Vkontakte API with version and access token
func (vk *VK) buildRequest(methodName string, ctx context.Context, values url.Values) (*http.Request, error) {
values.Set(versionKey, Version)
values.Set(tokenKey, vk.token)
reqBody := bytes.NewBufferString(values.Encode())
reqURL := &url.URL{
Scheme: apiScheme,
Host: apiHost,
Path: apiPath + "/" + methodName,
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL.String(), reqBody)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
// parseResponse parse response and returns Error if present
func (vk *VK) parseResponse(resp *http.Response, dst interface{}) (ApiErr ApiError, err error) {
defer func() {
if closeErr := resp.Body.Close(); err == nil {
err = closeErr
}
}()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var apiErr apiError
if err = json.Unmarshal(respBody, &apiErr); apiErr.ErrorCode != 0 || err != nil {
return &apiErr, err
}
if err = json.Unmarshal(respBody, &dst); err != nil {
return nil, err
}
return nil, nil
}