-
Notifications
You must be signed in to change notification settings - Fork 20
/
event.go
160 lines (129 loc) · 4.01 KB
/
event.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
package lago
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
)
type EventRequest struct {
client *Client
}
type EventParams struct {
Event *EventInput `json:"event"`
}
type BatchEventParams struct {
Events *[]EventInput `json:"events"`
}
type EventInput struct {
TransactionID string `json:"transaction_id,omitempty"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
ExternalSubscriptionID string `json:"external_subscription_id,omitempty"`
Code string `json:"code,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Properties map[string]interface{} `json:"properties,omitempty"`
}
type EventEstimateFeesParams struct {
Event *EventEstimateFeesInput `json:"event"`
}
type EventEstimateFeesInput struct {
ExternalCustomerID string `json:"external_customer_id,omitempty"`
ExternalSubscriptionID string `json:"external_subscription_id,omitempty"`
Code string `json:"code,omitempty"`
Properties map[string]string `json:"properties,omitempty"`
}
type BatchEventResult struct {
Events *[]Event `json:"events"`
}
type EventResult struct {
Event *Event `json:"event"`
}
type Event struct {
LagoID uuid.UUID `json:"lago_id"`
TransactionID string `json:"transaction_id"`
LagoCustomerID *uuid.UUID `json:"lago_customer_id,omitempty"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
Code string `json:"code,omitempty"`
Timestamp time.Time `json:"timestamp"`
Properties map[string]interface{} `json:"properties,omitempty"`
LagoSubscriptionID *uuid.UUID `json:"lago_subscription_id,omitempty"`
ExternalSubscriptionID string `json:"external_subscription_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
func (c *Client) Event() *EventRequest {
return &EventRequest{
client: c,
}
}
func (er *EventRequest) Create(ctx context.Context, eventInput *EventInput) (*Event, *Error) {
eventParams := &EventParams{
Event: eventInput,
}
clientRequest := &ClientRequest{
Path: "events",
Result: &EventResult{},
Body: eventParams,
}
result, err := er.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
eventResult, ok := result.(*EventResult)
if !ok {
return nil, err
}
return eventResult.Event, nil
}
func (er *EventRequest) EstimateFees(ctx context.Context, estimateInput EventEstimateFeesInput) (*FeeResult, *Error) {
eventEstimateParams := &EventEstimateFeesParams{
Event: &estimateInput,
}
clientRequest := &ClientRequest{
Path: "events/estimate_fees",
Result: &FeeResult{},
Body: eventEstimateParams,
}
result, clientErr := er.client.Post(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
feeResult, ok := result.(*FeeResult)
if !ok {
return nil, &ErrorTypeAssert
}
return feeResult, nil
}
func (er *EventRequest) Get(ctx context.Context, eventID string) (*Event, *Error) {
subPath := fmt.Sprintf("%s/%s", "events", eventID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &EventResult{},
}
result, err := er.client.Get(ctx, clientRequest)
if err != nil {
return nil, err
}
eventResult, ok := result.(*EventResult)
if !ok {
return nil, &ErrorTypeAssert
}
return eventResult.Event, nil
}
func (er *EventRequest) Batch(ctx context.Context, batchInput *[]EventInput) (*[]Event, *Error) {
eventParams := &BatchEventParams{
Events: batchInput,
}
clientRequest := &ClientRequest{
Path: "events/batch",
Result: &EventResult{},
Body: eventParams,
}
result, err := er.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
batchEventResult, ok := result.(*BatchEventResult)
if !ok {
return nil, err
}
return batchEventResult.Events, nil
}