Debian Codegraph @

maps/areainsights/apiv1/area_insights_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 areainsights
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27 "time"
28
29 areainsightspb "cloud.google.com/go/maps/areainsights/apiv1/areainsightspb"
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 newClientHook clientHook
41
42 // CallOptions contains the retry settings for each method of Client.
43 type CallOptions struct {
44 ComputeInsights []gax.CallOption
45 }
46
47 func defaultGRPCClientOptions() []option.ClientOption {
48 return []option.ClientOption{
49 internaloption.WithDefaultEndpoint("areainsights.googleapis.com:443"),
50 internaloption.WithDefaultEndpointTemplate("areainsights.UNIVERSE_DOMAIN:443"),
51 internaloption.WithDefaultMTLSEndpoint("areainsights.mtls.googleapis.com:443"),
52 internaloption.WithDefaultUniverseDomain("googleapis.com"),
53 internaloption.WithDefaultAudience("https://areainsights.googleapis.com/"),
54 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
55 internaloption.EnableJwtWithScope(),
56 internaloption.EnableNewAuthLibrary(),
57 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
58 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
59 }
60 }
61
62 func defaultCallOptions() *CallOptions {
63 return &CallOptions{
64 ComputeInsights: []gax.CallOption{
65 gax.WithTimeout(120000 * time.Millisecond),
66 gax.WithRetry(func() gax.Retryer {
67 return gax.OnCodes([]codes.Code{
68 codes.Unavailable,
69 }, gax.Backoff{
70 Initial: 1000 * time.Millisecond,
71 Max: 10000 * time.Millisecond,
72 Multiplier: 1.30,
73 })
74 }),
75 },
76 }
77 }
78
79 func defaultRESTCallOptions() *CallOptions {
80 return &CallOptions{
81 ComputeInsights: []gax.CallOption{
82 gax.WithTimeout(120000 * time.Millisecond),
83 gax.WithRetry(func() gax.Retryer {
84 return gax.OnHTTPCodes(gax.Backoff{
85 Initial: 1000 * time.Millisecond,
86 Max: 10000 * time.Millisecond,
87 Multiplier: 1.30,
88 },
89 http.StatusServiceUnavailable)
90 }),
91 },
92 }
93 }
94
95 // internalClient is an interface that defines the methods available from Places Aggregate API.
96 type internalClient interface {
97 Close() error
98 setGoogleClientInfo(...string)
99 Connection() *grpc.ClientConn
100 ComputeInsights(context.Context, *areainsightspb.ComputeInsightsRequest, ...gax.CallOption) (*areainsightspb.ComputeInsightsResponse, error)
101 }
102
103 // Client is a client for interacting with Places Aggregate API.
104 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
105 //
106 // Service definition for the Places Aggregate RPC.
107 type Client struct {
108 // The internal transport-dependent client.
109 internalClient internalClient
110
111 // The call options for this service.
112 CallOptions *CallOptions
113 }
114
115 // Wrapper methods routed to the internal client.
116
117 // Close closes the connection to the API service. The user should invoke this when
118 // the client is no longer required.
119 func (c *Client) Close() error {
120 return c.internalClient.Close()
121 }
122
123 // setGoogleClientInfo sets the name and version of the application in
124 // the `x-goog-api-client` header passed on each request. Intended for
125 // use by Google-written clients.
126 func (c *Client) setGoogleClientInfo(keyval ...string) {
127 c.internalClient.setGoogleClientInfo(keyval...)
128 }
129
130 // Connection returns a connection to the API service.
131 //
132 // Deprecated: Connections are now pooled so this method does not always
133 // return the same resource.
134 func (c *Client) Connection() *grpc.ClientConn {
135 return c.internalClient.Connection()
136 }
137
138 // ComputeInsights this method lets you retrieve insights about areas using a variety of
139 // filter such as: area, place type, operating status, price level
140 // and ratings. Currently “count” and “places” insights are supported. With
141 // “count” insights you can answer questions such as “How many restaurant are
142 // located in California that are operational, are inexpensive and have an
143 // average rating of at least 4 stars” (see insight enum for more details).
144 // With “places” insights, you can determine which places match the
145 // requested filter. Clients can then use those place resource names to fetch
146 // more details about each individual place using the Places API.
147 func (c *Client) ComputeInsights(ctx context.Context, req *areainsightspb.ComputeInsightsRequest, opts ...gax.CallOption) (*areainsightspb.ComputeInsightsResponse, error) {
148 return c.internalClient.ComputeInsights(ctx, req, opts...)
149 }
150
151 // gRPCClient is a client for interacting with Places Aggregate API over gRPC transport.
152 //
153 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
154 type gRPCClient struct {
155 // Connection pool of gRPC connections to the service.
156 connPool gtransport.ConnPool
157
158 // Points back to the CallOptions field of the containing Client
159 CallOptions **CallOptions
160
161 // The gRPC API client.
162 client areainsightspb.AreaInsightsClient
163
164 // The x-goog-* metadata to be sent with each request.
165 xGoogHeaders []string
166
167 logger *slog.Logger
168 }
169
170 // NewClient creates a new area insights client based on gRPC.
171 // The returned client must be Closed when it is done being used to clean up its underlying connections.
172 //
173 // Service definition for the Places Aggregate RPC.
174 func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
175 clientOpts := defaultGRPCClientOptions()
176 if newClientHook != nil {
177 hookOpts, err := newClientHook(ctx, clientHookParams{})
178 if err != nil {
179 return nil, err
180 }
181 clientOpts = append(clientOpts, hookOpts...)
182 }
183
184 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
185 if err != nil {
186 return nil, err
187 }
188 client := Client{CallOptions: defaultCallOptions()}
189
190 c := &gRPCClient{
191 connPool: connPool,
192 client: areainsightspb.NewAreaInsightsClient(connPool),
193 CallOptions: &client.CallOptions,
194 logger: internaloption.GetLogger(opts),
195 }
196 c.setGoogleClientInfo()
197
198 client.internalClient = c
199
200 return &client, nil
201 }
202
203 // Connection returns a connection to the API service.
204 //
205 // Deprecated: Connections are now pooled so this method does not always
206 // return the same resource.
207 func (c *gRPCClient) Connection() *grpc.ClientConn {
208 return c.connPool.Conn()
209 }
210
211 // setGoogleClientInfo sets the name and version of the application in
212 // the `x-goog-api-client` header passed on each request. Intended for
213 // use by Google-written clients.
214 func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
215 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
216 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
217 c.xGoogHeaders = []string{
218 "x-goog-api-client", gax.XGoogHeader(kv...),
219 }
220 }
221
222 // Close closes the connection to the API service. The user should invoke this when
223 // the client is no longer required.
224 func (c *gRPCClient) Close() error {
225 return c.connPool.Close()
226 }
227
228 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
229 type restClient struct {
230 // The http endpoint to connect to.
231 endpoint string
232
233 // The http client.
234 httpClient *http.Client
235
236 // The x-goog-* headers to be sent with each request.
237 xGoogHeaders []string
238
239 // Points back to the CallOptions field of the containing Client
240 CallOptions **CallOptions
241
242 logger *slog.Logger
243 }
244
245 // NewRESTClient creates a new area insights rest client.
246 //
247 // Service definition for the Places Aggregate RPC.
248 func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
249 clientOpts := append(defaultRESTClientOptions(), opts...)
250 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
251 if err != nil {
252 return nil, err
253 }
254
255 callOpts := defaultRESTCallOptions()
256 c := &restClient{
257 endpoint: endpoint,
258 httpClient: httpClient,
259 CallOptions: &callOpts,
260 logger: internaloption.GetLogger(opts),
261 }
262 c.setGoogleClientInfo()
263
264 return &Client{internalClient: c, CallOptions: callOpts}, nil
265 }
266
267 func defaultRESTClientOptions() []option.ClientOption {
268 return []option.ClientOption{
269 internaloption.WithDefaultEndpoint("https://areainsights.googleapis.com"),
270 internaloption.WithDefaultEndpointTemplate("https://areainsights.UNIVERSE_DOMAIN"),
271 internaloption.WithDefaultMTLSEndpoint("https://areainsights.mtls.googleapis.com"),
272 internaloption.WithDefaultUniverseDomain("googleapis.com"),
273 internaloption.WithDefaultAudience("https://areainsights.googleapis.com/"),
274 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
275 internaloption.EnableNewAuthLibrary(),
276 }
277 }
278
279 // setGoogleClientInfo sets the name and version of the application in
280 // the `x-goog-api-client` header passed on each request. Intended for
281 // use by Google-written clients.
282 func (c *restClient) setGoogleClientInfo(keyval ...string) {
283 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
284 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
285 c.xGoogHeaders = []string{
286 "x-goog-api-client", gax.XGoogHeader(kv...),
287 }
288 }
289
290 // Close closes the connection to the API service. The user should invoke this when
291 // the client is no longer required.
292 func (c *restClient) Close() error {
293 // Replace httpClient with nil to force cleanup.
294 c.httpClient = nil
295 return nil
296 }
297
298 // Connection returns a connection to the API service.
299 //
300 // Deprecated: This method always returns nil.
301 func (c *restClient) Connection() *grpc.ClientConn {
302 return nil
303 }
304 func (c *gRPCClient) ComputeInsights(ctx context.Context, req *areainsightspb.ComputeInsightsRequest, opts ...gax.CallOption) (*areainsightspb.ComputeInsightsResponse, error) {
305 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
306 opts = append((*c.CallOptions).ComputeInsights[0:len((*c.CallOptions).ComputeInsights):len((*c.CallOptions).ComputeInsights)], opts...)
307 var resp *areainsightspb.ComputeInsightsResponse
308 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
309 var err error
310 resp, err = executeRPC(ctx, c.client.ComputeInsights, req, settings.GRPC, c.logger, "ComputeInsights")
311 return err
312 }, opts...)
313 if err != nil {
314 return nil, err
315 }
316 return resp, nil
317 }
318
319 // ComputeInsights this method lets you retrieve insights about areas using a variety of
320 // filter such as: area, place type, operating status, price level
321 // and ratings. Currently “count” and “places” insights are supported. With
322 // “count” insights you can answer questions such as “How many restaurant are
323 // located in California that are operational, are inexpensive and have an
324 // average rating of at least 4 stars” (see insight enum for more details).
325 // With “places” insights, you can determine which places match the
326 // requested filter. Clients can then use those place resource names to fetch
327 // more details about each individual place using the Places API.
328 func (c *restClient) ComputeInsights(ctx context.Context, req *areainsightspb.ComputeInsightsRequest, opts ...gax.CallOption) (*areainsightspb.ComputeInsightsResponse, error) {
329 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
330 jsonReq, err := m.Marshal(req)
331 if err != nil {
332 return nil, err
333 }
334
335 baseUrl, err := url.Parse(c.endpoint)
336 if err != nil {
337 return nil, err
338 }
339 baseUrl.Path += fmt.Sprintf("/v1:computeInsights")
340
341 params := url.Values{}
342 params.Add("$alt", "json;enum-encoding=int")
343
344 baseUrl.RawQuery = params.Encode()
345
346 // Build HTTP headers from client and context metadata.
347 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
348 headers := gax.BuildHeaders(ctx, hds...)
349 opts = append((*c.CallOptions).ComputeInsights[0:len((*c.CallOptions).ComputeInsights):len((*c.CallOptions).ComputeInsights)], opts...)
350 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
351 resp := &areainsightspb.ComputeInsightsResponse{}
352 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
353 if settings.Path != "" {
354 baseUrl.Path = settings.Path
355 }
356 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
357 if err != nil {
358 return err
359 }
360 httpReq = httpReq.WithContext(ctx)
361 httpReq.Header = headers
362
363 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "ComputeInsights")
364 if err != nil {
365 return err
366 }
367
368 if err := unm.Unmarshal(buf, resp); err != nil {
369 return err
370 }
371
372 return nil
373 }, opts...)
374 if e != nil {
375 return nil, e
376 }
377 return resp, nil
378 }