-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
74 lines (59 loc) · 2.02 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package event
import (
"context"
"encoding/json"
"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"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
const source = "opg.poas.makeregister"
type eventbridgeClient interface {
PutEvents(ctx context.Context, params *eventbridge.PutEventsInput, optFns ...func(*eventbridge.Options)) (*eventbridge.PutEventsOutput, error)
}
type Client struct {
svc eventbridgeClient
eventBusName string
}
func NewClient(cfg aws.Config, eventBusName string) *Client {
return &Client{
svc: eventbridge.NewFromConfig(cfg),
eventBusName: eventBusName,
}
}
func (c *Client) SendUidRequested(ctx context.Context, event UidRequested) error {
return c.send(ctx, "uid-requested", event)
}
func (c *Client) SendApplicationUpdated(ctx context.Context, event ApplicationUpdated) error {
return c.send(ctx, "application-updated", event)
}
func (c *Client) SendPreviousApplicationLinked(ctx context.Context, event PreviousApplicationLinked) error {
return c.send(ctx, "previous-application-linked", event)
}
func (c *Client) SendReducedFeeRequested(ctx context.Context, event ReducedFeeRequested) error {
return c.send(ctx, "reduced-fee-requested", event)
}
func (c *Client) SendNotificationSent(ctx context.Context, event NotificationSent) error {
return c.send(ctx, "notification-sent", event)
}
func (c *Client) send(ctx context.Context, detailType string, detail any) error {
tracer := otel.GetTracerProvider().Tracer("mlpab")
ctx, span := tracer.Start(ctx, detailType,
trace.WithSpanKind(trace.SpanKindInternal),
)
defer span.End()
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(detailType),
Detail: aws.String(string(v)),
}},
})
return err
}