-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexport.go
67 lines (59 loc) · 1.4 KB
/
export.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
package mixpanel
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
const (
exportUrl = "/api/2.0/export"
ExportNoLimit int = 0
ExportNoEventFilter string = ""
ExportNoWhereFilter string = ""
)
// Export calls the Raw Export API
// https://developer.mixpanel.com/reference/raw-event-export
func (a *ApiClient) Export(ctx context.Context, fromDate, toDate time.Time, limit int, event, where string) ([]*Event, error) {
query := url.Values{}
query.Add("from_date", fromDate.Format("2006-01-02"))
query.Add("to_date", toDate.Format("2006-01-02"))
if limit != ExportNoLimit {
query.Add("limit", strconv.Itoa(limit))
}
if event != "" {
query.Add("event", event)
}
if where != "" {
query.Add("where", where)
}
httpResponse, err := a.doRequestBody(
ctx,
http.MethodGet,
a.dataEndpoint+exportUrl,
nil,
a.exportServiceAccount(), acceptPlainText(), addQueryParams(query),
)
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
switch httpResponse.StatusCode {
case http.StatusOK:
var results []*Event
dec := json.NewDecoder(httpResponse.Body)
for dec.More() {
var e *Event
err := dec.Decode(&e)
if err != nil {
return nil, fmt.Errorf("failed to decode event:%w", err)
}
results = append(results, e)
}
return results, nil
default:
return nil, newHttpError(httpResponse.StatusCode, httpResponse.Body)
}
}