Debian Codegraph @

shopping/merchant/quota/apiv1beta/quota_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 quota
18
19 import (
20 "context"
21 "fmt"
22 "log/slog"
23 "math"
24 "net/http"
25 "net/url"
26 "time"
27
28 quotapb "cloud.google.com/go/shopping/merchant/quota/apiv1beta/quotapb"
29 gax "github.com/googleapis/gax-go/v2"
30 "google.golang.org/api/iterator"
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 "google.golang.org/protobuf/proto"
39 )
40
41 var newClientHook clientHook
42
43 // CallOptions contains the retry settings for each method of Client.
44 type CallOptions struct {
45 ListQuotaGroups []gax.CallOption
46 }
47
48 func defaultGRPCClientOptions() []option.ClientOption {
49 return []option.ClientOption{
50 internaloption.WithDefaultEndpoint("merchantapi.googleapis.com:443"),
51 internaloption.WithDefaultEndpointTemplate("merchantapi.UNIVERSE_DOMAIN:443"),
52 internaloption.WithDefaultMTLSEndpoint("merchantapi.mtls.googleapis.com:443"),
53 internaloption.WithDefaultUniverseDomain("googleapis.com"),
54 internaloption.WithDefaultAudience("https://merchantapi.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 defaultCallOptions() *CallOptions {
64 return &CallOptions{
65 ListQuotaGroups: []gax.CallOption{
66 gax.WithTimeout(60000 * time.Millisecond),
67 gax.WithRetry(func() gax.Retryer {
68 return gax.OnCodes([]codes.Code{
69 codes.Unavailable,
70 }, gax.Backoff{
71 Initial: 1000 * time.Millisecond,
72 Max: 10000 * time.Millisecond,
73 Multiplier: 1.30,
74 })
75 }),
76 },
77 }
78 }
79
80 func defaultRESTCallOptions() *CallOptions {
81 return &CallOptions{
82 ListQuotaGroups: []gax.CallOption{
83 gax.WithTimeout(60000 * time.Millisecond),
84 gax.WithRetry(func() gax.Retryer {
85 return gax.OnHTTPCodes(gax.Backoff{
86 Initial: 1000 * time.Millisecond,
87 Max: 10000 * time.Millisecond,
88 Multiplier: 1.30,
89 },
90 http.StatusServiceUnavailable)
91 }),
92 },
93 }
94 }
95
96 // internalClient is an interface that defines the methods available from Merchant API.
97 type internalClient interface {
98 Close() error
99 setGoogleClientInfo(...string)
100 Connection() *grpc.ClientConn
101 ListQuotaGroups(context.Context, *quotapb.ListQuotaGroupsRequest, ...gax.CallOption) *QuotaGroupIterator
102 }
103
104 // Client is a client for interacting with Merchant API.
105 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
106 //
107 // Service to get method call quota information per Merchant API method.
108 type Client struct {
109 // The internal transport-dependent client.
110 internalClient internalClient
111
112 // The call options for this service.
113 CallOptions *CallOptions
114 }
115
116 // Wrapper methods routed to the internal client.
117
118 // Close closes the connection to the API service. The user should invoke this when
119 // the client is no longer required.
120 func (c *Client) Close() error {
121 return c.internalClient.Close()
122 }
123
124 // setGoogleClientInfo sets the name and version of the application in
125 // the `x-goog-api-client` header passed on each request. Intended for
126 // use by Google-written clients.
127 func (c *Client) setGoogleClientInfo(keyval ...string) {
128 c.internalClient.setGoogleClientInfo(keyval...)
129 }
130
131 // Connection returns a connection to the API service.
132 //
133 // Deprecated: Connections are now pooled so this method does not always
134 // return the same resource.
135 func (c *Client) Connection() *grpc.ClientConn {
136 return c.internalClient.Connection()
137 }
138
139 // ListQuotaGroups lists the daily call quota and usage per group for your Merchant
140 // Center account.
141 func (c *Client) ListQuotaGroups(ctx context.Context, req *quotapb.ListQuotaGroupsRequest, opts ...gax.CallOption) *QuotaGroupIterator {
142 return c.internalClient.ListQuotaGroups(ctx, req, opts...)
143 }
144
145 // gRPCClient is a client for interacting with Merchant API over gRPC transport.
146 //
147 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
148 type gRPCClient struct {
149 // Connection pool of gRPC connections to the service.
150 connPool gtransport.ConnPool
151
152 // Points back to the CallOptions field of the containing Client
153 CallOptions **CallOptions
154
155 // The gRPC API client.
156 client quotapb.QuotaServiceClient
157
158 // The x-goog-* metadata to be sent with each request.
159 xGoogHeaders []string
160
161 logger *slog.Logger
162 }
163
164 // NewClient creates a new quota service client based on gRPC.
165 // The returned client must be Closed when it is done being used to clean up its underlying connections.
166 //
167 // Service to get method call quota information per Merchant API method.
168 func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
169 clientOpts := defaultGRPCClientOptions()
170 if newClientHook != nil {
171 hookOpts, err := newClientHook(ctx, clientHookParams{})
172 if err != nil {
173 return nil, err
174 }
175 clientOpts = append(clientOpts, hookOpts...)
176 }
177
178 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
179 if err != nil {
180 return nil, err
181 }
182 client := Client{CallOptions: defaultCallOptions()}
183
184 c := &gRPCClient{
185 connPool: connPool,
186 client: quotapb.NewQuotaServiceClient(connPool),
187 CallOptions: &client.CallOptions,
188 logger: internaloption.GetLogger(opts),
189 }
190 c.setGoogleClientInfo()
191
192 client.internalClient = c
193
194 return &client, nil
195 }
196
197 // Connection returns a connection to the API service.
198 //
199 // Deprecated: Connections are now pooled so this method does not always
200 // return the same resource.
201 func (c *gRPCClient) Connection() *grpc.ClientConn {
202 return c.connPool.Conn()
203 }
204
205 // setGoogleClientInfo sets the name and version of the application in
206 // the `x-goog-api-client` header passed on each request. Intended for
207 // use by Google-written clients.
208 func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
209 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
210 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
211 c.xGoogHeaders = []string{
212 "x-goog-api-client", gax.XGoogHeader(kv...),
213 }
214 }
215
216 // Close closes the connection to the API service. The user should invoke this when
217 // the client is no longer required.
218 func (c *gRPCClient) Close() error {
219 return c.connPool.Close()
220 }
221
222 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
223 type restClient struct {
224 // The http endpoint to connect to.
225 endpoint string
226
227 // The http client.
228 httpClient *http.Client
229
230 // The x-goog-* headers to be sent with each request.
231 xGoogHeaders []string
232
233 // Points back to the CallOptions field of the containing Client
234 CallOptions **CallOptions
235
236 logger *slog.Logger
237 }
238
239 // NewRESTClient creates a new quota service rest client.
240 //
241 // Service to get method call quota information per Merchant API method.
242 func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
243 clientOpts := append(defaultRESTClientOptions(), opts...)
244 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
245 if err != nil {
246 return nil, err
247 }
248
249 callOpts := defaultRESTCallOptions()
250 c := &restClient{
251 endpoint: endpoint,
252 httpClient: httpClient,
253 CallOptions: &callOpts,
254 logger: internaloption.GetLogger(opts),
255 }
256 c.setGoogleClientInfo()
257
258 return &Client{internalClient: c, CallOptions: callOpts}, nil
259 }
260
261 func defaultRESTClientOptions() []option.ClientOption {
262 return []option.ClientOption{
263 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
264 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
265 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
266 internaloption.WithDefaultUniverseDomain("googleapis.com"),
267 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
268 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
269 internaloption.EnableNewAuthLibrary(),
270 }
271 }
272
273 // setGoogleClientInfo sets the name and version of the application in
274 // the `x-goog-api-client` header passed on each request. Intended for
275 // use by Google-written clients.
276 func (c *restClient) setGoogleClientInfo(keyval ...string) {
277 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
278 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
279 c.xGoogHeaders = []string{
280 "x-goog-api-client", gax.XGoogHeader(kv...),
281 }
282 }
283
284 // Close closes the connection to the API service. The user should invoke this when
285 // the client is no longer required.
286 func (c *restClient) Close() error {
287 // Replace httpClient with nil to force cleanup.
288 c.httpClient = nil
289 return nil
290 }
291
292 // Connection returns a connection to the API service.
293 //
294 // Deprecated: This method always returns nil.
295 func (c *restClient) Connection() *grpc.ClientConn {
296 return nil
297 }
298 func (c *gRPCClient) ListQuotaGroups(ctx context.Context, req *quotapb.ListQuotaGroupsRequest, opts ...gax.CallOption) *QuotaGroupIterator {
299 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
300
301 hds = append(c.xGoogHeaders, hds...)
302 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
303 opts = append((*c.CallOptions).ListQuotaGroups[0:len((*c.CallOptions).ListQuotaGroups):len((*c.CallOptions).ListQuotaGroups)], opts...)
304 it := &QuotaGroupIterator{}
305 req = proto.Clone(req).(*quotapb.ListQuotaGroupsRequest)
306 it.InternalFetch = func(pageSize int, pageToken string) ([]*quotapb.QuotaGroup, string, error) {
307 resp := &quotapb.ListQuotaGroupsResponse{}
308 if pageToken != "" {
309 req.PageToken = pageToken
310 }
311 if pageSize > math.MaxInt32 {
312 req.PageSize = math.MaxInt32
313 } else if pageSize != 0 {
314 req.PageSize = int32(pageSize)
315 }
316 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
317 var err error
318 resp, err = executeRPC(ctx, c.client.ListQuotaGroups, req, settings.GRPC, c.logger, "ListQuotaGroups")
319 return err
320 }, opts...)
321 if err != nil {
322 return nil, "", err
323 }
324
325 it.Response = resp
326 return resp.GetQuotaGroups(), resp.GetNextPageToken(), nil
327 }
328 fetch := func(pageSize int, pageToken string) (string, error) {
329 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
330 if err != nil {
331 return "", err
332 }
333 it.items = append(it.items, items...)
334 return nextPageToken, nil
335 }
336
337 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
338 it.pageInfo.MaxSize = int(req.GetPageSize())
339 it.pageInfo.Token = req.GetPageToken()
340
341 return it
342 }
343
344 // ListQuotaGroups lists the daily call quota and usage per group for your Merchant
345 // Center account.
346 func (c *restClient) ListQuotaGroups(ctx context.Context, req *quotapb.ListQuotaGroupsRequest, opts ...gax.CallOption) *QuotaGroupIterator {
347 it := &QuotaGroupIterator{}
348 req = proto.Clone(req).(*quotapb.ListQuotaGroupsRequest)
349 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
350 it.InternalFetch = func(pageSize int, pageToken string) ([]*quotapb.QuotaGroup, string, error) {
351 resp := &quotapb.ListQuotaGroupsResponse{}
352 if pageToken != "" {
353 req.PageToken = pageToken
354 }
355 if pageSize > math.MaxInt32 {
356 req.PageSize = math.MaxInt32
357 } else if pageSize != 0 {
358 req.PageSize = int32(pageSize)
359 }
360 baseUrl, err := url.Parse(c.endpoint)
361 if err != nil {
362 return nil, "", err
363 }
364 baseUrl.Path += fmt.Sprintf("/quota/v1beta/%v/quotas", req.GetParent())
365
366 params := url.Values{}
367 params.Add("$alt", "json;enum-encoding=int")
368 if req.GetPageSize() != 0 {
369 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
370 }
371 if req.GetPageToken() != "" {
372 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
373 }
374
375 baseUrl.RawQuery = params.Encode()
376
377 // Build HTTP headers from client and context metadata.
378 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
379 headers := gax.BuildHeaders(ctx, hds...)
380 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
381 if settings.Path != "" {
382 baseUrl.Path = settings.Path
383 }
384 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
385 if err != nil {
386 return err
387 }
388 httpReq.Header = headers
389
390 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListQuotaGroups")
391 if err != nil {
392 return err
393 }
394 if err := unm.Unmarshal(buf, resp); err != nil {
395 return err
396 }
397
398 return nil
399 }, opts...)
400 if e != nil {
401 return nil, "", e
402 }
403 it.Response = resp
404 return resp.GetQuotaGroups(), resp.GetNextPageToken(), nil
405 }
406
407 fetch := func(pageSize int, pageToken string) (string, error) {
408 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
409 if err != nil {
410 return "", err
411 }
412 it.items = append(it.items, items...)
413 return nextPageToken, nil
414 }
415
416 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
417 it.pageInfo.MaxSize = int(req.GetPageSize())
418 it.pageInfo.Token = req.GetPageToken()
419
420 return it
421 }