github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/pubsub/gcppubsub/example_test.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     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  //     https://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  
    15  package gcppubsub_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"gocloud.dev/gcp"
    22  	"gocloud.dev/pubsub"
    23  	"gocloud.dev/pubsub/gcppubsub"
    24  )
    25  
    26  func ExampleOpenTopic() {
    27  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    28  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    29  	ctx := context.Background()
    30  
    31  	// Your GCP credentials.
    32  	// See https://cloud.google.com/docs/authentication/production
    33  	// for more info on alternatives.
    34  	creds, err := gcp.DefaultCredentials(ctx)
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	// Open a gRPC connection to the GCP Pub/Sub API.
    39  	conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  	defer cleanup()
    44  
    45  	// Construct a PublisherClient using the connection.
    46  	pubClient, err := gcppubsub.PublisherClient(ctx, conn)
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	defer pubClient.Close()
    51  
    52  	// Construct a *pubsub.Topic.
    53  	topic, err := gcppubsub.OpenTopicByPath(pubClient, "projects/myprojectID/topics/example-topic", nil)
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	defer topic.Shutdown(ctx)
    58  }
    59  
    60  func Example_openTopicFromURL() {
    61  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    62  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsub"
    63  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    64  	ctx := context.Background()
    65  
    66  	topic, err := pubsub.OpenTopic(ctx, "gcppubsub://projects/myproject/topics/mytopic")
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	defer topic.Shutdown(ctx)
    71  }
    72  
    73  func ExampleOpenSubscription() {
    74  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    75  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    76  	ctx := context.Background()
    77  
    78  	// Your GCP credentials.
    79  	// See https://cloud.google.com/docs/authentication/production
    80  	// for more info on alternatives.
    81  	creds, err := gcp.DefaultCredentials(ctx)
    82  	if err != nil {
    83  		log.Fatal(err)
    84  	}
    85  
    86  	// Open a gRPC connection to the GCP Pub/Sub API.
    87  	conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	defer cleanup()
    92  
    93  	// Construct a SubscriberClient using the connection.
    94  	subClient, err := gcppubsub.SubscriberClient(ctx, conn)
    95  	if err != nil {
    96  		log.Fatal(err)
    97  	}
    98  	defer subClient.Close()
    99  
   100  	// Construct a *pubsub.Subscription.
   101  	subscription, err := gcppubsub.OpenSubscriptionByPath(
   102  		subClient, "projects/myprojectID/subscriptions/example-subscription", nil)
   103  	if err != nil {
   104  		log.Fatal(err)
   105  	}
   106  	defer subscription.Shutdown(ctx)
   107  }
   108  
   109  func Example_openSubscriptionFromURL() {
   110  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
   111  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsub"
   112  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
   113  	ctx := context.Background()
   114  
   115  	subscription, err := pubsub.OpenSubscription(ctx,
   116  		"gcppubsub://projects/my-project/subscriptions/my-subscription")
   117  	if err != nil {
   118  		log.Fatal(err)
   119  	}
   120  	defer subscription.Shutdown(ctx)
   121  }