-
Notifications
You must be signed in to change notification settings - Fork 6
/
sift.go
83 lines (67 loc) · 2.18 KB
/
sift.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
// Copyright 2016 Nevio Vesic
// Please check out LICENSE file for more information about what you CAN and what you CANNOT do!
// MIT License
package sift
import (
"encoding/json"
"fmt"
)
type Sift struct {
Client
}
// Track - Send tracking event towards sift science
func (s *Sift) Track(event string, params map[string]interface{}, args map[string]interface{}) (*Response, error) {
// The name of the event to send. This can either be a reserved
// event name such as "$transaction" or "$create_order" or a custom event
// name (that does not start with a $).
params["$type"] = event
// Whether the API response should include actions in the response. For
// more information on how this works, please visit the tutorial at:
// https://siftscience.com/resources/tutorials/formulas
if action, ok := args["return_action"]; ok {
params["return_action"] = action.(bool)
}
return s.HttpRequest("POST", s.GetEventsUrl(), params)
}
// Score - Get out user score
func (s *Sift) Score(userID string) (*Response, error) {
return s.HttpRequest("GET", fmt.Sprintf("%s?api_key=%s", s.GetScoreUrl(userID), s.ApiKey), map[string]interface{}{})
}
// Label - Request labeling for specific user
func (s *Sift) Label(userID string, params map[string]interface{}) (*Response, error) {
return s.HttpRequest("POST", s.GetLabelUrl(userID), params)
}
// UnLabel - Request unlabeling for specific user
func (s *Sift) UnLabel(userID string) (*Response, error) {
return s.HttpRequest("DELETE", fmt.Sprintf("%s?api_key=%s", s.GetLabelUrl(userID), s.ApiKey), map[string]interface{}{})
}
// New - Return Sift API client.
func New(apiKey string) *Sift {
return &Sift{
Client: Client{
Config: Config{
ApiUrl: API_URL,
ApiKey: apiKey,
ApiVersion: API_VERSION,
Timeout: TIMEOUT,
},
},
}
}
// NewFromJSON - Decode JSON file into new Sift client.
func NewFromJSON(config []byte) (*Sift, error) {
var client Sift
if err := json.Unmarshal(config, &client); err != nil {
return &client, err
}
if client.ApiUrl == "" {
client.ApiUrl = API_URL
}
if client.ApiVersion == 0 {
client.ApiVersion = API_VERSION
}
if client.Timeout == 0 {
client.Timeout = TIMEOUT
}
return &client, nil
}