This repository was archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent.go
More file actions
134 lines (118 loc) · 3.62 KB
/
event.go
File metadata and controls
134 lines (118 loc) · 3.62 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
package chatbase
import (
"context"
"encoding/json"
"fmt"
)
var (
eventEndpoint = "https://api.chatbase.com/apis/v1/events/insert"
eventsEndpoint = "https://api.chatbase.com/apis/v1/events/insert_batch"
)
// Event contains data about an event
type Event struct {
APIKey string `json:"api_key"`
UserID string `json:"user_id"`
Intent string `json:"intent"`
TimeStamp int64 `json:"timestamp_millis,omitempty"`
Platform string `json:"platform,omitempty"`
Version string `json:"version,omitempty"`
Properties []EventProperty `json:"properties"`
}
// SetTimeStamp adds an optional "timestamp" value to the event
func (e *Event) SetTimeStamp(t int64) *Event {
e.TimeStamp = t
return e
}
// SetPlatform adds an optional "platform" value to the event
func (e *Event) SetPlatform(p string) *Event {
e.Platform = p
return e
}
// SetVersion adds an optional "version" value to the event
func (e *Event) SetVersion(v string) *Event {
e.Version = v
return e
}
// AddProperty adds a new property to the event using the given name and value.
// The passed value needs to be one of `int`, `string`, `bool` or `float64`
func (e *Event) AddProperty(name string, v interface{}) error {
prop, err := NewEventProperty(name, v)
if err != nil {
return err
}
e.Properties = append(e.Properties, prop)
return nil
}
// Submit tries to deliver the event to Chatbase
func (e *Event) Submit() error {
_, err := apiPost(eventEndpoint, e)
return err
}
// SubmitWithContext tries to deliver the event to Chatbase
// while considering the given context's deadline
func (e *Event) SubmitWithContext(ctx context.Context) error {
return withContext(ctx, e.Submit)
}
// Events is a collection of Event
type Events []Event
// MarshalJSON ensure the collection is correctly wrapped
// into an object and added the api_key value
func (e Events) MarshalJSON() ([]byte, error) {
var apiKey string
if len(e) > 0 {
apiKey = e[0].APIKey
}
return json.Marshal(map[string]interface{}{
"api_key": apiKey,
"events": []Event(e),
})
}
// Submit tries to deliver the set of events to Chatbase
func (e *Events) Submit() error {
_, err := apiPost(eventsEndpoint, e)
return err
}
// SubmitWithContext tries to deliver the set of events to Chatbase
// while considering the context's deadline
func (e *Events) SubmitWithContext(ctx context.Context) error {
return withContext(ctx, e.Submit)
}
// Append adds events to the the collection. The collection should not
// contain events using different API keys
func (e *Events) Append(addition ...*Event) *Events {
for _, a := range addition {
*e = append(*e, *a)
}
return e
}
// EventProperty is a property that is attached to an event
type EventProperty struct {
Name string `json:"property_name"`
StringValue string `json:"string_value,omitempty"`
IntegerValue int `json:"integer_value,omitempty"`
FloatValue float64 `json:"float_value,omitempty"`
BoolValue bool `json:"bool_value,omitempty"`
}
// NewEventProperty generates an EventProperty containing the correctly
// typed field for the passed value. The passed value needs to be one of
// `int`, `string`, `bool` or `float64`
func NewEventProperty(name string, value interface{}) (EventProperty, error) {
p := EventProperty{Name: name}
if s, ok := value.(string); ok {
p.StringValue = s
return p, nil
}
if i, ok := value.(int); ok {
p.IntegerValue = i
return p, nil
}
if f, ok := value.(float64); ok {
p.FloatValue = f
return p, nil
}
if b, ok := value.(bool); ok {
p.BoolValue = b
return p, nil
}
return p, fmt.Errorf("could not use %v as event property value", value)
}