1// Copyright 2018 Google LLC
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
15package cloud_test
16
17import (
18	"context"
19	"time"
20
21	"cloud.google.com/go/bigquery"
22	"cloud.google.com/go/datastore"
23	"cloud.google.com/go/pubsub"
24	"golang.org/x/oauth2/google"
25	"google.golang.org/api/option"
26)
27
28// To set a timeout for an RPC, use context.WithTimeout.
29func Example_timeout() {
30	ctx := context.Background()
31	// Do not set a timeout on the context passed to NewClient: dialing happens
32	// asynchronously, and the context is used to refresh credentials in the
33	// background.
34	client, err := bigquery.NewClient(ctx, "project-id")
35	if err != nil {
36		// TODO: handle error.
37	}
38	// Time out if it takes more than 10 seconds to create a dataset.
39	tctx, cancel := context.WithTimeout(ctx, 10*time.Second)
40	defer cancel() // Always call cancel.
41
42	if err := client.Dataset("new-dataset").Create(tctx, nil); err != nil {
43		// TODO: handle error.
44	}
45}
46
47// To arrange for an RPC to be canceled, use context.WithCancel.
48func Example_cancellation() {
49	ctx := context.Background()
50	// Do not cancel the context passed to NewClient: dialing happens asynchronously,
51	// and the context is used to refresh credentials in the background.
52	client, err := bigquery.NewClient(ctx, "project-id")
53	if err != nil {
54		// TODO: handle error.
55	}
56	cctx, cancel := context.WithCancel(ctx)
57	defer cancel() // Always call cancel.
58
59	// TODO: Make the cancel function available to whatever might want to cancel the
60	// call--perhaps a GUI button.
61	if err := client.Dataset("new-dataset").Create(cctx, nil); err != nil {
62		// TODO: handle error.
63	}
64}
65
66// Google Application Default Credentials is the recommended way to authorize
67// and authenticate clients.
68//
69// For information on how to create and obtain Application Default Credentials, see
70// https://developers.google.com/identity/protocols/application-default-credentials.
71func Example_applicationDefaultCredentials() {
72	client, err := datastore.NewClient(context.Background(), "project-id")
73	if err != nil {
74		// TODO: handle error.
75	}
76	_ = client // Use the client.
77}
78
79// You can use a file with credentials to authenticate and authorize, such as a JSON
80// key file associated with a Google service account. Service Account keys can be
81// created and downloaded from
82// https://console.developers.google.com/permissions/serviceaccounts.
83//
84// This example uses the Datastore client, but the same steps apply to
85// the other client libraries underneath this package.
86func Example_credentialsFile() {
87	client, err := datastore.NewClient(context.Background(),
88		"project-id", option.WithCredentialsFile("/path/to/service-account-key.json"))
89	if err != nil {
90		// TODO: handle error.
91	}
92	_ = client // Use the client.
93}
94
95// In some cases (for instance, you don't want to store secrets on disk), you can
96// create credentials from in-memory JSON and use the WithCredentials option.
97//
98// The google package in this example is at golang.org/x/oauth2/google.
99//
100// This example uses the PubSub client, but the same steps apply to
101// the other client libraries underneath this package. Note that scopes can be
102// found at https://developers.google.com/identity/protocols/googlescopes, and
103// are also provided in all auto-generated libraries: for example,
104// cloud.google.com/go/pubsub/apiv1 provides DefaultAuthScopes.
105func Example_credentialsFromJSON() {
106	ctx := context.Background()
107	creds, err := google.CredentialsFromJSON(ctx, []byte("JSON creds"), pubsub.ScopePubSub)
108	if err != nil {
109		// TODO: handle error.
110	}
111	client, err := pubsub.NewClient(ctx, "project-id", option.WithCredentials(creds))
112	if err != nil {
113		// TODO: handle error.
114	}
115	_ = client // Use the client.
116}
117