-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgophish.go
273 lines (239 loc) · 7.56 KB
/
gophish.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Package gophish provides a convenient wrapper around the Phish.Net API.
package gophish
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)
const (
dateFormat = "2006-01-02"
baseUrl = "https://api.phish.net/v3"
// By default, we will limit queries to 120/min.
defaultQueryRate = time.Minute / 120
)
// ParseDate parses the date string into a time.Time for use in the Phish.Net
// API.
func ParseDate(date string) (time.Time, error) {
return time.Parse(dateFormat, date)
}
// FormatDate converts a time.Time into the correct format that the Phish.Net API expects.
func FormatDate(date time.Time) string {
return date.Format(dateFormat)
}
// Client is a http.Client wrapper that handles rate limiting as well as
// provides convenience methods for various Phish.Net API endpoints.
type Client struct {
apiKey string
queryRate time.Duration
client *http.Client
baseUrl string
throttle <-chan time.Time
}
// ClientOpt is a option configuring the Client.
type ClientOpt func(*Client)
// WithQueryRate sets the rate limiting that should be used when making requests
// with the client.
func WithQueryRate(rate time.Duration) ClientOpt {
return func(c *Client) {
c.queryRate = rate
}
}
// WithTimeout sets the default timeout for making HTTP requests.
func WithTimeout(timeout time.Duration) ClientOpt {
return func(c *Client) {
c.client.Timeout = timeout
}
}
// WithBaseUrl sets the baseurl that should be used for requests. The default is https://api.phish.net/v3.
func WithBaseUrl(url string) ClientOpt {
return func(c *Client) {
c.baseUrl = url
}
}
// NewClient returns a new Client for making requests to the Phish.Net API.
func NewClient(apiKey string, opts ...ClientOpt) *Client {
c := &Client{
apiKey: apiKey,
queryRate: defaultQueryRate,
client: new(http.Client),
baseUrl: baseUrl,
}
for _, opt := range opts {
opt(c)
}
c.throttle = time.Tick(c.queryRate)
return c
}
func (c *Client) do(method, apiPath string, req, resp interface{}) error {
vals, err := query.Values(req)
if err != nil {
return errors.Wrapf(err, "unable to convert %v to URL values", req)
}
vals.Add("apikey", c.apiKey)
urlString := c.baseUrl + apiPath + "?" + vals.Encode()
hreq, err := http.NewRequest(method, urlString, nil)
if err != nil {
return errors.Wrapf(err, "unable to create http request for url %s", urlString)
}
res, err := c.Do(hreq)
if err != nil {
return errors.Wrap(err, "unable to perform the API lookup")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "unable to read body")
}
if res.StatusCode != 200 {
var errResp ErrorResponse
if err := json.Unmarshal(body, &errResp); err != nil {
return errors.Wrap(err, "unable to unmarshal json error")
}
return errResp
}
return errors.Wrap(json.Unmarshal(body, resp), "unable to umarshal json response")
}
// Do sends a throttled request.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
<-c.throttle
return c.client.Do(req)
}
type ErrorResponseBody struct {
Message string `json:"message"`
Body map[string]interface{} `json:"body"`
}
// ErrorResponse is the standard error format for API errors.
type ErrorResponse struct {
ErrorCode int `json:"error"`
Response *ErrorResponseBody `json:"response"`
}
func (e ErrorResponse) Error() string {
return fmt.Sprintf("API Error: %v", e)
}
type ResponseHeader struct {
ErrorCode int `json:"error_code"`
ErrorMessage string `json:"error_message"`
}
// ShowsQueryRequest is the request type for the shows/query endpoint.
type ShowsQueryRequest struct {
ShowIds []string `url:"showids,omitempty"`
Year int `url:"year,omitempty"`
Month int `url:"month,omitempty"`
Day int `url:"day,omitempty"`
VenueId int `url:"venueid,omitempty"`
TourId int `url:"tourid,omitempty"`
Country string `url:"country,omitempty"`
City string `url:"city,omitempty"`
State string `url:"state,omitempty"`
ShowdateGt string `url:"showdate_gt,omitempty"`
ShowdateGte string `url:"showdate_gte,omitempty"`
ShowdateLt string `url:"showdate_lt,omitempty"`
ShowdateLte string `url:"showdate_lte,omitempty"`
ShowyearGt int `url:"showyear_gt,omitempty"`
ShowyearGte int `url:"showyear_gte,omitempty"`
ShowyearLt int `url:"showyear_lt,omitempty"`
ShowyearLte int `url:"showyear_lte,omitempty"`
Limit int `url:"limit,omitempty"`
Order string `url:"order,omitempty"`
}
type Show struct {
ShowId int `json:"showid"`
ShowDate string `json:"showdate"`
ArtistId int `json:"artistid"`
BilledAs string `json:"billed_as"`
Link string `json:"link"`
Location string `json:"location"`
Venue string `json:"venue"`
SetlistNotes string `json:"setlistnotes"`
VenueId int `json:"venueid"`
TourId int `json:"tourid"`
TourName string `json:"tourname"`
TourWhen string `json:"tourwhen"`
ArtistLink string `json:"artistlink"`
}
type ShowsQueryResponse struct {
*ResponseHeader
Response *ShowsQueryResponseBody `json:"response"`
}
type ShowsQueryResponseBody struct {
Count int `json:"count"`
Data []*Show `json:"data"`
}
func (c *Client) ShowsQuery(req *ShowsQueryRequest) (*ShowsQueryResponse, error) {
var resp ShowsQueryResponse
if err := c.do(http.MethodPost, "/shows/query", req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
type SetlistsGetRequest struct {
ShowId int `url:"showid,omitempty"`
ShowDate string `url:"showdate,omitempty"`
}
type SetlistsRecentRequest struct {
Limit int `url:"limit,omitempty"`
}
type SetlistsResponse struct {
*ResponseHeader
Response *SetlistsResponseBody `json:"response"`
}
type SetlistsResponseBody struct {
Count int `json:"count"`
Data []*Setlist `json:"data"`
}
type Setlist struct {
ShowId int `json:"showid"`
ShowDate string `json:"showdate"`
ShortDate string `json:"short_date"`
LongDate string `json:"long_date"`
RelativeDate string `json:"relative_date"`
Url string `json:"url"`
GapChart string `json:"gapchart"`
Artist string `json:"artist"`
ArtistId int `json:"artistid"`
VenueId int `json:"venueid"`
Venue string `json:"venue"`
Location string `json:"location"`
SetlistData string `json:"setlistdata"`
SetlistNotes string `json:"setlistnotes"`
Rating string `json:"rating"`
}
func (c *Client) SetlistsGet(req *SetlistsGetRequest) (*SetlistsResponse, error) {
var resp SetlistsResponse
if err := c.do(http.MethodGet, "/setlists/get", req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) SetlistsLatest() (*SetlistsResponse, error) {
var resp SetlistsResponse
if err := c.do(http.MethodGet, "/setlists/latest", nil, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) SetlistsRecent(req *SetlistsRecentRequest) (*SetlistsResponse, error) {
var resp SetlistsResponse
if err := c.do(http.MethodGet, "/setlists/recent", req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) SetlistsTiph() (*SetlistsResponse, error) {
var resp SetlistsResponse
if err := c.do(http.MethodGet, "/setlists/tiph", nil, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) SetlistsRandom() (*SetlistsResponse, error) {
var resp SetlistsResponse
if err := c.do(http.MethodGet, "/setlists/random", nil, &resp); err != nil {
return nil, err
}
return &resp, nil
}