-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclient.go
182 lines (150 loc) · 3.99 KB
/
client.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package radiko
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"path"
"runtime"
"time"
)
const (
defaultEndpoint = "https://radiko.jp"
defaultHTTPTimeout = 120 * time.Second
apiV2 = "v2"
apiV3 = "v3"
// HTTP Headers
radikoAppHeader = "X-Radiko-App"
radikoAppVersionHeader = "X-Radiko-App-Version"
radikoUserHeader = "X-Radiko-User"
radikoDeviceHeader = "X-Radiko-Device"
radikoAuthTokenHeader = "X-Radiko-AuthToken"
radikoKeyLentghHeader = "X-Radiko-KeyLength"
radikoKeyOffsetHeader = "X-Radiko-KeyOffset"
radikoPartialKeyHeader = "X-Radiko-Partialkey"
radikoAuthkeyValue = "bcd151073c03b352e1ef2fd66c32209da9ca0afa"
radikoApp = "pc_html5"
radikoAppVersion = "0.0.1"
radikoUser = "test-stream"
radikoDevice = "pc"
)
var (
httpClient = &http.Client{Timeout: defaultHTTPTimeout}
userAgent = fmt.Sprintf("go-radiko (%s)", runtime.Version())
)
// Client represents a single connection to radiko API endpoint.
type Client struct {
URL *url.URL
httpClient *http.Client
authTokenHeader string
areaID string
}
// New returns a new Client struct.
func New(authToken string) (*Client, error) {
if httpClient == nil {
return nil, errors.New("httpClient is nil")
}
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
httpClient.Jar = jar
parsedURL, err := url.Parse(defaultEndpoint)
if err != nil {
return nil, err
}
areaID, err := AreaID()
if err != nil {
return nil, err
}
return &Client{
URL: parsedURL,
httpClient: httpClient,
authTokenHeader: authToken,
areaID: areaID,
}, nil
}
// Jar returns the cookieJar.
func (c *Client) Jar() http.CookieJar {
return c.httpClient.Jar
}
// SetJar sets the cookieJar in httpClient.
func (c *Client) SetJar(jar *cookiejar.Jar) {
c.httpClient.Jar = jar
}
// AreaID returns the areaID.
func (c *Client) AreaID() string {
return c.areaID
}
// SetAreaID sets the areaID.
func (c *Client) SetAreaID(areaID string) {
c.areaID = areaID
}
// AuthToken returns the authtoken.
func (c *Client) AuthToken() string {
return c.authTokenHeader
}
func (c *Client) setAuthTokenHeader(authToken string) {
c.authTokenHeader = authToken
}
func (c *Client) newRequest(ctx context.Context, verb, apiEndpoint string, params *Params) (*http.Request, error) {
u := *c.URL
u.Path = path.Join(c.URL.Path, apiEndpoint)
// Add query parameters
urlQuery := u.Query()
for k, v := range params.query {
urlQuery.Set(k, v)
}
u.RawQuery = urlQuery.Encode()
req, err := http.NewRequest(verb, u.String(), params.body)
if err != nil {
return nil, err
}
// Set the request's context
if ctx == nil {
return nil, errors.New("Context is nil")
}
req = req.WithContext(ctx)
// Add request headers
for k, v := range params.header {
req.Header.Set(k, v)
}
req.Header.Set("User-Agent", userAgent)
// For backwards compatibility with HTTP/1.0
// https://tools.ietf.org/html/rfc7234#page-29
req.Header.Set("pragma", "no-cache")
// Add auth_token in HTTP Header
if params.setAuthToken {
req.Header.Set(radikoAuthTokenHeader, c.AuthToken())
}
return req, nil
}
// Do executes an API request.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.httpClient.Do(req)
}
// Params is the list of options to pass to the request.
type Params struct {
// optional body used in http.NewRequest.
body io.Reader
// query is a map of key-value pairs that will be added to the Request.
query map[string]string
// header is a map of key-value pairs that will be added to the Request.
header map[string]string
// setAuthToken is a boolean value. If true, set auth_token in HTTP Header.
setAuthToken bool
}
// SetHTTPClient overrides the default HTTP client.
func SetHTTPClient(client *http.Client) {
httpClient = client
}
// SetUserAgent overrides the default User-Agent header.
func SetUserAgent(ua string) {
userAgent = ua
}
func apiPath(apiVersion, pathStr string) string {
return path.Join(apiVersion, "api", pathStr)
}