Debian Codegraph @

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