-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
68 lines (61 loc) · 1.47 KB
/
api.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
package vk
import (
"net/url"
"strconv"
"time"
)
var (
// Version of VK API
Version = "5.12"
// APIURL is a base to make API calls
APIURL = "https://api.vk.com/method/"
// HTTPS defines if use https instead of http. 1 - use https. 0 - use http
HTTPS = 1
)
// API holds data to use for communication
type API struct {
AppID string
Secret string
Scope []string
AccessToken string
Expiry time.Time
UserID string
UserEmail string
callbackURL *url.URL
requestTokenURL *url.URL
accessTokenURL *url.URL
}
// NewAPI creates instance of API
func NewAPI(appID, secret string, scope []string, callback string) *API {
var err error
var callbackURL *url.URL
if appID == "" {
return nil
}
if secret == "" {
return nil
}
if callbackURL, err = url.Parse(callback); err != nil {
return nil
}
reqTokURL, _ := url.Parse("https://oauth.vk.com/authorize")
accTokURL, _ := url.Parse("https://oauth.vk.com/access_token")
return &API{
AppID: appID,
Secret: secret,
Scope: scope,
callbackURL: callbackURL,
requestTokenURL: reqTokURL,
accessTokenURL: accTokURL,
}
}
// getAPIURL prepares URL instance with defined method
func (api *API) getAPIURL(method string) *url.URL {
q := url.Values{
"v": {Version},
"https": {strconv.Itoa(HTTPS)},
"access_token": {api.AccessToken},
}.Encode()
apiURL, _ := url.Parse(APIURL + method + "?" + q)
return apiURL
}