/
FaqSnippets.cs
162 lines (145 loc) · 5.61 KB
/
FaqSnippets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Apis.Logging;
using Google.Cloud.PubSub.V1;
using Google.Cloud.Translation.V2;
using static Google.Apis.Http.ConfigurableMessageHandler;
using Grpc.Core;
using System;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Vision.V1;
using System.Collections.Generic;
using Google.Cloud.Scheduler.V1;
namespace Google.Cloud.Tools.Snippets
{
public class FaqSnippets
{
public void Emulator()
{
// Sample: Emulator
// [START pubsub_use_emulator]
// For example, "localhost:8615"
string emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
Channel channel = new Channel(emulatorHostAndPort, ChannelCredentials.Insecure);
PublisherServiceApiClient client = PublisherServiceApiClient.Create(channel);
client.CreateTopic(new TopicName("project", "topic"));
foreach (var topic in client.ListTopics(new ProjectName("project")))
{
Console.WriteLine(topic.Name);
}
// [END pubsub_use_emulator]
// End sample
}
public void RestLogging()
{
// Sample: RestLogging
// Required using directives:
// using static Google.Apis.Http.ConfigurableMessageHandler;
// using Google.Apis.Logging;
// using Google.Cloud.Translation.V2;
// Register a verbose console logger
ApplicationContext.RegisterLogger(new ConsoleLogger(LogLevel.All));
// Create a translation client
TranslationClient client = TranslationClient.Create();
// Configure which events the message handler will log.
client.Service.HttpClient.MessageHandler.LogEvents =
LogEventType.RequestHeaders | LogEventType.ResponseBody;
// Make the request
client.ListLanguages();
// End sample
}
public void ProtoRepeatedField1()
{
// Sample: ProtoRepeatedField1
// In normal code you'd populate these individual requests with more
// information.
AnnotateImageRequest request1 = new AnnotateImageRequest();
AnnotateImageRequest request2 = new AnnotateImageRequest();
// Create the batch request using an object initializer
BatchAnnotateImagesRequest batch = new BatchAnnotateImagesRequest
{
// Populate the repeated field with a collection initializer
Requests = { request1, request2 }
};
// End sample
}
public void ProtoRepeatedField2()
{
// Sample: ProtoRepeatedField2
// In normal code you'd populate these individual requests with more
// information.
AnnotateImageRequest request1 = new AnnotateImageRequest();
AnnotateImageRequest request2 = new AnnotateImageRequest();
// Populate the batch without using an object initializer, just by calling
// Add on the repeated field
BatchAnnotateImagesRequest batch = new BatchAnnotateImagesRequest();
batch.Requests.Add(request1);
batch.Requests.Add(request2);
// End sample
}
public void ProtoRepeatedField3()
{
// Sample: ProtoRepeatedField3
// In normal code you'd populate these individual requests with more
// information.
List<AnnotateImageRequest> requests = new List<AnnotateImageRequest>
{
new AnnotateImageRequest(),
new AnnotateImageRequest()
};
// Create the batch request using an object initializer
BatchAnnotateImagesRequest batch = new BatchAnnotateImagesRequest
{
// Populate the repeated field using the Add overload that accepts
// an IEnumerable<T>
Requests = { requests }
};
// End sample
}
public void ProtoMap1()
{
// Sample: ProtoMap1
HttpTarget target = new HttpTarget
{
Headers =
{
{ "X-Custom-Header1", "Value1" },
{ "X-Custom-Header2", "Value2" },
}
};
// End sample
}
public void ProtoMap2()
{
// Sample: ProtoMap2
HttpTarget target = new HttpTarget
{
Headers =
{
["X-Custom-Header1"] = "Value1",
["X-Custom-Header2"] = "Value2",
}
};
// End sample
}
public void ProtoMap3()
{
// Sample: ProtoMap3
HttpTarget target = new HttpTarget();
target.Headers["X-Custom-Header1"] = "Value1";
target.Headers["X-Custom-Header2"] = "Value2";
// End sample
}
}
}