Debian Codegraph @

maps/places/apiv1/places_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 places
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27
28 placespb "cloud.google.com/go/maps/places/apiv1/placespb"
29 gax "github.com/googleapis/gax-go/v2"
30 "google.golang.org/api/option"
31 "google.golang.org/api/option/internaloption"
32 gtransport "google.golang.org/api/transport/grpc"
33 httptransport "google.golang.org/api/transport/http"
34 "google.golang.org/grpc"
35 "google.golang.org/protobuf/encoding/protojson"
36 )
37
38 var newClientHook clientHook
39
40 // CallOptions contains the retry settings for each method of Client.
41 type CallOptions struct {
42 SearchNearby []gax.CallOption
43 SearchText []gax.CallOption
44 GetPhotoMedia []gax.CallOption
45 GetPlace []gax.CallOption
46 AutocompletePlaces []gax.CallOption
47 }
48
49 func defaultGRPCClientOptions() []option.ClientOption {
50 return []option.ClientOption{
51 internaloption.WithDefaultEndpoint("places.googleapis.com:443"),
52 internaloption.WithDefaultEndpointTemplate("places.UNIVERSE_DOMAIN:443"),
53 internaloption.WithDefaultMTLSEndpoint("places.mtls.googleapis.com:443"),
54 internaloption.WithDefaultUniverseDomain("googleapis.com"),
55 internaloption.WithDefaultAudience("https://places.googleapis.com/"),
56 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
57 internaloption.EnableJwtWithScope(),
58 internaloption.EnableNewAuthLibrary(),
59 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
60 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
61 }
62 }
63
64 func defaultCallOptions() *CallOptions {
65 return &CallOptions{
66 SearchNearby: []gax.CallOption{},
67 SearchText: []gax.CallOption{},
68 GetPhotoMedia: []gax.CallOption{},
69 GetPlace: []gax.CallOption{},
70 AutocompletePlaces: []gax.CallOption{},
71 }
72 }
73
74 func defaultRESTCallOptions() *CallOptions {
75 return &CallOptions{
76 SearchNearby: []gax.CallOption{},
77 SearchText: []gax.CallOption{},
78 GetPhotoMedia: []gax.CallOption{},
79 GetPlace: []gax.CallOption{},
80 AutocompletePlaces: []gax.CallOption{},
81 }
82 }
83
84 // internalClient is an interface that defines the methods available from Places API (New).
85 type internalClient interface {
86 Close() error
87 setGoogleClientInfo(...string)
88 Connection() *grpc.ClientConn
89 SearchNearby(context.Context, *placespb.SearchNearbyRequest, ...gax.CallOption) (*placespb.SearchNearbyResponse, error)
90 SearchText(context.Context, *placespb.SearchTextRequest, ...gax.CallOption) (*placespb.SearchTextResponse, error)
91 GetPhotoMedia(context.Context, *placespb.GetPhotoMediaRequest, ...gax.CallOption) (*placespb.PhotoMedia, error)
92 GetPlace(context.Context, *placespb.GetPlaceRequest, ...gax.CallOption) (*placespb.Place, error)
93 AutocompletePlaces(context.Context, *placespb.AutocompletePlacesRequest, ...gax.CallOption) (*placespb.AutocompletePlacesResponse, error)
94 }
95
96 // Client is a client for interacting with Places API (New).
97 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
98 //
99 // Service definition for the Places API.
100 // Note: every request (except for Autocomplete and GetPhotoMedia requests)
101 // requires a field mask set outside of the request proto (all/*, is not
102 // assumed). The field mask can be set via the HTTP header X-Goog-FieldMask.
103 // See:
104 // https://developers.google.com/maps/documentation/places/web-service/choose-fields (at https://developers.google.com/maps/documentation/places/web-service/choose-fields)
105 type Client struct {
106 // The internal transport-dependent client.
107 internalClient internalClient
108
109 // The call options for this service.
110 CallOptions *CallOptions
111 }
112
113 // Wrapper methods routed to the internal client.
114
115 // Close closes the connection to the API service. The user should invoke this when
116 // the client is no longer required.
117 func (c *Client) Close() error {
118 return c.internalClient.Close()
119 }
120
121 // setGoogleClientInfo sets the name and version of the application in
122 // the `x-goog-api-client` header passed on each request. Intended for
123 // use by Google-written clients.
124 func (c *Client) setGoogleClientInfo(keyval ...string) {
125 c.internalClient.setGoogleClientInfo(keyval...)
126 }
127
128 // Connection returns a connection to the API service.
129 //
130 // Deprecated: Connections are now pooled so this method does not always
131 // return the same resource.
132 func (c *Client) Connection() *grpc.ClientConn {
133 return c.internalClient.Connection()
134 }
135
136 // SearchNearby search for places near locations.
137 func (c *Client) SearchNearby(ctx context.Context, req *placespb.SearchNearbyRequest, opts ...gax.CallOption) (*placespb.SearchNearbyResponse, error) {
138 return c.internalClient.SearchNearby(ctx, req, opts...)
139 }
140
141 // SearchText text query based place search.
142 func (c *Client) SearchText(ctx context.Context, req *placespb.SearchTextRequest, opts ...gax.CallOption) (*placespb.SearchTextResponse, error) {
143 return c.internalClient.SearchText(ctx, req, opts...)
144 }
145
146 // GetPhotoMedia get a photo media with a photo reference string.
147 func (c *Client) GetPhotoMedia(ctx context.Context, req *placespb.GetPhotoMediaRequest, opts ...gax.CallOption) (*placespb.PhotoMedia, error) {
148 return c.internalClient.GetPhotoMedia(ctx, req, opts...)
149 }
150
151 // GetPlace get the details of a place based on its resource name, which is a string
152 // in the places/{place_id} format.
153 func (c *Client) GetPlace(ctx context.Context, req *placespb.GetPlaceRequest, opts ...gax.CallOption) (*placespb.Place, error) {
154 return c.internalClient.GetPlace(ctx, req, opts...)
155 }
156
157 // AutocompletePlaces returns predictions for the given input.
158 func (c *Client) AutocompletePlaces(ctx context.Context, req *placespb.AutocompletePlacesRequest, opts ...gax.CallOption) (*placespb.AutocompletePlacesResponse, error) {
159 return c.internalClient.AutocompletePlaces(ctx, req, opts...)
160 }
161
162 // gRPCClient is a client for interacting with Places API (New) over gRPC transport.
163 //
164 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
165 type gRPCClient struct {
166 // Connection pool of gRPC connections to the service.
167 connPool gtransport.ConnPool
168
169 // Points back to the CallOptions field of the containing Client
170 CallOptions **CallOptions
171
172 // The gRPC API client.
173 client placespb.PlacesClient
174
175 // The x-goog-* metadata to be sent with each request.
176 xGoogHeaders []string
177
178 logger *slog.Logger
179 }
180
181 // NewClient creates a new places client based on gRPC.
182 // The returned client must be Closed when it is done being used to clean up its underlying connections.
183 //
184 // Service definition for the Places API.
185 // Note: every request (except for Autocomplete and GetPhotoMedia requests)
186 // requires a field mask set outside of the request proto (all/*, is not
187 // assumed). The field mask can be set via the HTTP header X-Goog-FieldMask.
188 // See:
189 // https://developers.google.com/maps/documentation/places/web-service/choose-fields (at https://developers.google.com/maps/documentation/places/web-service/choose-fields)
190 func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
191 clientOpts := defaultGRPCClientOptions()
192 if newClientHook != nil {
193 hookOpts, err := newClientHook(ctx, clientHookParams{})
194 if err != nil {
195 return nil, err
196 }
197 clientOpts = append(clientOpts, hookOpts...)
198 }
199
200 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
201 if err != nil {
202 return nil, err
203 }
204 client := Client{CallOptions: defaultCallOptions()}
205
206 c := &gRPCClient{
207 connPool: connPool,
208 client: placespb.NewPlacesClient(connPool),
209 CallOptions: &client.CallOptions,
210 logger: internaloption.GetLogger(opts),
211 }
212 c.setGoogleClientInfo()
213
214 client.internalClient = c
215
216 return &client, nil
217 }
218
219 // Connection returns a connection to the API service.
220 //
221 // Deprecated: Connections are now pooled so this method does not always
222 // return the same resource.
223 func (c *gRPCClient) Connection() *grpc.ClientConn {
224 return c.connPool.Conn()
225 }
226
227 // setGoogleClientInfo sets the name and version of the application in
228 // the `x-goog-api-client` header passed on each request. Intended for
229 // use by Google-written clients.
230 func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
231 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
232 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
233 c.xGoogHeaders = []string{
234 "x-goog-api-client", gax.XGoogHeader(kv...),
235 }
236 }
237
238 // Close closes the connection to the API service. The user should invoke this when
239 // the client is no longer required.
240 func (c *gRPCClient) Close() error {
241 return c.connPool.Close()
242 }
243
244 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
245 type restClient struct {
246 // The http endpoint to connect to.
247 endpoint string
248
249 // The http client.
250 httpClient *http.Client
251
252 // The x-goog-* headers to be sent with each request.
253 xGoogHeaders []string
254
255 // Points back to the CallOptions field of the containing Client
256 CallOptions **CallOptions
257
258 logger *slog.Logger
259 }
260
261 // NewRESTClient creates a new places rest client.
262 //
263 // Service definition for the Places API.
264 // Note: every request (except for Autocomplete and GetPhotoMedia requests)
265 // requires a field mask set outside of the request proto (all/*, is not
266 // assumed). The field mask can be set via the HTTP header X-Goog-FieldMask.
267 // See:
268 // https://developers.google.com/maps/documentation/places/web-service/choose-fields (at https://developers.google.com/maps/documentation/places/web-service/choose-fields)
269 func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
270 clientOpts := append(defaultRESTClientOptions(), opts...)
271 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
272 if err != nil {
273 return nil, err
274 }
275
276 callOpts := defaultRESTCallOptions()
277 c := &restClient{
278 endpoint: endpoint,
279 httpClient: httpClient,
280 CallOptions: &callOpts,
281 logger: internaloption.GetLogger(opts),
282 }
283 c.setGoogleClientInfo()
284
285 return &Client{internalClient: c, CallOptions: callOpts}, nil
286 }
287
288 func defaultRESTClientOptions() []option.ClientOption {
289 return []option.ClientOption{
290 internaloption.WithDefaultEndpoint("https://places.googleapis.com"),
291 internaloption.WithDefaultEndpointTemplate("https://places.UNIVERSE_DOMAIN"),
292 internaloption.WithDefaultMTLSEndpoint("https://places.mtls.googleapis.com"),
293 internaloption.WithDefaultUniverseDomain("googleapis.com"),
294 internaloption.WithDefaultAudience("https://places.googleapis.com/"),
295 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
296 internaloption.EnableNewAuthLibrary(),
297 }
298 }
299
300 // setGoogleClientInfo sets the name and version of the application in
301 // the `x-goog-api-client` header passed on each request. Intended for
302 // use by Google-written clients.
303 func (c *restClient) setGoogleClientInfo(keyval ...string) {
304 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
305 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
306 c.xGoogHeaders = []string{
307 "x-goog-api-client", gax.XGoogHeader(kv...),
308 }
309 }
310
311 // Close closes the connection to the API service. The user should invoke this when
312 // the client is no longer required.
313 func (c *restClient) Close() error {
314 // Replace httpClient with nil to force cleanup.
315 c.httpClient = nil
316 return nil
317 }
318
319 // Connection returns a connection to the API service.
320 //
321 // Deprecated: This method always returns nil.
322 func (c *restClient) Connection() *grpc.ClientConn {
323 return nil
324 }
325 func (c *gRPCClient) SearchNearby(ctx context.Context, req *placespb.SearchNearbyRequest, opts ...gax.CallOption) (*placespb.SearchNearbyResponse, error) {
326 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
327 opts = append((*c.CallOptions).SearchNearby[0:len((*c.CallOptions).SearchNearby):len((*c.CallOptions).SearchNearby)], opts...)
328 var resp *placespb.SearchNearbyResponse
329 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
330 var err error
331 resp, err = executeRPC(ctx, c.client.SearchNearby, req, settings.GRPC, c.logger, "SearchNearby")
332 return err
333 }, opts...)
334 if err != nil {
335 return nil, err
336 }
337 return resp, nil
338 }
339
340 func (c *gRPCClient) SearchText(ctx context.Context, req *placespb.SearchTextRequest, opts ...gax.CallOption) (*placespb.SearchTextResponse, error) {
341 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
342 opts = append((*c.CallOptions).SearchText[0:len((*c.CallOptions).SearchText):len((*c.CallOptions).SearchText)], opts...)
343 var resp *placespb.SearchTextResponse
344 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
345 var err error
346 resp, err = executeRPC(ctx, c.client.SearchText, req, settings.GRPC, c.logger, "SearchText")
347 return err
348 }, opts...)
349 if err != nil {
350 return nil, err
351 }
352 return resp, nil
353 }
354
355 func (c *gRPCClient) GetPhotoMedia(ctx context.Context, req *placespb.GetPhotoMediaRequest, opts ...gax.CallOption) (*placespb.PhotoMedia, error) {
356 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
357
358 hds = append(c.xGoogHeaders, hds...)
359 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
360 opts = append((*c.CallOptions).GetPhotoMedia[0:len((*c.CallOptions).GetPhotoMedia):len((*c.CallOptions).GetPhotoMedia)], opts...)
361 var resp *placespb.PhotoMedia
362 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
363 var err error
364 resp, err = executeRPC(ctx, c.client.GetPhotoMedia, req, settings.GRPC, c.logger, "GetPhotoMedia")
365 return err
366 }, opts...)
367 if err != nil {
368 return nil, err
369 }
370 return resp, nil
371 }
372
373 func (c *gRPCClient) GetPlace(ctx context.Context, req *placespb.GetPlaceRequest, opts ...gax.CallOption) (*placespb.Place, error) {
374 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
375
376 hds = append(c.xGoogHeaders, hds...)
377 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
378 opts = append((*c.CallOptions).GetPlace[0:len((*c.CallOptions).GetPlace):len((*c.CallOptions).GetPlace)], opts...)
379 var resp *placespb.Place
380 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
381 var err error
382 resp, err = executeRPC(ctx, c.client.GetPlace, req, settings.GRPC, c.logger, "GetPlace")
383 return err
384 }, opts...)
385 if err != nil {
386 return nil, err
387 }
388 return resp, nil
389 }
390
391 func (c *gRPCClient) AutocompletePlaces(ctx context.Context, req *placespb.AutocompletePlacesRequest, opts ...gax.CallOption) (*placespb.AutocompletePlacesResponse, error) {
392 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
393 opts = append((*c.CallOptions).AutocompletePlaces[0:len((*c.CallOptions).AutocompletePlaces):len((*c.CallOptions).AutocompletePlaces)], opts...)
394 var resp *placespb.AutocompletePlacesResponse
395 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
396 var err error
397 resp, err = executeRPC(ctx, c.client.AutocompletePlaces, req, settings.GRPC, c.logger, "AutocompletePlaces")
398 return err
399 }, opts...)
400 if err != nil {
401 return nil, err
402 }
403 return resp, nil
404 }
405
406 // SearchNearby search for places near locations.
407 func (c *restClient) SearchNearby(ctx context.Context, req *placespb.SearchNearbyRequest, opts ...gax.CallOption) (*placespb.SearchNearbyResponse, error) {
408 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
409 jsonReq, err := m.Marshal(req)
410 if err != nil {
411 return nil, err
412 }
413
414 baseUrl, err := url.Parse(c.endpoint)
415 if err != nil {
416 return nil, err
417 }
418 baseUrl.Path += fmt.Sprintf("/v1/places:searchNearby")
419
420 params := url.Values{}
421 params.Add("$alt", "json;enum-encoding=int")
422
423 baseUrl.RawQuery = params.Encode()
424
425 // Build HTTP headers from client and context metadata.
426 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
427 headers := gax.BuildHeaders(ctx, hds...)
428 opts = append((*c.CallOptions).SearchNearby[0:len((*c.CallOptions).SearchNearby):len((*c.CallOptions).SearchNearby)], opts...)
429 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
430 resp := &placespb.SearchNearbyResponse{}
431 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
432 if settings.Path != "" {
433 baseUrl.Path = settings.Path
434 }
435 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
436 if err != nil {
437 return err
438 }
439 httpReq = httpReq.WithContext(ctx)
440 httpReq.Header = headers
441
442 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "SearchNearby")
443 if err != nil {
444 return err
445 }
446
447 if err := unm.Unmarshal(buf, resp); err != nil {
448 return err
449 }
450
451 return nil
452 }, opts...)
453 if e != nil {
454 return nil, e
455 }
456 return resp, nil
457 }
458
459 // SearchText text query based place search.
460 func (c *restClient) SearchText(ctx context.Context, req *placespb.SearchTextRequest, opts ...gax.CallOption) (*placespb.SearchTextResponse, error) {
461 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
462 jsonReq, err := m.Marshal(req)
463 if err != nil {
464 return nil, err
465 }
466
467 baseUrl, err := url.Parse(c.endpoint)
468 if err != nil {
469 return nil, err
470 }
471 baseUrl.Path += fmt.Sprintf("/v1/places:searchText")
472
473 params := url.Values{}
474 params.Add("$alt", "json;enum-encoding=int")
475
476 baseUrl.RawQuery = params.Encode()
477
478 // Build HTTP headers from client and context metadata.
479 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
480 headers := gax.BuildHeaders(ctx, hds...)
481 opts = append((*c.CallOptions).SearchText[0:len((*c.CallOptions).SearchText):len((*c.CallOptions).SearchText)], opts...)
482 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
483 resp := &placespb.SearchTextResponse{}
484 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
485 if settings.Path != "" {
486 baseUrl.Path = settings.Path
487 }
488 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
489 if err != nil {
490 return err
491 }
492 httpReq = httpReq.WithContext(ctx)
493 httpReq.Header = headers
494
495 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "SearchText")
496 if err != nil {
497 return err
498 }
499
500 if err := unm.Unmarshal(buf, resp); err != nil {
501 return err
502 }
503
504 return nil
505 }, opts...)
506 if e != nil {
507 return nil, e
508 }
509 return resp, nil
510 }
511
512 // GetPhotoMedia get a photo media with a photo reference string.
513 func (c *restClient) GetPhotoMedia(ctx context.Context, req *placespb.GetPhotoMediaRequest, opts ...gax.CallOption) (*placespb.PhotoMedia, error) {
514 baseUrl, err := url.Parse(c.endpoint)
515 if err != nil {
516 return nil, err
517 }
518 baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
519
520 params := url.Values{}
521 params.Add("$alt", "json;enum-encoding=int")
522 if req.GetMaxHeightPx() != 0 {
523 params.Add("maxHeightPx", fmt.Sprintf("%v", req.GetMaxHeightPx()))
524 }
525 if req.GetMaxWidthPx() != 0 {
526 params.Add("maxWidthPx", fmt.Sprintf("%v", req.GetMaxWidthPx()))
527 }
528 if req.GetSkipHttpRedirect() {
529 params.Add("skipHttpRedirect", fmt.Sprintf("%v", req.GetSkipHttpRedirect()))
530 }
531
532 baseUrl.RawQuery = params.Encode()
533
534 // Build HTTP headers from client and context metadata.
535 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
536
537 hds = append(c.xGoogHeaders, hds...)
538 hds = append(hds, "Content-Type", "application/json")
539 headers := gax.BuildHeaders(ctx, hds...)
540 opts = append((*c.CallOptions).GetPhotoMedia[0:len((*c.CallOptions).GetPhotoMedia):len((*c.CallOptions).GetPhotoMedia)], opts...)
541 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
542 resp := &placespb.PhotoMedia{}
543 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
544 if settings.Path != "" {
545 baseUrl.Path = settings.Path
546 }
547 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
548 if err != nil {
549 return err
550 }
551 httpReq = httpReq.WithContext(ctx)
552 httpReq.Header = headers
553
554 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetPhotoMedia")
555 if err != nil {
556 return err
557 }
558
559 if err := unm.Unmarshal(buf, resp); err != nil {
560 return err
561 }
562
563 return nil
564 }, opts...)
565 if e != nil {
566 return nil, e
567 }
568 return resp, nil
569 }
570
571 // GetPlace get the details of a place based on its resource name, which is a string
572 // in the places/{place_id} format.
573 func (c *restClient) GetPlace(ctx context.Context, req *placespb.GetPlaceRequest, opts ...gax.CallOption) (*placespb.Place, error) {
574 baseUrl, err := url.Parse(c.endpoint)
575 if err != nil {
576 return nil, err
577 }
578 baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
579
580 params := url.Values{}
581 params.Add("$alt", "json;enum-encoding=int")
582 if req.GetLanguageCode() != "" {
583 params.Add("languageCode", fmt.Sprintf("%v", req.GetLanguageCode()))
584 }
585 if req.GetRegionCode() != "" {
586 params.Add("regionCode", fmt.Sprintf("%v", req.GetRegionCode()))
587 }
588 if req.GetSessionToken() != "" {
589 params.Add("sessionToken", fmt.Sprintf("%v", req.GetSessionToken()))
590 }
591
592 baseUrl.RawQuery = params.Encode()
593
594 // Build HTTP headers from client and context metadata.
595 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
596
597 hds = append(c.xGoogHeaders, hds...)
598 hds = append(hds, "Content-Type", "application/json")
599 headers := gax.BuildHeaders(ctx, hds...)
600 opts = append((*c.CallOptions).GetPlace[0:len((*c.CallOptions).GetPlace):len((*c.CallOptions).GetPlace)], opts...)
601 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
602 resp := &placespb.Place{}
603 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
604 if settings.Path != "" {
605 baseUrl.Path = settings.Path
606 }
607 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
608 if err != nil {
609 return err
610 }
611 httpReq = httpReq.WithContext(ctx)
612 httpReq.Header = headers
613
614 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetPlace")
615 if err != nil {
616 return err
617 }
618
619 if err := unm.Unmarshal(buf, resp); err != nil {
620 return err
621 }
622
623 return nil
624 }, opts...)
625 if e != nil {
626 return nil, e
627 }
628 return resp, nil
629 }
630
631 // AutocompletePlaces returns predictions for the given input.
632 func (c *restClient) AutocompletePlaces(ctx context.Context, req *placespb.AutocompletePlacesRequest, opts ...gax.CallOption) (*placespb.AutocompletePlacesResponse, error) {
633 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
634 jsonReq, err := m.Marshal(req)
635 if err != nil {
636 return nil, err
637 }
638
639 baseUrl, err := url.Parse(c.endpoint)
640 if err != nil {
641 return nil, err
642 }
643 baseUrl.Path += fmt.Sprintf("/v1/places:autocomplete")
644
645 params := url.Values{}
646 params.Add("$alt", "json;enum-encoding=int")
647
648 baseUrl.RawQuery = params.Encode()
649
650 // Build HTTP headers from client and context metadata.
651 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
652 headers := gax.BuildHeaders(ctx, hds...)
653 opts = append((*c.CallOptions).AutocompletePlaces[0:len((*c.CallOptions).AutocompletePlaces):len((*c.CallOptions).AutocompletePlaces)], opts...)
654 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
655 resp := &placespb.AutocompletePlacesResponse{}
656 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
657 if settings.Path != "" {
658 baseUrl.Path = settings.Path
659 }
660 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
661 if err != nil {
662 return err
663 }
664 httpReq = httpReq.WithContext(ctx)
665 httpReq.Header = headers
666
667 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "AutocompletePlaces")
668 if err != nil {
669 return err
670 }
671
672 if err := unm.Unmarshal(buf, resp); err != nil {
673 return err
674 }
675
676 return nil
677 }, opts...)
678 if e != nil {
679 return nil, e
680 }
681 return resp, nil
682 }