-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (142 loc) · 3.31 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"flag"
"fmt"
"log"
retail "cloud.google.com/go/retail/apiv2alpha"
"google.golang.org/api/option"
retailpb "google.golang.org/genproto/googleapis/cloud/retail/v2alpha"
amqp "github.com/rabbitmq/amqp091-go"
)
const PROJECT_ID = "piotrostr-resources"
const PRODUCT_ID = "test-product"
func GetProductClient(ctx context.Context) *retail.ProductClient {
opts := []option.ClientOption{
option.WithCredentialsFile("service-account.json"),
}
client, err := retail.NewProductClient(ctx, opts...)
if err != nil {
log.Fatal(err)
}
return client
}
func ConvertToRetail(product *any) *retailpb.Product {
return nil
}
func Publish(ctx context.Context) {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
log.Fatal(err)
}
defer ch.Close()
q, err := ch.QueueDeclare(
"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Fatal(err)
}
body := "Hello World!"
err = ch.PublishWithContext(
ctx,
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
DeliveryMode: amqp.Persistent,
},
)
if err != nil {
log.Fatal(err)
}
}
func Subscribe(ctx context.Context, productClient *retail.ProductClient) {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
log.Fatal(err)
}
defer ch.Close()
q, err := ch.QueueDeclare(
"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Fatal(err)
}
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
log.Fatal(err)
}
fmt.Println("Waiting for messages...")
forever := make(chan bool)
go func() {
for d := range msgs {
fmt.Printf("Received a message: %s\n", d.Body)
fmt.Printf("Changing the test %s title\n", PRODUCT_ID)
product, err := productClient.UpdateProduct(ctx, &retailpb.UpdateProductRequest{
Product: &retailpb.Product{
Name: fmt.Sprintf(
"projects/%s/locations/global/catalogs/default_catalog/branches/default_branch/products/%s",
PROJECT_ID,
PRODUCT_ID,
),
Title: string(d.Body),
Description: "Test product description",
Categories: []string{"Testing"},
},
AllowMissing: true,
// optional: UpdateMask: &retailpb.FieldMask{}
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Product updated: %s\n", product)
}
}()
<-forever
}
func main() {
publisher := flag.Bool("publisher", false, "publish message")
subscriber := flag.Bool("subscriber", false, "receive messages")
flag.Parse()
ctx := context.Background()
productClient := GetProductClient(ctx)
defer productClient.Close()
if *publisher {
fmt.Println("Publishing message...")
Publish(ctx)
} else if *subscriber {
fmt.Println("Subscribing & updating...")
Subscribe(ctx, productClient)
}
}