-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlytics.go
More file actions
321 lines (263 loc) · 7.53 KB
/
lytics.go
File metadata and controls
321 lines (263 loc) · 7.53 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
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
package lytics
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// Client defines the supported subset of the Lytics API. The Lytics API may contain other features
// that have been added or deprecated since the last update of this SDK.
//
// Lytics reserves the right to deprecate or modify endpoints at any time and does not guarantee
// backwards compatibility or SemVer versioning for early version of API.
//
// For more information see the Lytics API documentation at
// http://getlytics.com/developers/rest-api
//
// Original Author: Mark Hayden
// Contributions: Mark Hayden
// Version: 0.0.2
//
const (
updated = "2017-01-12"
apiVersion = "1.1.0"
libraryVersion = "0.0.3"
)
var (
apiBase = "https://api.lytics.io/api"
)
func init() {
if apiEnv := os.Getenv("LIOAPI"); apiEnv != "" {
apiBase = apiEnv + "/api"
}
}
type TableWriter interface {
Headers() []interface{}
Row() []interface{}
}
// Client bundles the data necessary to interact with the vast majority of Lytics REST endpoints.
type Client struct {
baseURL string
apiKey string
dataApiKey string
client *http.Client
}
// ApiResp is the core api response for all Lytics endpoints. In some instances the "Status" is returned
// as a string rather than an int. This is a known but and will be addressed / updated.
type ApiResp struct {
Status interface{} `json:"status"`
Message string `json:"message"`
Meta map[string]interface{} `json:"meta"`
Next string `json:"_next"`
Total int `json:"total"`
Data json.RawMessage `json:"data"`
}
// NewLytics creates a new client instance. This contains the segment pager, segment details
// and maintains all core data used throughout this SDK
func NewLytics(apiKey string, httpclient *http.Client) *Client {
l := Client{
baseURL: apiBase,
}
if httpclient != nil {
l.client = httpclient
} else {
l.client = http.DefaultClient
}
// set the apikey if not null
l.apiKey = apiKey
return &l
}
// BaseUrl returns the base url used in all calls for this client.
func (l *Client) BaseUrl() string {
return l.baseURL
}
// ApiKey returns the API key configured for this client.
func (l *Client) ApiKey() string {
return l.apiKey
}
// DataApiKey returns the public API key configured for this client.
func (l *Client) DataApiKey() string {
return l.dataApiKey
}
// Client returns the HTTP client configured for this client.
func (l *Client) Client() *http.Client {
return l.client
}
// SetClient updates the HTTP client for this client. Used to alter main client
// handling for instances such as AppEngine
func (l *Client) SetClient(c *http.Client) {
l.client = c
}
// PrepUrl handles the parsing and setup for all api calls. The encoded url string is passed
// along with a set of param. Params are looped and injected into the master url
func (l *Client) PrepUrl(endpoint string, params url.Values, dataKey bool) (string, error) {
// parse the url into native http.URL
url, err := url.Parse(fmt.Sprintf("%s/%s", l.BaseUrl(), endpoint))
if err != nil {
return "", err
}
values := url.Query()
// add the api key
if params != nil {
for key, val := range params {
for _, v := range val {
values.Add(key, v)
}
}
}
// if there is a data key use that by default, if not use the main api key
// assumption here is that if its a data key there are specific reasons
if dataKey {
values.Add("key", l.dataApiKey)
} else {
values.Add("key", l.apiKey)
}
// encode the final url so we can return string and make call
url.RawQuery = values.Encode()
return url.String(), nil
}
// Do handles executing all http requests for the SDK. Takes a httpRequest and parses
// the response into the master api struct as well as a specific data type.
func (l *Client) Do(r *http.Request, response, data interface{}) error {
// make the request
res, err := l.client.Do(r)
if err != nil {
return err
}
defer res.Body.Close()
// get the response
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
// if we have some struct to unmarshal body into, do that and return
if response != nil {
err = buildRespJSON(b, response, data)
if err != nil {
return err
}
switch rt := response.(type) {
case *ApiResp:
// if we have an invalid response code error out
if res.StatusCode >= 301 {
return fmt.Errorf(rt.Message)
}
}
}
// if we have an invalid response code error out
if res.StatusCode >= 301 {
return fmt.Errorf("Received non-successful response: %d", res.StatusCode)
}
return nil
}
// Get prepares a get request and then executes using the Do method
func (l *Client) Get(endpoint string, params url.Values, body interface{}, response, data interface{}) error {
method := "GET"
// get the formatted endpoint url
path, err := l.PrepUrl(endpoint, params, false)
if err != nil {
return err
}
payload, err := prepRequestBody(body)
if err != nil {
return err
}
// build the request
r, _ := http.NewRequest(method, path, payload)
// execute the request
err = l.Do(r, response, data)
if err != nil {
return err
}
return nil
}
// Get prepares a post request and then executes using the Do method
func (l *Client) Post(endpoint string, params url.Values, body interface{}, response, data interface{}) error {
return l.PostType("application/json", endpoint, params, body, response, data)
}
// Get prepares a post request and then executes using the Do method
func (l *Client) PostType(contentType, endpoint string, params url.Values, body interface{}, response, data interface{}) error {
method := "POST"
// get the formatted endpoint url
path, err := l.PrepUrl(endpoint, params, false)
if err != nil {
return err
}
//log.Printf("prep %s %T \n", path, body)
payload, err := prepRequestBody(body)
if err != nil {
return err
}
// build the request
r, _ := http.NewRequest(method, path, payload)
r.Header.Set("Content-Type", contentType)
// execute the request
err = l.Do(r, response, data)
if err != nil {
return err
}
return nil
}
// prepBodyRequest takes the payload and returns an io.Reader
func prepRequestBody(body interface{}) (io.Reader, error) {
switch val := body.(type) {
case string:
return strings.NewReader(val), nil
case nil:
return nil, nil
default:
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
return nil, nil
}
// buildRespJSON handles the first round of unmarshaling into the master Api Response struct
func buildRespJSON(b []byte, response, data interface{}) error {
var err error
err = json.Unmarshal(b, response)
if err != nil {
return err
}
switch rt := response.(type) {
case *ApiResp:
if len(rt.Data) > 0 {
err = json.Unmarshal(rt.Data, &data)
if err != nil {
return err
}
}
}
return nil
}
// parseLyticsTime translates a timestamp as returned by Lytics into a Go standard timestamp.
func parseLyticsTime(ts string) (time.Time, error) {
var err error
i, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return time.Now(), err
}
tm := time.Unix((i / 1000), 0)
return tm, err
}
// formatLyticsTime translates a timestamp into a human-readable form.
func formatLyticsTime(t *time.Time) string {
return t.Format("Mon, 2 Jan 2006 15:04:05 -0700")
}
// parseLyticsURL joins params with a string using : notation
func parseLyticsURL(url string, params map[string]string) string {
out := url
for key, value := range params {
out = strings.Replace(out, fmt.Sprintf(":%s", key), value, -1)
}
return out
}