Skip to content

Commit 805a7a4

Browse files
mattlcopybara-github
authored andcommitted
Update app request.go to use cloud.google.com/go/pubsub
PiperOrigin-RevId: 327275083
1 parent c11f9cc commit 805a7a4

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

appengine/endpoints/request.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import (
3131

3232
"google.golang.org/appengine"
3333
"google.golang.org/appengine/log"
34-
"google.golang.org/api/pubsub/v1"
35-
"golang.org/x/oauth2/google"
34+
"cloud.google.com/go/pubsub"
3635
"github.com/google/splice/appengine/server"
3736
basic "github.com/google/splice/appengine/validators"
3837
"github.com/google/splice/models"
@@ -304,34 +303,25 @@ func publishRequest(ctx context.Context, reqID string) error {
304303
return errors.New("PUBSUB_TOPIC environment variable not set")
305304
}
306305

307-
// PubSub requires an http client with an oAuth token, use the
308-
// default credential for our token.
309-
httpClient, err := google.DefaultClient(ctx, pubsub.PubsubScope)
310-
ps, err := pubsub.New(httpClient)
306+
ps, err := pubsub.NewClient(ctx, envProject)
311307
if err != nil {
312-
return fmt.Errorf("pubsub.New(%v) returned: %v", httpClient, err)
308+
return fmt.Errorf("pubsub.NewClient(%q) returned: %v", envProject, err)
313309
}
314310

315311
// Create topic if it doesn't exist.
316312
topicName := "projects/" + envProject + "/topics/" + envTopic
317-
topicSvc := pubsub.NewProjectsTopicsService(ps)
318-
_, err = topicSvc.Create(topicName, &pubsub.Topic{}).Do()
313+
topic, err := ps.CreateTopic(ctx, topicName)
319314
if err != nil && !strings.Contains(err.Error(), "alreadyExists") {
320315
return fmt.Errorf("failed to create topic %q: %v", topicName, err)
321316
}
317+
defer topic.Stop()
318+
res := topic.Publish(ctx, &pubsub.Message{Data: []byte(reqID)})
322319

323-
msg := &pubsub.PubsubMessage{
324-
Data: base64.StdEncoding.EncodeToString([]byte(reqID)),
325-
}
326-
publishReq := &pubsub.PublishRequest{
327-
Messages: []*pubsub.PubsubMessage{msg},
328-
}
329-
330-
response, err := topicSvc.Publish(topicName, publishReq).Do()
320+
msgID, err := res.Get(ctx)
331321
if err != nil {
332-
return fmt.Errorf("publishRequest error publishing to topic %s: %v, %v", topicName, response, err)
322+
return fmt.Errorf("topic.Publish error publishing to topic %s: %v, %v", topicName, res, err)
333323
}
334324

335-
log.Infof(ctx, "request id %q published with msg id '%s'", reqID, response.MessageIds[0])
325+
log.Infof(ctx, "request id %q published with msg id %q", reqID, msgID)
336326
return nil
337327
}

0 commit comments

Comments
 (0)