Skip to content

Commit

Permalink
Update app request.go to use cloud.google.com/go/pubsub
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 327275083
  • Loading branch information
ItsMattL authored and copybara-github committed Aug 18, 2020
1 parent c11f9cc commit 805a7a4
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions appengine/endpoints/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import (

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

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

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

msg := &pubsub.PubsubMessage{
Data: base64.StdEncoding.EncodeToString([]byte(reqID)),
}
publishReq := &pubsub.PublishRequest{
Messages: []*pubsub.PubsubMessage{msg},
}

response, err := topicSvc.Publish(topicName, publishReq).Do()
msgID, err := res.Get(ctx)
if err != nil {
return fmt.Errorf("publishRequest error publishing to topic %s: %v, %v", topicName, response, err)
return fmt.Errorf("topic.Publish error publishing to topic %s: %v, %v", topicName, res, err)
}

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

0 comments on commit 805a7a4

Please sign in to comment.