Debian Codegraph @

talent/apiv4/event_client.go

raw
1 // Copyright 2025 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 // 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 // Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17 package talent
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27 "time"
28
29 longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb"
30 talentpb "cloud.google.com/go/talent/apiv4/talentpb"
31 gax "github.com/googleapis/gax-go/v2"
32 "google.golang.org/api/option"
33 "google.golang.org/api/option/internaloption"
34 gtransport "google.golang.org/api/transport/grpc"
35 httptransport "google.golang.org/api/transport/http"
36 "google.golang.org/grpc"
37 "google.golang.org/protobuf/encoding/protojson"
38 )
39
40 var newEventClientHook clientHook
41
42 // EventCallOptions contains the retry settings for each method of EventClient.
43 type EventCallOptions struct {
44 CreateClientEvent []gax.CallOption
45 GetOperation []gax.CallOption
46 }
47
48 func defaultEventGRPCClientOptions() []option.ClientOption {
49 return []option.ClientOption{
50 internaloption.WithDefaultEndpoint("jobs.googleapis.com:443"),
51 internaloption.WithDefaultEndpointTemplate("jobs.UNIVERSE_DOMAIN:443"),
52 internaloption.WithDefaultMTLSEndpoint("jobs.mtls.googleapis.com:443"),
53 internaloption.WithDefaultUniverseDomain("googleapis.com"),
54 internaloption.WithDefaultAudience("https://jobs.googleapis.com/"),
55 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
56 internaloption.EnableJwtWithScope(),
57 internaloption.EnableNewAuthLibrary(),
58 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
59 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
60 }
61 }
62
63 func defaultEventCallOptions() *EventCallOptions {
64 return &EventCallOptions{
65 CreateClientEvent: []gax.CallOption{
66 gax.WithTimeout(30000 * time.Millisecond),
67 },
68 GetOperation: []gax.CallOption{},
69 }
70 }
71
72 func defaultEventRESTCallOptions() *EventCallOptions {
73 return &EventCallOptions{
74 CreateClientEvent: []gax.CallOption{
75 gax.WithTimeout(30000 * time.Millisecond),
76 },
77 GetOperation: []gax.CallOption{},
78 }
79 }
80
81 // internalEventClient is an interface that defines the methods available from Cloud Talent Solution API.
82 type internalEventClient interface {
83 Close() error
84 setGoogleClientInfo(...string)
85 Connection() *grpc.ClientConn
86 CreateClientEvent(context.Context, *talentpb.CreateClientEventRequest, ...gax.CallOption) (*talentpb.ClientEvent, error)
87 GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error)
88 }
89
90 // EventClient is a client for interacting with Cloud Talent Solution API.
91 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
92 //
93 // A service handles client event report.
94 type EventClient struct {
95 // The internal transport-dependent client.
96 internalClient internalEventClient
97
98 // The call options for this service.
99 CallOptions *EventCallOptions
100 }
101
102 // Wrapper methods routed to the internal client.
103
104 // Close closes the connection to the API service. The user should invoke this when
105 // the client is no longer required.
106 func (c *EventClient) Close() error {
107 return c.internalClient.Close()
108 }
109
110 // setGoogleClientInfo sets the name and version of the application in
111 // the `x-goog-api-client` header passed on each request. Intended for
112 // use by Google-written clients.
113 func (c *EventClient) setGoogleClientInfo(keyval ...string) {
114 c.internalClient.setGoogleClientInfo(keyval...)
115 }
116
117 // Connection returns a connection to the API service.
118 //
119 // Deprecated: Connections are now pooled so this method does not always
120 // return the same resource.
121 func (c *EventClient) Connection() *grpc.ClientConn {
122 return c.internalClient.Connection()
123 }
124
125 // CreateClientEvent report events issued when end user interacts with customer’s application
126 // that uses Cloud Talent Solution. You may inspect the created events in
127 // self service
128 // tools (at https://console.cloud.google.com/talent-solution/overview).
129 // Learn
130 // more (at https://cloud.google.com/talent-solution/docs/management-tools)
131 // about self service tools.
132 func (c *EventClient) CreateClientEvent(ctx context.Context, req *talentpb.CreateClientEventRequest, opts ...gax.CallOption) (*talentpb.ClientEvent, error) {
133 return c.internalClient.CreateClientEvent(ctx, req, opts...)
134 }
135
136 // GetOperation is a utility method from google.longrunning.Operations.
137 func (c *EventClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
138 return c.internalClient.GetOperation(ctx, req, opts...)
139 }
140
141 // eventGRPCClient is a client for interacting with Cloud Talent Solution API over gRPC transport.
142 //
143 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
144 type eventGRPCClient struct {
145 // Connection pool of gRPC connections to the service.
146 connPool gtransport.ConnPool
147
148 // Points back to the CallOptions field of the containing EventClient
149 CallOptions **EventCallOptions
150
151 // The gRPC API client.
152 eventClient talentpb.EventServiceClient
153
154 operationsClient longrunningpb.OperationsClient
155
156 // The x-goog-* metadata to be sent with each request.
157 xGoogHeaders []string
158
159 logger *slog.Logger
160 }
161
162 // NewEventClient creates a new event service client based on gRPC.
163 // The returned client must be Closed when it is done being used to clean up its underlying connections.
164 //
165 // A service handles client event report.
166 func NewEventClient(ctx context.Context, opts ...option.ClientOption) (*EventClient, error) {
167 clientOpts := defaultEventGRPCClientOptions()
168 if newEventClientHook != nil {
169 hookOpts, err := newEventClientHook(ctx, clientHookParams{})
170 if err != nil {
171 return nil, err
172 }
173 clientOpts = append(clientOpts, hookOpts...)
174 }
175
176 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
177 if err != nil {
178 return nil, err
179 }
180 client := EventClient{CallOptions: defaultEventCallOptions()}
181
182 c := &eventGRPCClient{
183 connPool: connPool,
184 eventClient: talentpb.NewEventServiceClient(connPool),
185 CallOptions: &client.CallOptions,
186 logger: internaloption.GetLogger(opts),
187 operationsClient: longrunningpb.NewOperationsClient(connPool),
188 }
189 c.setGoogleClientInfo()
190
191 client.internalClient = c
192
193 return &client, nil
194 }
195
196 // Connection returns a connection to the API service.
197 //
198 // Deprecated: Connections are now pooled so this method does not always
199 // return the same resource.
200 func (c *eventGRPCClient) Connection() *grpc.ClientConn {
201 return c.connPool.Conn()
202 }
203
204 // setGoogleClientInfo sets the name and version of the application in
205 // the `x-goog-api-client` header passed on each request. Intended for
206 // use by Google-written clients.
207 func (c *eventGRPCClient) setGoogleClientInfo(keyval ...string) {
208 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
209 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
210 c.xGoogHeaders = []string{
211 "x-goog-api-client", gax.XGoogHeader(kv...),
212 }
213 }
214
215 // Close closes the connection to the API service. The user should invoke this when
216 // the client is no longer required.
217 func (c *eventGRPCClient) Close() error {
218 return c.connPool.Close()
219 }
220
221 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
222 type eventRESTClient struct {
223 // The http endpoint to connect to.
224 endpoint string
225
226 // The http client.
227 httpClient *http.Client
228
229 // The x-goog-* headers to be sent with each request.
230 xGoogHeaders []string
231
232 // Points back to the CallOptions field of the containing EventClient
233 CallOptions **EventCallOptions
234
235 logger *slog.Logger
236 }
237
238 // NewEventRESTClient creates a new event service rest client.
239 //
240 // A service handles client event report.
241 func NewEventRESTClient(ctx context.Context, opts ...option.ClientOption) (*EventClient, error) {
242 clientOpts := append(defaultEventRESTClientOptions(), opts...)
243 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
244 if err != nil {
245 return nil, err
246 }
247
248 callOpts := defaultEventRESTCallOptions()
249 c := &eventRESTClient{
250 endpoint: endpoint,
251 httpClient: httpClient,
252 CallOptions: &callOpts,
253 logger: internaloption.GetLogger(opts),
254 }
255 c.setGoogleClientInfo()
256
257 return &EventClient{internalClient: c, CallOptions: callOpts}, nil
258 }
259
260 func defaultEventRESTClientOptions() []option.ClientOption {
261 return []option.ClientOption{
262 internaloption.WithDefaultEndpoint("https://jobs.googleapis.com"),
263 internaloption.WithDefaultEndpointTemplate("https://jobs.UNIVERSE_DOMAIN"),
264 internaloption.WithDefaultMTLSEndpoint("https://jobs.mtls.googleapis.com"),
265 internaloption.WithDefaultUniverseDomain("googleapis.com"),
266 internaloption.WithDefaultAudience("https://jobs.googleapis.com/"),
267 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
268 internaloption.EnableNewAuthLibrary(),
269 }
270 }
271
272 // setGoogleClientInfo sets the name and version of the application in
273 // the `x-goog-api-client` header passed on each request. Intended for
274 // use by Google-written clients.
275 func (c *eventRESTClient) setGoogleClientInfo(keyval ...string) {
276 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
277 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
278 c.xGoogHeaders = []string{
279 "x-goog-api-client", gax.XGoogHeader(kv...),
280 }
281 }
282
283 // Close closes the connection to the API service. The user should invoke this when
284 // the client is no longer required.
285 func (c *eventRESTClient) Close() error {
286 // Replace httpClient with nil to force cleanup.
287 c.httpClient = nil
288 return nil
289 }
290
291 // Connection returns a connection to the API service.
292 //
293 // Deprecated: This method always returns nil.
294 func (c *eventRESTClient) Connection() *grpc.ClientConn {
295 return nil
296 }
297 func (c *eventGRPCClient) CreateClientEvent(ctx context.Context, req *talentpb.CreateClientEventRequest, opts ...gax.CallOption) (*talentpb.ClientEvent, error) {
298 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
299
300 hds = append(c.xGoogHeaders, hds...)
301 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
302 opts = append((*c.CallOptions).CreateClientEvent[0:len((*c.CallOptions).CreateClientEvent):len((*c.CallOptions).CreateClientEvent)], opts...)
303 var resp *talentpb.ClientEvent
304 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
305 var err error
306 resp, err = executeRPC(ctx, c.eventClient.CreateClientEvent, req, settings.GRPC, c.logger, "CreateClientEvent")
307 return err
308 }, opts...)
309 if err != nil {
310 return nil, err
311 }
312 return resp, nil
313 }
314
315 func (c *eventGRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
316 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
317
318 hds = append(c.xGoogHeaders, hds...)
319 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
320 opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
321 var resp *longrunningpb.Operation
322 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
323 var err error
324 resp, err = executeRPC(ctx, c.operationsClient.GetOperation, req, settings.GRPC, c.logger, "GetOperation")
325 return err
326 }, opts...)
327 if err != nil {
328 return nil, err
329 }
330 return resp, nil
331 }
332
333 // CreateClientEvent report events issued when end user interacts with customer’s application
334 // that uses Cloud Talent Solution. You may inspect the created events in
335 // self service
336 // tools (at https://console.cloud.google.com/talent-solution/overview).
337 // Learn
338 // more (at https://cloud.google.com/talent-solution/docs/management-tools)
339 // about self service tools.
340 func (c *eventRESTClient) CreateClientEvent(ctx context.Context, req *talentpb.CreateClientEventRequest, opts ...gax.CallOption) (*talentpb.ClientEvent, error) {
341 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
342 body := req.GetClientEvent()
343 jsonReq, err := m.Marshal(body)
344 if err != nil {
345 return nil, err
346 }
347
348 baseUrl, err := url.Parse(c.endpoint)
349 if err != nil {
350 return nil, err
351 }
352 baseUrl.Path += fmt.Sprintf("/v4/%v/clientEvents", req.GetParent())
353
354 params := url.Values{}
355 params.Add("$alt", "json;enum-encoding=int")
356
357 baseUrl.RawQuery = params.Encode()
358
359 // Build HTTP headers from client and context metadata.
360 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
361
362 hds = append(c.xGoogHeaders, hds...)
363 hds = append(hds, "Content-Type", "application/json")
364 headers := gax.BuildHeaders(ctx, hds...)
365 opts = append((*c.CallOptions).CreateClientEvent[0:len((*c.CallOptions).CreateClientEvent):len((*c.CallOptions).CreateClientEvent)], opts...)
366 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
367 resp := &talentpb.ClientEvent{}
368 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
369 if settings.Path != "" {
370 baseUrl.Path = settings.Path
371 }
372 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
373 if err != nil {
374 return err
375 }
376 httpReq = httpReq.WithContext(ctx)
377 httpReq.Header = headers
378
379 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "CreateClientEvent")
380 if err != nil {
381 return err
382 }
383
384 if err := unm.Unmarshal(buf, resp); err != nil {
385 return err
386 }
387
388 return nil
389 }, opts...)
390 if e != nil {
391 return nil, e
392 }
393 return resp, nil
394 }
395
396 // GetOperation is a utility method from google.longrunning.Operations.
397 func (c *eventRESTClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
398 baseUrl, err := url.Parse(c.endpoint)
399 if err != nil {
400 return nil, err
401 }
402 baseUrl.Path += fmt.Sprintf("/v4/%v", req.GetName())
403
404 params := url.Values{}
405 params.Add("$alt", "json;enum-encoding=int")
406
407 baseUrl.RawQuery = params.Encode()
408
409 // Build HTTP headers from client and context metadata.
410 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
411
412 hds = append(c.xGoogHeaders, hds...)
413 hds = append(hds, "Content-Type", "application/json")
414 headers := gax.BuildHeaders(ctx, hds...)
415 opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
416 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
417 resp := &longrunningpb.Operation{}
418 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
419 if settings.Path != "" {
420 baseUrl.Path = settings.Path
421 }
422 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
423 if err != nil {
424 return err
425 }
426 httpReq = httpReq.WithContext(ctx)
427 httpReq.Header = headers
428
429 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetOperation")
430 if err != nil {
431 return err
432 }
433
434 if err := unm.Unmarshal(buf, resp); err != nil {
435 return err
436 }
437
438 return nil
439 }, opts...)
440 if e != nil {
441 return nil, e
442 }
443 return resp, nil
444 }