forked from yanatan16/golang-soundcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.go
More file actions
167 lines (139 loc) · 3.65 KB
/
core.go
File metadata and controls
167 lines (139 loc) · 3.65 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Package soundcloud provides a minimialist soundcloud API wrapper.
package soundcloud
import (
"encoding/json"
"github.com/pkg/errors"
"io"
"net/http"
"net/url"
"strings"
)
var (
ErrNoRedirects = errors.New("no redirects")
baseUrl = "https://api.soundcloud.com"
client = &http.Client{CheckRedirect: noRedirects}
)
func noRedirects(_ *http.Request, _ []*http.Request) error {
return ErrNoRedirects
}
type Api struct {
ClientId string
ClientSecret string
AccessToken string
RefreshToken string
}
func (api *Api) Authenticated() bool {
return api.AccessToken != ""
}
// -- Implementation of request --
func buildGetRequest(urlStr string, params url.Values) (*http.Request, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
// If we are getting, then we can't merge query params
if params != nil {
if u.RawQuery != "" {
return nil, errors.New("cannot merge query params in urlStr and params")
}
u.RawQuery = params.Encode()
}
u.Path += ".json"
return http.NewRequest("GET", u.String(), nil)
}
func buildNonGetRequest(method, urlStr string, params url.Values) (*http.Request, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
// If we are getting, then we can't merge query params
if u.RawQuery != "" {
if params != nil {
return nil, errors.New("cannot merge query params in urlStr and params")
}
params = u.Query()
u.RawQuery = ""
}
if u.Path != "/oauth2/token" {
u.Path += ".json"
}
return http.NewRequest(method, u.String(), strings.NewReader(params.Encode()))
}
func (api *Api) extendParams(p url.Values, auth ...bool) (url.Values, error) {
if p == nil {
p = url.Values{}
}
if len(auth) > 0 && auth[0] {
if api.AccessToken != "" {
p.Set("oauth_token", api.AccessToken)
} else {
return p, errors.New("access token required to use this endpoint")
}
} else {
p.Set("client_id", api.ClientId)
}
return p, nil
}
func (api *Api) get(path string, params url.Values, r interface{}, auth ...bool) error {
params, err := api.extendParams(params, auth...)
if err != nil {
return err
}
req, err := buildGetRequest(urlify(path), params)
if err != nil {
return err
}
return api.do(req, r)
}
func (api *Api) post(path string, params url.Values, r interface{}, auth ...bool) error {
return api.nonGet("POST", path, params, r)
}
func (api *Api) put(path string, params url.Values, r interface{}, auth ...bool) error {
return api.nonGet("PUT", path, params, r)
}
func (api *Api) delete(path string, params url.Values, r interface{}, auth ...bool) error {
return api.nonGet("DELETE", path, params, r)
}
func (api *Api) nonGet(method, path string, params url.Values, r interface{}, auth ...bool) error {
params, err := api.extendParams(params, auth...)
if err != nil {
return err
}
req, err := buildNonGetRequest(method, urlify(path), params)
if err != nil {
return err
}
return api.do(req, r)
}
func (api *Api) do(req *http.Request, r interface{}) error {
resp, err := client.Do(req)
if urlErr, ok := err.(*url.Error); ok && urlErr.Err == ErrNoRedirects {
err = nil
} else if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
return newApiError(req, resp)
}
if err := decodeResponse(resp.Body, r); err != nil {
return newApiErrorStack(err, req, resp)
}
return nil
}
func decodeResponse(body io.Reader, to interface{}) error {
err := json.NewDecoder(body).Decode(to)
if err != nil {
return errors.Wrap(err, "soundcloud: error decoding body")
}
return nil
}
func urlify(path string) string {
return baseUrl + path
}
func ensureParams(v url.Values) url.Values {
if v == nil {
return url.Values{}
}
return v
}