-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
106 lines (89 loc) · 3.06 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
package lago
import (
"context"
"time"
"github.com/google/uuid"
)
type eventParams struct {
Event *EventInput `json:"event"`
}
type batchEventParams struct {
Events *[]*EventInput `json:"events"`
}
type EventInput struct {
TransactionID string `json:"transaction_id,omitempty"`
ExternalSubscriptionID string `json:"external_subscription_id,omitempty"`
Code string `json:"code,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
PreciseTotalAmountCents string `json:"precise_total_amount_cents,omitempty"`
Properties map[string]interface{} `json:"properties,omitempty"`
}
type eventEstimateFeesParams struct {
Event *EventEstimateFeesInput `json:"event"`
}
type EventEstimateFeesInput struct {
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"`
Code string `json:"code,omitempty"`
Timestamp time.Time `json:"timestamp"`
PreciseTotalAmountCents string `json:"precise_total_amount_cents,omitempty"`
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) CreateEvent(ctx context.Context, eventInput *EventInput) (*Event, error) {
u := c.url("events", nil)
result, err := post[eventParams, EventResult](
ctx,
c,
u,
&eventParams{Event: eventInput},
)
if err != nil {
return nil, err
}
return result.Event, nil
}
func (c *Client) EstimateEventFees(ctx context.Context, estimateInput *EventEstimateFeesInput) (*feeResult, error) {
u := c.url("events/estimate_fees", nil)
return post[eventEstimateFeesParams, feeResult](
ctx,
c,
u,
&eventEstimateFeesParams{Event: estimateInput},
)
}
func (c *Client) GetEvent(ctx context.Context, eventID string) (*Event, error) {
u := c.url("events/"+eventID, nil)
result, err := get[EventResult](ctx, c, u)
if err != nil {
return nil, err
}
return result.Event, nil
}
func (c *Client) BatchEvents(ctx context.Context, batchInput *[]*EventInput) (*[]*Event, error) {
u := c.url("events/batch", nil)
result, err := post[batchEventParams, BatchEventResult](
ctx,
c,
u,
&batchEventParams{Events: batchInput},
)
if err != nil {
return nil, err
}
return result.Events, nil
}