pubsub.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package pubsub contains a Google Cloud Pub/Sub client.
  15. //
  16. // This package is experimental and may make backwards-incompatible changes.
  17. //
  18. // More information about Google Cloud Pub/Sub is available at
  19. // https://cloud.google.com/pubsub/docs
  20. package pubsub // import "google.golang.org/cloud/pubsub"
  21. import (
  22. "fmt"
  23. "net/http"
  24. raw "google.golang.org/api/pubsub/v1"
  25. "google.golang.org/cloud"
  26. "google.golang.org/cloud/internal"
  27. "google.golang.org/cloud/internal/transport"
  28. "golang.org/x/net/context"
  29. )
  30. const (
  31. // ScopePubSub grants permissions to view and manage Pub/Sub
  32. // topics and subscriptions.
  33. ScopePubSub = "https://www.googleapis.com/auth/pubsub"
  34. // ScopeCloudPlatform grants permissions to view and manage your data
  35. // across Google Cloud Platform services.
  36. ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform"
  37. )
  38. const prodAddr = "https://pubsub.googleapis.com/"
  39. const userAgent = "gcloud-golang-pubsub/20151008"
  40. // Client is a Google Pub/Sub client, which may be used to perform Pub/Sub operations with a project.
  41. // Note: Some operations are not yet available via Client, and must be performed via the legacy standalone functions.
  42. // It must be constructed via NewClient.
  43. type Client struct {
  44. projectID string
  45. s service
  46. }
  47. // NewClient create a new PubSub client.
  48. func NewClient(ctx context.Context, projectID string, opts ...cloud.ClientOption) (*Client, error) {
  49. o := []cloud.ClientOption{
  50. cloud.WithEndpoint(prodAddr),
  51. cloud.WithScopes(raw.PubsubScope, raw.CloudPlatformScope),
  52. cloud.WithUserAgent(userAgent),
  53. }
  54. o = append(o, opts...)
  55. httpClient, endpoint, err := transport.NewHTTPClient(ctx, o...)
  56. if err != nil {
  57. return nil, fmt.Errorf("dialing: %v", err)
  58. }
  59. s, err := newPubSubService(httpClient, endpoint)
  60. c := &Client{
  61. projectID: projectID,
  62. s: s,
  63. }
  64. return c, nil
  65. }
  66. func (c *Client) fullyQualifiedProjectName() string {
  67. return fmt.Sprintf("projects/%s", c.projectID)
  68. }
  69. // ModifyPushEndpoint modifies the URL endpoint to modify the resource
  70. // to handle push notifications coming from the Pub/Sub backend
  71. // for the specified subscription.
  72. func ModifyPushEndpoint(ctx context.Context, sub, endpoint string) error {
  73. _, err := rawService(ctx).Projects.Subscriptions.ModifyPushConfig(fullSubName(internal.ProjID(ctx), sub), &raw.ModifyPushConfigRequest{
  74. PushConfig: &raw.PushConfig{
  75. PushEndpoint: endpoint,
  76. },
  77. }).Do()
  78. return err
  79. }
  80. // fullSubName returns the fully qualified name for a subscription.
  81. // E.g. /subscriptions/project-id/subscription-name.
  82. func fullSubName(proj, name string) string {
  83. return fmt.Sprintf("projects/%s/subscriptions/%s", proj, name)
  84. }
  85. func rawService(ctx context.Context) *raw.Service {
  86. return internal.Service(ctx, "pubsub", func(hc *http.Client) interface{} {
  87. svc, _ := raw.New(hc)
  88. return svc
  89. }).(*raw.Service)
  90. }