Debian Codegraph @

servicedirectory/apiv1beta1/lookup_client.go

raw symbols SCIP
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 servicedirectory
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27 "time"
28
29 servicedirectorypb "cloud.google.com/go/servicedirectory/apiv1beta1/servicedirectorypb"
30 gax "github.com/googleapis/gax-go/v2"
31 "google.golang.org/api/iterator"
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 locationpb "google.golang.org/genproto/googleapis/cloud/location"
37 "google.golang.org/grpc"
38 "google.golang.org/grpc/codes"
39 "google.golang.org/protobuf/encoding/protojson"
40 "google.golang.org/protobuf/proto"
41 )
42
43 var newLookupClientHook clientHook
44
45 // LookupCallOptions contains the retry settings for each method of LookupClient.
46 type LookupCallOptions struct {
47 ResolveService []gax.CallOption
48 GetLocation []gax.CallOption
49 ListLocations []gax.CallOption
50 }
51
52 func defaultLookupGRPCClientOptions() []option.ClientOption {
53 return []option.ClientOption{
54 internaloption.WithDefaultEndpoint("servicedirectory.googleapis.com:443"),
55 internaloption.WithDefaultEndpointTemplate("servicedirectory.UNIVERSE_DOMAIN:443"),
56 internaloption.WithDefaultMTLSEndpoint("servicedirectory.mtls.googleapis.com:443"),
57 internaloption.WithDefaultUniverseDomain("googleapis.com"),
58 internaloption.WithDefaultAudience("https://servicedirectory.googleapis.com/"),
59 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
60 internaloption.EnableJwtWithScope(),
61 internaloption.EnableNewAuthLibrary(),
62 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
63 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
64 }
65 }
66
67 func defaultLookupCallOptions() *LookupCallOptions {
68 return &LookupCallOptions{
69 ResolveService: []gax.CallOption{
70 gax.WithTimeout(15000 * time.Millisecond),
71 gax.WithRetry(func() gax.Retryer {
72 return gax.OnCodes([]codes.Code{
73 codes.Unavailable,
74 codes.Unknown,
75 }, gax.Backoff{
76 Initial: 1000 * time.Millisecond,
77 Max: 60000 * time.Millisecond,
78 Multiplier: 1.30,
79 })
80 }),
81 },
82 GetLocation: []gax.CallOption{},
83 ListLocations: []gax.CallOption{},
84 }
85 }
86
87 func defaultLookupRESTCallOptions() *LookupCallOptions {
88 return &LookupCallOptions{
89 ResolveService: []gax.CallOption{
90 gax.WithTimeout(15000 * time.Millisecond),
91 gax.WithRetry(func() gax.Retryer {
92 return gax.OnHTTPCodes(gax.Backoff{
93 Initial: 1000 * time.Millisecond,
94 Max: 60000 * time.Millisecond,
95 Multiplier: 1.30,
96 },
97 http.StatusServiceUnavailable,
98 http.StatusInternalServerError)
99 }),
100 },
101 GetLocation: []gax.CallOption{},
102 ListLocations: []gax.CallOption{},
103 }
104 }
105
106 // internalLookupClient is an interface that defines the methods available from Service Directory API.
107 type internalLookupClient interface {
108 Close() error
109 setGoogleClientInfo(...string)
110 Connection() *grpc.ClientConn
111 ResolveService(context.Context, *servicedirectorypb.ResolveServiceRequest, ...gax.CallOption) (*servicedirectorypb.ResolveServiceResponse, error)
112 GetLocation(context.Context, *locationpb.GetLocationRequest, ...gax.CallOption) (*locationpb.Location, error)
113 ListLocations(context.Context, *locationpb.ListLocationsRequest, ...gax.CallOption) *LocationIterator
114 }
115
116 // LookupClient is a client for interacting with Service Directory API.
117 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
118 //
119 // Service Directory API for looking up service data at runtime.
120 type LookupClient struct {
121 // The internal transport-dependent client.
122 internalClient internalLookupClient
123
124 // The call options for this service.
125 CallOptions *LookupCallOptions
126 }
127
128 // Wrapper methods routed to the internal client.
129
130 // Close closes the connection to the API service. The user should invoke this when
131 // the client is no longer required.
132 func (c *LookupClient) Close() error {
133 return c.internalClient.Close()
134 }
135
136 // setGoogleClientInfo sets the name and version of the application in
137 // the `x-goog-api-client` header passed on each request. Intended for
138 // use by Google-written clients.
139 func (c *LookupClient) setGoogleClientInfo(keyval ...string) {
140 c.internalClient.setGoogleClientInfo(keyval...)
141 }
142
143 // Connection returns a connection to the API service.
144 //
145 // Deprecated: Connections are now pooled so this method does not always
146 // return the same resource.
147 func (c *LookupClient) Connection() *grpc.ClientConn {
148 return c.internalClient.Connection()
149 }
150
151 // ResolveService returns a service and its
152 // associated endpoints.
153 // Resolving a service is not considered an active developer method.
154 func (c *LookupClient) ResolveService(ctx context.Context, req *servicedirectorypb.ResolveServiceRequest, opts ...gax.CallOption) (*servicedirectorypb.ResolveServiceResponse, error) {
155 return c.internalClient.ResolveService(ctx, req, opts...)
156 }
157
158 // GetLocation gets information about a location.
159 func (c *LookupClient) GetLocation(ctx context.Context, req *locationpb.GetLocationRequest, opts ...gax.CallOption) (*locationpb.Location, error) {
160 return c.internalClient.GetLocation(ctx, req, opts...)
161 }
162
163 // ListLocations lists information about the supported locations for this service.
164 func (c *LookupClient) ListLocations(ctx context.Context, req *locationpb.ListLocationsRequest, opts ...gax.CallOption) *LocationIterator {
165 return c.internalClient.ListLocations(ctx, req, opts...)
166 }
167
168 // lookupGRPCClient is a client for interacting with Service Directory API over gRPC transport.
169 //
170 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
171 type lookupGRPCClient struct {
172 // Connection pool of gRPC connections to the service.
173 connPool gtransport.ConnPool
174
175 // Points back to the CallOptions field of the containing LookupClient
176 CallOptions **LookupCallOptions
177
178 // The gRPC API client.
179 lookupClient servicedirectorypb.LookupServiceClient
180
181 locationsClient locationpb.LocationsClient
182
183 // The x-goog-* metadata to be sent with each request.
184 xGoogHeaders []string
185
186 logger *slog.Logger
187 }
188
189 // NewLookupClient creates a new lookup service client based on gRPC.
190 // The returned client must be Closed when it is done being used to clean up its underlying connections.
191 //
192 // Service Directory API for looking up service data at runtime.
193 func NewLookupClient(ctx context.Context, opts ...option.ClientOption) (*LookupClient, error) {
194 clientOpts := defaultLookupGRPCClientOptions()
195 if newLookupClientHook != nil {
196 hookOpts, err := newLookupClientHook(ctx, clientHookParams{})
197 if err != nil {
198 return nil, err
199 }
200 clientOpts = append(clientOpts, hookOpts...)
201 }
202
203 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
204 if err != nil {
205 return nil, err
206 }
207 client := LookupClient{CallOptions: defaultLookupCallOptions()}
208
209 c := &lookupGRPCClient{
210 connPool: connPool,
211 lookupClient: servicedirectorypb.NewLookupServiceClient(connPool),
212 CallOptions: &client.CallOptions,
213 logger: internaloption.GetLogger(opts),
214 locationsClient: locationpb.NewLocationsClient(connPool),
215 }
216 c.setGoogleClientInfo()
217
218 client.internalClient = c
219
220 return &client, nil
221 }
222
223 // Connection returns a connection to the API service.
224 //
225 // Deprecated: Connections are now pooled so this method does not always
226 // return the same resource.
227 func (c *lookupGRPCClient) Connection() *grpc.ClientConn {
228 return c.connPool.Conn()
229 }
230
231 // setGoogleClientInfo sets the name and version of the application in
232 // the `x-goog-api-client` header passed on each request. Intended for
233 // use by Google-written clients.
234 func (c *lookupGRPCClient) setGoogleClientInfo(keyval ...string) {
235 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
236 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
237 c.xGoogHeaders = []string{
238 "x-goog-api-client", gax.XGoogHeader(kv...),
239 }
240 }
241
242 // Close closes the connection to the API service. The user should invoke this when
243 // the client is no longer required.
244 func (c *lookupGRPCClient) Close() error {
245 return c.connPool.Close()
246 }
247
248 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
249 type lookupRESTClient struct {
250 // The http endpoint to connect to.
251 endpoint string
252
253 // The http client.
254 httpClient *http.Client
255
256 // The x-goog-* headers to be sent with each request.
257 xGoogHeaders []string
258
259 // Points back to the CallOptions field of the containing LookupClient
260 CallOptions **LookupCallOptions
261
262 logger *slog.Logger
263 }
264
265 // NewLookupRESTClient creates a new lookup service rest client.
266 //
267 // Service Directory API for looking up service data at runtime.
268 func NewLookupRESTClient(ctx context.Context, opts ...option.ClientOption) (*LookupClient, error) {
269 clientOpts := append(defaultLookupRESTClientOptions(), opts...)
270 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
271 if err != nil {
272 return nil, err
273 }
274
275 callOpts := defaultLookupRESTCallOptions()
276 c := &lookupRESTClient{
277 endpoint: endpoint,
278 httpClient: httpClient,
279 CallOptions: &callOpts,
280 logger: internaloption.GetLogger(opts),
281 }
282 c.setGoogleClientInfo()
283
284 return &LookupClient{internalClient: c, CallOptions: callOpts}, nil
285 }
286
287 func defaultLookupRESTClientOptions() []option.ClientOption {
288 return []option.ClientOption{
289 internaloption.WithDefaultEndpoint("https://servicedirectory.googleapis.com"),
290 internaloption.WithDefaultEndpointTemplate("https://servicedirectory.UNIVERSE_DOMAIN"),
291 internaloption.WithDefaultMTLSEndpoint("https://servicedirectory.mtls.googleapis.com"),
292 internaloption.WithDefaultUniverseDomain("googleapis.com"),
293 internaloption.WithDefaultAudience("https://servicedirectory.googleapis.com/"),
294 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
295 internaloption.EnableNewAuthLibrary(),
296 }
297 }
298
299 // setGoogleClientInfo sets the name and version of the application in
300 // the `x-goog-api-client` header passed on each request. Intended for
301 // use by Google-written clients.
302 func (c *lookupRESTClient) setGoogleClientInfo(keyval ...string) {
303 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
304 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
305 c.xGoogHeaders = []string{
306 "x-goog-api-client", gax.XGoogHeader(kv...),
307 }
308 }
309
310 // Close closes the connection to the API service. The user should invoke this when
311 // the client is no longer required.
312 func (c *lookupRESTClient) Close() error {
313 // Replace httpClient with nil to force cleanup.
314 c.httpClient = nil
315 return nil
316 }
317
318 // Connection returns a connection to the API service.
319 //
320 // Deprecated: This method always returns nil.
321 func (c *lookupRESTClient) Connection() *grpc.ClientConn {
322 return nil
323 }
324 func (c *lookupGRPCClient) ResolveService(ctx context.Context, req *servicedirectorypb.ResolveServiceRequest, opts ...gax.CallOption) (*servicedirectorypb.ResolveServiceResponse, error) {
325 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
326
327 hds = append(c.xGoogHeaders, hds...)
328 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
329 opts = append((*c.CallOptions).ResolveService[0:len((*c.CallOptions).ResolveService):len((*c.CallOptions).ResolveService)], opts...)
330 var resp *servicedirectorypb.ResolveServiceResponse
331 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
332 var err error
333 resp, err = executeRPC(ctx, c.lookupClient.ResolveService, req, settings.GRPC, c.logger, "ResolveService")
334 return err
335 }, opts...)
336 if err != nil {
337 return nil, err
338 }
339 return resp, nil
340 }
341
342 func (c *lookupGRPCClient) GetLocation(ctx context.Context, req *locationpb.GetLocationRequest, opts ...gax.CallOption) (*locationpb.Location, error) {
343 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
344
345 hds = append(c.xGoogHeaders, hds...)
346 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
347 opts = append((*c.CallOptions).GetLocation[0:len((*c.CallOptions).GetLocation):len((*c.CallOptions).GetLocation)], opts...)
348 var resp *locationpb.Location
349 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
350 var err error
351 resp, err = executeRPC(ctx, c.locationsClient.GetLocation, req, settings.GRPC, c.logger, "GetLocation")
352 return err
353 }, opts...)
354 if err != nil {
355 return nil, err
356 }
357 return resp, nil
358 }
359
360 func (c *lookupGRPCClient) ListLocations(ctx context.Context, req *locationpb.ListLocationsRequest, opts ...gax.CallOption) *LocationIterator {
361 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
362
363 hds = append(c.xGoogHeaders, hds...)
364 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
365 opts = append((*c.CallOptions).ListLocations[0:len((*c.CallOptions).ListLocations):len((*c.CallOptions).ListLocations)], opts...)
366 it := &LocationIterator{}
367 req = proto.Clone(req).(*locationpb.ListLocationsRequest)
368 it.InternalFetch = func(pageSize int, pageToken string) ([]*locationpb.Location, string, error) {
369 resp := &locationpb.ListLocationsResponse{}
370 if pageToken != "" {
371 req.PageToken = pageToken
372 }
373 if pageSize > math.MaxInt32 {
374 req.PageSize = math.MaxInt32
375 } else if pageSize != 0 {
376 req.PageSize = int32(pageSize)
377 }
378 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
379 var err error
380 resp, err = executeRPC(ctx, c.locationsClient.ListLocations, req, settings.GRPC, c.logger, "ListLocations")
381 return err
382 }, opts...)
383 if err != nil {
384 return nil, "", err
385 }
386
387 it.Response = resp
388 return resp.GetLocations(), resp.GetNextPageToken(), nil
389 }
390 fetch := func(pageSize int, pageToken string) (string, error) {
391 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
392 if err != nil {
393 return "", err
394 }
395 it.items = append(it.items, items...)
396 return nextPageToken, nil
397 }
398
399 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
400 it.pageInfo.MaxSize = int(req.GetPageSize())
401 it.pageInfo.Token = req.GetPageToken()
402
403 return it
404 }
405
406 // ResolveService returns a service and its
407 // associated endpoints.
408 // Resolving a service is not considered an active developer method.
409 func (c *lookupRESTClient) ResolveService(ctx context.Context, req *servicedirectorypb.ResolveServiceRequest, opts ...gax.CallOption) (*servicedirectorypb.ResolveServiceResponse, error) {
410 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
411 jsonReq, err := m.Marshal(req)
412 if err != nil {
413 return nil, err
414 }
415
416 baseUrl, err := url.Parse(c.endpoint)
417 if err != nil {
418 return nil, err
419 }
420 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:resolve", 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).ResolveService[0:len((*c.CallOptions).ResolveService):len((*c.CallOptions).ResolveService)], opts...)
434 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
435 resp := &servicedirectorypb.ResolveServiceResponse{}
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("POST", baseUrl.String(), bytes.NewReader(jsonReq))
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, jsonReq, "ResolveService")
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 }
463
464 // GetLocation gets information about a location.
465 func (c *lookupRESTClient) GetLocation(ctx context.Context, req *locationpb.GetLocationRequest, opts ...gax.CallOption) (*locationpb.Location, error) {
466 baseUrl, err := url.Parse(c.endpoint)
467 if err != nil {
468 return nil, err
469 }
470 baseUrl.Path += fmt.Sprintf("/v1beta1/%v", req.GetName())
471
472 params := url.Values{}
473 params.Add("$alt", "json;enum-encoding=int")
474
475 baseUrl.RawQuery = params.Encode()
476
477 // Build HTTP headers from client and context metadata.
478 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
479
480 hds = append(c.xGoogHeaders, hds...)
481 hds = append(hds, "Content-Type", "application/json")
482 headers := gax.BuildHeaders(ctx, hds...)
483 opts = append((*c.CallOptions).GetLocation[0:len((*c.CallOptions).GetLocation):len((*c.CallOptions).GetLocation)], opts...)
484 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
485 resp := &locationpb.Location{}
486 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
487 if settings.Path != "" {
488 baseUrl.Path = settings.Path
489 }
490 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
491 if err != nil {
492 return err
493 }
494 httpReq = httpReq.WithContext(ctx)
495 httpReq.Header = headers
496
497 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetLocation")
498 if err != nil {
499 return err
500 }
501
502 if err := unm.Unmarshal(buf, resp); err != nil {
503 return err
504 }
505
506 return nil
507 }, opts...)
508 if e != nil {
509 return nil, e
510 }
511 return resp, nil
512 }
513
514 // ListLocations lists information about the supported locations for this service.
515 func (c *lookupRESTClient) ListLocations(ctx context.Context, req *locationpb.ListLocationsRequest, opts ...gax.CallOption) *LocationIterator {
516 it := &LocationIterator{}
517 req = proto.Clone(req).(*locationpb.ListLocationsRequest)
518 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
519 it.InternalFetch = func(pageSize int, pageToken string) ([]*locationpb.Location, string, error) {
520 resp := &locationpb.ListLocationsResponse{}
521 if pageToken != "" {
522 req.PageToken = pageToken
523 }
524 if pageSize > math.MaxInt32 {
525 req.PageSize = math.MaxInt32
526 } else if pageSize != 0 {
527 req.PageSize = int32(pageSize)
528 }
529 baseUrl, err := url.Parse(c.endpoint)
530 if err != nil {
531 return nil, "", err
532 }
533 baseUrl.Path += fmt.Sprintf("/v1beta1/%v/locations", req.GetName())
534
535 params := url.Values{}
536 params.Add("$alt", "json;enum-encoding=int")
537 if req.GetFilter() != "" {
538 params.Add("filter", fmt.Sprintf("%v", req.GetFilter()))
539 }
540 if req.GetPageSize() != 0 {
541 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
542 }
543 if req.GetPageToken() != "" {
544 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
545 }
546
547 baseUrl.RawQuery = params.Encode()
548
549 // Build HTTP headers from client and context metadata.
550 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
551 headers := gax.BuildHeaders(ctx, hds...)
552 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
553 if settings.Path != "" {
554 baseUrl.Path = settings.Path
555 }
556 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
557 if err != nil {
558 return err
559 }
560 httpReq.Header = headers
561
562 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListLocations")
563 if err != nil {
564 return err
565 }
566 if err := unm.Unmarshal(buf, resp); err != nil {
567 return err
568 }
569
570 return nil
571 }, opts...)
572 if e != nil {
573 return nil, "", e
574 }
575 it.Response = resp
576 return resp.GetLocations(), resp.GetNextPageToken(), nil
577 }
578
579 fetch := func(pageSize int, pageToken string) (string, error) {
580 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
581 if err != nil {
582 return "", err
583 }
584 it.items = append(it.items, items...)
585 return nextPageToken, nil
586 }
587
588 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
589 it.pageInfo.MaxSize = int(req.GetPageSize())
590 it.pageInfo.Token = req.GetPageToken()
591
592 return it
593 }