/
client.go
56 lines (45 loc) · 1.35 KB
/
client.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
package event
import (
"context"
"encoding/json"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/service/eventbridge/types"
)
const source = "opg.poas.lpastore"
type EventBridgeClient interface {
PutEvents(ctx context.Context, params *eventbridge.PutEventsInput, optFns ...func(*eventbridge.Options)) (*eventbridge.PutEventsOutput, error)
}
type Client struct {
eventBusName string
svc EventBridgeClient
}
func NewClient(cfg aws.Config, eventBusName string) *Client {
return &Client{
svc: eventbridge.NewFromConfig(cfg, func(o *eventbridge.Options) {
if os.Getenv("AWS_DYNAMODB_ENDPOINT") != "" {
o.BaseEndpoint = aws.String(os.Getenv("AWS_EVENTBRIDGE_ENDPOINT"))
}
}),
eventBusName: eventBusName,
}
}
func (c *Client) SendLpaUpdated(ctx context.Context, event LpaUpdated) error {
return c.send(ctx, "lpa-updated", event)
}
func (c *Client) send(ctx context.Context, eventType string, detail any) error {
v, err := json.Marshal(detail)
if err != nil {
return err
}
_, err = c.svc.PutEvents(ctx, &eventbridge.PutEventsInput{
Entries: []types.PutEventsRequestEntry{{
EventBusName: aws.String(c.eventBusName),
Source: aws.String(source),
DetailType: aws.String(eventType),
Detail: aws.String(string(v)),
}},
})
return err
}