-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
40 lines (35 loc) · 781 Bytes
/
pubsub.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
package audiy_importer
import (
"cloud.google.com/go/pubsub"
"context"
"log"
)
// pubsubClient is pubsub client wrapper
type pubsubClient struct {
topic string
client *pubsub.Client
}
func NewClient(project, topic string) (*pubsubClient, error) {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, project)
if err != nil {
return nil, err
}
psCli := pubsubClient{
topic: topic,
client: client,
}
return &psCli, err
}
func (c *pubsubClient) Publish(ctx context.Context, serialized []byte) error {
topic := c.client.Topic(c.topic)
result, err := topic.Publish(ctx, &pubsub.Message{Data: serialized}).Get(ctx)
if err != nil {
return err
}
if err != nil {
return err
}
log.Printf("successfully published ID: %v", result)
return nil
}