-
Notifications
You must be signed in to change notification settings - Fork 3
/
kkbox.go
116 lines (99 loc) · 2.42 KB
/
kkbox.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
package kkbox
import (
"crypto/tls"
"encoding/base64"
"errors"
"strconv"
"github.com/astaxie/beego/httplib"
)
// Box struct
type Box struct {
ClientID string
ClientSecret string
Auth *AuthData
Debug bool
}
// AuthData for access token
type AuthData struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Error string `json:"error"`
}
var (
// ErrorMissingIDorSecret for missing client id or secret
ErrorMissingIDorSecret = errors.New("missing client id or secret")
// ErrorInvalidClient for wrong token
ErrorInvalidClient = errors.New("invalid_client")
)
// New MyAllocator object
func New(id, secret string) (*Box, error) {
if id == "" || secret == "" {
return nil, ErrorMissingIDorSecret
}
box := &Box{
ClientID: id,
ClientSecret: secret,
}
// get token
auth, err := box.GetToken()
box.Auth = auth
if auth.Error != "" {
return nil, ErrorInvalidClient
}
return box, err
}
func (b *Box) getCredential() string {
data := []byte(b.ClientID + ":" + b.ClientSecret)
return base64.StdEncoding.EncodeToString(data)
}
// GetToken get access token
func (b *Box) GetToken() (*AuthData, error) {
req := httplib.Post(OAuthTokenURL).Debug(b.Debug)
req.Param("grant_type", "client_credentials")
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
req.Header("Authorization", "Basic "+b.getCredential())
res := new(AuthData)
err := req.ToJSON(res)
return res, err
}
func (b *Box) fetchData(url string, res interface{}, params ...Param) error {
req := httplib.Get(url).Debug(b.Debug)
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
offset := 0
limit := 10
territory := "TW"
q := ""
contentType := ""
if len(params) > 0 {
param := params[0]
if param.PerPage > 0 {
limit = param.PerPage
}
if param.Page > 1 {
offset = (param.Page - 1) * limit
}
if param.Territory != "" {
territory = param.Territory
}
if param.Q != "" {
q = param.Q
}
if param.Type != "" {
contentType = param.Type
}
}
// Add authorization header
authorization := b.Auth.TokenType + " " + b.Auth.AccessToken
req.Header("Authorization", authorization)
req.Param("offset", strconv.Itoa(offset))
req.Param("limit", strconv.Itoa(limit))
req.Param("territory", territory)
if q != "" {
req.Param("q", q)
}
if contentType != "" {
req.Param("type", contentType)
}
return req.ToJSON(res)
}