Debian Codegraph @

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