-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
111 lines (90 loc) · 2.9 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
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
package inspector
import (
"context"
"encoding/json"
"fmt"
"cloud.google.com/go/firestore"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Client struct {
firestoreClient *firestore.Client
pubSubRootCollection string
}
func NewClient(firestoreClient *firestore.Client, pubSubRootCollection string) (*Client, error) {
if firestoreClient == nil {
return nil, errors.New("empty firestore client")
}
if pubSubRootCollection == "" {
return nil, errors.New("empty pubsub root collection")
}
return &Client{firestoreClient, pubSubRootCollection}, nil
}
func (c *Client) GetTopics(ctx context.Context) ([]string, error) {
topicsDocs, err := c.rootCollection().Documents(ctx).GetAll()
if err != nil {
return nil, fmt.Errorf("failed to get topics: %s", err)
}
var topics []string
for _, topicDoc := range topicsDocs {
topics = append(topics, topicDoc.Ref.ID)
}
return topics, nil
}
func (c *Client) GetTopicSubscriptions(ctx context.Context, topic string) ([]string, error) {
subscriptionsDocs, err := c.rootCollection().Doc(topic).Collection("subscriptions").Documents(ctx).GetAll()
if err != nil {
return nil, err
}
var subscriptions []string
for _, topicDoc := range subscriptionsDocs {
subscriptions = append(subscriptions, topicDoc.Ref.ID)
}
return subscriptions, nil
}
func (c *Client) GetSubscriptionMessages(ctx context.Context, topic, subscription string) (message.Messages, error) {
docs, err := c.rootCollection().Doc(topic).Collection(subscription).Documents(ctx).GetAll()
if err != nil {
return nil, errors.Wrap(err, "could not get messages documents")
}
var messages message.Messages
for _, doc := range docs {
j, err := json.Marshal(doc.Data())
if err != nil {
return nil, errors.Wrap(err, "could not marshal message to json")
}
messages = append(messages, message.NewMessage(doc.Ref.ID, j))
}
return messages, nil
}
func (c *Client) AckMessage(ctx context.Context, topic, subscription, uuid string) error {
_, err := c.rootCollection().Doc(topic).Collection(subscription).Doc(uuid).Delete(ctx, firestore.Exists)
if status.Code(err) == codes.NotFound {
return nil
}
if err != nil {
return errors.Wrap(err, "could not delete document")
}
return nil
}
func (c *Client) PurgeSubscription(ctx context.Context, topic, subscription string) error {
docs, err := c.rootCollection().Doc(topic).Collection(subscription).Documents(ctx).GetAll()
if err != nil {
return errors.Wrap(err, "could not get all messages documents")
}
for _, doc := range docs {
_, err := doc.Ref.Delete(ctx, firestore.Exists)
if status.Code(err) == codes.NotFound {
continue
}
if err != nil {
return errors.Wrap(err, "could not delete document")
}
}
return nil
}
func (c *Client) rootCollection() *firestore.CollectionRef {
return c.firestoreClient.Collection(c.pubSubRootCollection)
}