-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathl4w.go
707 lines (589 loc) · 18 KB
/
l4w.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
Copyright (c) 2018 Usabilla
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, dis-
tribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the fol-
lowing conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
package usabilla
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
// Canonical URI constants.
const (
websitesURI = "/live/websites"
buttonURI = websitesURI + "/button"
campaignURI = websitesURI + "/campaign"
inpageURI = websitesURI + "/inpage"
)
var (
feedbackURI = buttonURI + "/%s/feedback"
campaignResultsURI = campaignURI + "/%s/results"
campaignStatsURI = campaignURI + "/%s/stats"
inpageFeedbackURI = inpageURI + "/%s/feedback"
)
// Button represents a button item.
type Button struct {
ID string `json:"id"`
Name string `json:"name"`
}
// FeedbackItem represents a feedback item.
type FeedbackItem struct {
ID string `json:"id"`
UserAgent string `json:"userAgent"`
Comment string `json:"comment"`
Location string `json:"location"`
Date time.Time `json:"date"`
Custom map[string]string `json:"custom"`
Email string `json:"email"`
Image string `json:"image,omitempty"`
Labels []string `json:"labels"`
NPS int `json:"nps"`
PublicURL string `json:"publicUrl"`
Rating int `json:"rating"`
ButtonID string `json:"buttonId"`
Tags []string `json:"tags"`
URL string `json:"url"`
}
// Campaign represents a campaign item.
type CampaignStruct struct {
ID string `json:"id"`
Date time.Time `json:"date"`
ButtonID string `json:"buttonId"`
AnalyticsID string `json:"analyticsId"`
Status string `json:"status"`
Name string `json:"name"`
Type string `json:"type"`
}
// CampaignResult represents a campaign result item.
type CampaignResultStruct struct {
ID string `json:"id"`
UserAgent string `json:"userAgent"`
Location string `json:"location"`
Date time.Time `json:"date"`
CampaignID string `json:"campaignId"`
Custom map[string]string `json:"custom"`
Data map[string]interface{} `json:"data"`
URL string `json:"url"`
Time float64 `json:"time"`
}
// CampaignStat represents a campaign statistics item.
type CampaignStatStruct struct {
ID string `json:"id"`
Completed int `json:"completed"`
Conversion int `json:"conversion"`
Views int `json:"views"`
}
type InpageWidgetStruct struct {
ID string `json:"id"`
Date time.Time `json:"date"`
Name string `json:"name"`
}
type InpageWidgetFeedbackStruct struct {
ID string `json:"id"`
Date time.Time `json:"date"`
Data map[string]interface{} `json:"data"`
CustomData map[string]interface{} `json:"customData"`
Widget_ID string `json:"widgetId"`
Rating float64 `json:"rating"`
Mood int `json:"mood"`
Nps int `json:"nps,omitempty"`
Comment string `json:"comment"`
UserAgent string `json:"userAgent"`
Geo GeoLoc `json:"geo"`
Url string `json:"url"`
}
type GeoLoc struct {
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
}
// ButtonResponse is a response that contains button data.
type ButtonResponse struct {
response
Items []Button `json:"items"`
}
// FeedbackResponse is a response that contains feedback item data.
type FeedbackResponse struct {
response
Items []FeedbackItem `json:"items"`
}
// CampaignResponse is a response that contains campaign data.
type CampaignResponse struct {
response
Items []CampaignStruct `json:"items"`
}
// CampaignResultResponse is a response that contains campaign result data.
type CampaignResultResponse struct {
response
Items []CampaignResultStruct `json:"items"`
}
// CampaignStatsResponse is a response that contains campaign statistics data.
type CampaignStatsResponse struct {
response
Items []CampaignStatStruct `json:"items"`
}
// InpageWidgetResponse is a response that lists inpage widgets.
type InpageWidgetResponse struct {
response
Items []InpageWidgetStruct `json:"items"`
}
// InpageWidgetFeedbackResponse is the response with in-page feedback data.
type InpageWidgetFeedbackResponse struct {
response
Items []InpageWidgetFeedbackStruct `json:"items"`
}
// Buttons represents the button resource of Usabilla API.
type Buttons struct {
resource
client *http.Client
}
// FeedbackItems represents the feedback item subresource of Usabilla API.
type FeedbackItems struct {
resource
client *http.Client
}
// Campaigns represents the campaign resource of Usabilla API.
type Campaigns struct {
resource
client *http.Client
}
// CampaignResults represents the campaign result resource of Usabilla API.
type CampaignResults struct {
resource
client *http.Client
}
// CampaignStats represents the campaign statistics resource of Usabilla API.
type CampaignStats struct {
resource
client *http.Client
}
// InpageWidgets represents the inpage widgets resource of Usabilla API.
type InpageWidgets struct {
resource
client *http.Client
}
// InpageWidgetFeedbackItems represents the inpage feedbacks resource of Usabilla API.
type InpageWidgetFeedbackItems struct {
resource
client *http.Client
}
// NewButtonResponse creates a button response and unmarshals json API
// button response to Go struct.
func NewButtonResponse(data []byte) (*ButtonResponse, error) {
response := &ButtonResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// NewFeedbackResponse creates a feedback response and unmarshals json API
// feedback items response to Go struct.
func NewFeedbackResponse(data []byte) (*FeedbackResponse, error) {
response := &FeedbackResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// NewCampaignResponse creates a campaign response and unmarshals json API
// campaign response to Go struct.
func NewCampaignResponse(data []byte) (*CampaignResponse, error) {
response := &CampaignResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// NewCampaignResultResponse creates a new campaign result response and unmarshals json API
// campaign results response to Go struct.
func NewCampaignResultResponse(data []byte) (*CampaignResultResponse, error) {
response := &CampaignResultResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// NewCampaignStatsResponse creates a new campaign statistics response and unmarshals json API
// campaign statistics response to Go struct.
func NewCampaignStatsResponse(data []byte) (*CampaignStatsResponse, error) {
response := &CampaignStatsResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// NewInpageWidgetResponse creates a inpage response and unmarshals json API
// inpage response to Go struct.
func NewInpageWidgetResponse(data []byte) (*InpageWidgetResponse, error) {
response := &InpageWidgetResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// NewInpageWidgetFeedbackResponse creates a new inpage feedback response and unmarshals json API
// inpage feedback response to Go struct.
func NewInpageWidgetFeedbackResponse(data []byte) (*InpageWidgetFeedbackResponse, error) {
response := &InpageWidgetFeedbackResponse{}
err := json.Unmarshal(data, &response)
if err != nil {
return response, err
}
return response, nil
}
// Get function of Buttons resource returns all the buttons
// taking into account the specified query parameters.
//
// Valid query parameters are:
// limit int
// since string (Time stamp)
func (b *Buttons) Get(params map[string]string) (*ButtonResponse, error) {
request := request{
method: "GET",
auth: b.auth,
uri: buttonURI,
params: params,
client: b.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewButtonResponse(data)
}
// Feedback encapsulates the feedback item resource.
func (b *Buttons) Feedback() *FeedbackItems {
return &FeedbackItems{
resource: resource{
auth: b.auth,
},
client: b.client,
}
}
// Get function of FeedbackItem resource returns all the feedback items
// for a specific button, taking into account the provided query parameters.
//
// Valid query parameters are:
// limit int
// since string (Time stamp)
func (f *FeedbackItems) Get(buttonID string, params map[string]string) (*FeedbackResponse, error) {
uri := fmt.Sprintf(feedbackURI, buttonID)
request := &request{
method: "GET",
auth: f.auth,
uri: uri,
params: params,
client: f.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewFeedbackResponse(data)
}
// Iterate uses a FeedbackItem channel which transparently uses the HasMore field to fire
// a new api request once all items have been consumed on the channel.
func (f *FeedbackItems) Iterate(buttonID string, params map[string]string) chan FeedbackItem {
resp, err := f.Get(buttonID, params)
if err != nil {
panic(err)
}
fic := make(chan FeedbackItem)
go items(fic, resp, f, buttonID)
return fic
}
// items feeds a feedback item channel with items.
//
// While hasMore is true and all items have been consumed in the channel
// a new request is fired using the since parameter of the response, to
// retrieve new items.
//
// When HasMore is false, we close the channel.
func items(fic chan FeedbackItem, resp *FeedbackResponse, f *FeedbackItems, buttonID string) {
for {
for _, item := range resp.Items {
fic <- item
}
if !resp.HasMore {
close(fic)
return
}
params := map[string]string{
"since": strconv.FormatInt(resp.LastTimestamp, 10),
}
resp, err := f.Get(buttonID, params)
if err != nil {
panic(err)
}
items(fic, resp, f, buttonID)
return
}
}
// Get function of Campaigns resource returns all the campaigns
// taking into account the provided query parameters.
//
// Valid query parameters are:
// limit int
// since string (Time stamp)
func (c *Campaigns) Get(params map[string]string) (*CampaignResponse, error) {
request := request{
method: "GET",
auth: c.auth,
uri: campaignURI,
params: params,
client: c.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewCampaignResponse(data)
}
// Results encapsulates the campaign results resource.
func (c *Campaigns) Results() *CampaignResults {
return &CampaignResults{
resource: resource{
auth: c.auth,
},
client: c.client,
}
}
// Get function of CampaignResults resource returns all the campaign result items
// for a specific campaign, taking into account the provided query parameters.
//
// Valid query params are:
// limit int
// since string (Time stamp)
func (r *CampaignResults) Get(campaignID string, params map[string]string) (*CampaignResultResponse, error) {
uri := fmt.Sprintf(campaignResultsURI, campaignID)
request := request{
method: "GET",
auth: r.auth,
uri: uri,
params: params,
client: r.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewCampaignResultResponse(data)
}
// Iterate uses a CampaignResult channel which transparently uses the HasMore field to fire
// a new api request once all results have been consumed on the channel
func (r *CampaignResults) Iterate(campaignID string, params map[string]string) chan CampaignResultStruct {
resp, err := r.Get(campaignID, params)
if err != nil {
panic(err)
}
crc := make(chan CampaignResultStruct)
go yieldCampaignResults(crc, resp, r, campaignID)
return crc
}
// campaignResults feeds a campaign results channel with items
//
// while hasMore is true and all items have been consumed in the channel
// a new request is fired using the since parameter of the response, to
// retrieve new items
//
// when HasMore is false, we close the channel
func yieldCampaignResults(crc chan CampaignResultStruct, resp *CampaignResultResponse, r *CampaignResults, campaignID string) {
for {
for _, item := range resp.Items {
crc <- item
}
if !resp.HasMore {
close(crc)
return
}
params := map[string]string{
"since": strconv.FormatInt(resp.LastTimestamp, 10),
}
resp, err := r.Get(campaignID, params)
if err != nil {
panic(err)
}
yieldCampaignResults(crc, resp, r, campaignID)
return
}
}
// Stats encapsulates the campaign statistics resource.
func (c *Campaigns) Stats() *CampaignStats {
return &CampaignStats{
resource: resource{
auth: c.auth,
},
client: c.client,
}
}
// Get function of CampaignStats resource returns the campaign statistics
// for a specific campaign, taking into account the provided query parameters.
//
// Valid query parameters are:
// limit int
// since string (Time stamp)
func (cs *CampaignStats) Get(campaignID string, params map[string]string) (*CampaignStatsResponse, error) {
uri := fmt.Sprintf(campaignStatsURI, campaignID)
request := request{
method: "GET",
auth: cs.auth,
uri: uri,
params: params,
client: cs.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewCampaignStatsResponse(data)
}
// Iterate uses a CampaignStat channel which transparently uses the HasMore field to fire
// a new api request once all stats items have been consumed on the channel.
func (cs *CampaignStats) Iterate(campaignID string, params map[string]string) chan CampaignStatStruct {
resp, err := cs.Get(campaignID, params)
if err != nil {
panic(err)
}
csc := make(chan CampaignStatStruct)
go yieldCampaignStats(csc, resp, cs, campaignID)
return csc
}
// campagnStats feeds a campaign statistics channel with items
//
// while hasMore is true and all items have been consumed in the channel
// a new request is fired using the since parameter of the response, to
// retrieve new items
//
// when HasMore is false, we close the channel
func yieldCampaignStats(csc chan CampaignStatStruct, resp *CampaignStatsResponse, cs *CampaignStats, campaignID string) {
for {
for _, item := range resp.Items {
csc <- item
}
if !resp.HasMore {
close(csc)
return
}
params := map[string]string{
"since": strconv.FormatInt(resp.LastTimestamp, 10),
}
resp, err := cs.Get(campaignID, params)
if err != nil {
panic(err)
}
yieldCampaignStats(csc, resp, cs, campaignID)
return
}
}
////////////// Inpage //////////////
// Get function of Inpage resource returns all the Inpage Widgets
// taking into account the provided query parameters.
//
// Valid query parameters are:
// limit int
// since string (Time stamp)
func (c *InpageWidgets) Get(params map[string]string) (*InpageWidgetResponse, error) {
request := request{
method: "GET",
auth: c.auth,
uri: inpageURI,
params: params,
client: c.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewInpageWidgetResponse(data)
}
// Results encapsulates the inpage feedback resource.
func (c *InpageWidgets) Feedback() *InpageWidgetFeedbackItems {
return &InpageWidgetFeedbackItems{
resource: resource{
auth: c.auth,
},
client: c.client,
}
}
// Get function of CampaignResults resource returns all the campaign result items
// for a specific campaign, taking into account the provided query parameters.
//
// Valid query params are:
// limit int
// since string (Time stamp)
func (r *InpageWidgetFeedbackItems) Get(inpageID string, params map[string]string) (*InpageWidgetFeedbackResponse, error) {
uri := fmt.Sprintf(inpageFeedbackURI, inpageID)
request := request{
method: "GET",
auth: r.auth,
uri: uri,
params: params,
client: r.client,
}
data, err := request.get()
if err != nil {
panic(err)
}
return NewInpageWidgetFeedbackResponse(data)
}
// Iterate uses a InpageWidgetFeedbackItems channel which transparently uses the HasMore field to fire
// a new api request once all results have been consumed on the channel
func (r *InpageWidgetFeedbackItems) Iterate(inpageID string, params map[string]string) chan InpageWidgetFeedbackStruct {
resp, err := r.Get(inpageID, params)
if err != nil {
panic(err)
}
crc := make(chan InpageWidgetFeedbackStruct)
go yieldInpageWidgetFeedbackItems(crc, resp, r, inpageID)
return crc
}
// yieldInpageWidgetFeedbackItems feeds a inpage results channel with items
//
// while hasMore is true and all items have been consumed in the channel
// a new request is fired using the since parameter of the response, to
// retrieve new items
//
// when HasMore is false, we close the channel
func yieldInpageWidgetFeedbackItems(crc chan InpageWidgetFeedbackStruct, resp *InpageWidgetFeedbackResponse, r *InpageWidgetFeedbackItems, inpageID string) {
for {
for _, item := range resp.Items {
crc <- item
}
if !resp.HasMore {
close(crc)
return
}
params := map[string]string{
"since": strconv.FormatInt(resp.LastTimestamp, 10),
}
resp, err := r.Get(inpageID, params)
if err != nil {
panic(err)
}
yieldInpageWidgetFeedbackItems(crc, resp, r, inpageID)
return
}
}