Debian Codegraph @

shopping/merchant/accounts/apiv1beta/gbp_accounts_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 accounts
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27 "time"
28
29 accountspb "cloud.google.com/go/shopping/merchant/accounts/apiv1beta/accountspb"
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 "google.golang.org/grpc"
37 "google.golang.org/grpc/codes"
38 "google.golang.org/protobuf/encoding/protojson"
39 "google.golang.org/protobuf/proto"
40 )
41
42 var newGbpAccountsClientHook clientHook
43
44 // GbpAccountsCallOptions contains the retry settings for each method of GbpAccountsClient.
45 type GbpAccountsCallOptions struct {
46 ListGbpAccounts []gax.CallOption
47 LinkGbpAccount []gax.CallOption
48 }
49
50 func defaultGbpAccountsGRPCClientOptions() []option.ClientOption {
51 return []option.ClientOption{
52 internaloption.WithDefaultEndpoint("merchantapi.googleapis.com:443"),
53 internaloption.WithDefaultEndpointTemplate("merchantapi.UNIVERSE_DOMAIN:443"),
54 internaloption.WithDefaultMTLSEndpoint("merchantapi.mtls.googleapis.com:443"),
55 internaloption.WithDefaultUniverseDomain("googleapis.com"),
56 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
57 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
58 internaloption.EnableJwtWithScope(),
59 internaloption.EnableNewAuthLibrary(),
60 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
61 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
62 }
63 }
64
65 func defaultGbpAccountsCallOptions() *GbpAccountsCallOptions {
66 return &GbpAccountsCallOptions{
67 ListGbpAccounts: []gax.CallOption{
68 gax.WithTimeout(60000 * time.Millisecond),
69 gax.WithRetry(func() gax.Retryer {
70 return gax.OnCodes([]codes.Code{
71 codes.Unavailable,
72 }, gax.Backoff{
73 Initial: 1000 * time.Millisecond,
74 Max: 10000 * time.Millisecond,
75 Multiplier: 1.30,
76 })
77 }),
78 },
79 LinkGbpAccount: []gax.CallOption{
80 gax.WithTimeout(60000 * time.Millisecond),
81 gax.WithRetry(func() gax.Retryer {
82 return gax.OnCodes([]codes.Code{
83 codes.Unavailable,
84 }, gax.Backoff{
85 Initial: 1000 * time.Millisecond,
86 Max: 10000 * time.Millisecond,
87 Multiplier: 1.30,
88 })
89 }),
90 },
91 }
92 }
93
94 func defaultGbpAccountsRESTCallOptions() *GbpAccountsCallOptions {
95 return &GbpAccountsCallOptions{
96 ListGbpAccounts: []gax.CallOption{
97 gax.WithTimeout(60000 * time.Millisecond),
98 gax.WithRetry(func() gax.Retryer {
99 return gax.OnHTTPCodes(gax.Backoff{
100 Initial: 1000 * time.Millisecond,
101 Max: 10000 * time.Millisecond,
102 Multiplier: 1.30,
103 },
104 http.StatusServiceUnavailable)
105 }),
106 },
107 LinkGbpAccount: []gax.CallOption{
108 gax.WithTimeout(60000 * time.Millisecond),
109 gax.WithRetry(func() gax.Retryer {
110 return gax.OnHTTPCodes(gax.Backoff{
111 Initial: 1000 * time.Millisecond,
112 Max: 10000 * time.Millisecond,
113 Multiplier: 1.30,
114 },
115 http.StatusServiceUnavailable)
116 }),
117 },
118 }
119 }
120
121 // internalGbpAccountsClient is an interface that defines the methods available from Merchant API.
122 type internalGbpAccountsClient interface {
123 Close() error
124 setGoogleClientInfo(...string)
125 Connection() *grpc.ClientConn
126 ListGbpAccounts(context.Context, *accountspb.ListGbpAccountsRequest, ...gax.CallOption) *GbpAccountIterator
127 LinkGbpAccount(context.Context, *accountspb.LinkGbpAccountRequest, ...gax.CallOption) (*accountspb.LinkGbpAccountResponse, error)
128 }
129
130 // GbpAccountsClient is a client for interacting with Merchant API.
131 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
132 //
133 // The service facilitates the management of a merchant’s Google Business
134 // Profile (GBP) account settings. This API defines the following resource
135 // model:
136 //
137 // GbpAccount
138 type GbpAccountsClient struct {
139 // The internal transport-dependent client.
140 internalClient internalGbpAccountsClient
141
142 // The call options for this service.
143 CallOptions *GbpAccountsCallOptions
144 }
145
146 // Wrapper methods routed to the internal client.
147
148 // Close closes the connection to the API service. The user should invoke this when
149 // the client is no longer required.
150 func (c *GbpAccountsClient) Close() error {
151 return c.internalClient.Close()
152 }
153
154 // setGoogleClientInfo sets the name and version of the application in
155 // the `x-goog-api-client` header passed on each request. Intended for
156 // use by Google-written clients.
157 func (c *GbpAccountsClient) setGoogleClientInfo(keyval ...string) {
158 c.internalClient.setGoogleClientInfo(keyval...)
159 }
160
161 // Connection returns a connection to the API service.
162 //
163 // Deprecated: Connections are now pooled so this method does not always
164 // return the same resource.
165 func (c *GbpAccountsClient) Connection() *grpc.ClientConn {
166 return c.internalClient.Connection()
167 }
168
169 // ListGbpAccounts list the GBP accounts for a given merchant.
170 func (c *GbpAccountsClient) ListGbpAccounts(ctx context.Context, req *accountspb.ListGbpAccountsRequest, opts ...gax.CallOption) *GbpAccountIterator {
171 return c.internalClient.ListGbpAccounts(ctx, req, opts...)
172 }
173
174 // LinkGbpAccount link the specified merchant to a GBP account for all countries.
175 func (c *GbpAccountsClient) LinkGbpAccount(ctx context.Context, req *accountspb.LinkGbpAccountRequest, opts ...gax.CallOption) (*accountspb.LinkGbpAccountResponse, error) {
176 return c.internalClient.LinkGbpAccount(ctx, req, opts...)
177 }
178
179 // gbpAccountsGRPCClient is a client for interacting with Merchant API over gRPC transport.
180 //
181 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
182 type gbpAccountsGRPCClient struct {
183 // Connection pool of gRPC connections to the service.
184 connPool gtransport.ConnPool
185
186 // Points back to the CallOptions field of the containing GbpAccountsClient
187 CallOptions **GbpAccountsCallOptions
188
189 // The gRPC API client.
190 gbpAccountsClient accountspb.GbpAccountsServiceClient
191
192 // The x-goog-* metadata to be sent with each request.
193 xGoogHeaders []string
194
195 logger *slog.Logger
196 }
197
198 // NewGbpAccountsClient creates a new gbp accounts service client based on gRPC.
199 // The returned client must be Closed when it is done being used to clean up its underlying connections.
200 //
201 // The service facilitates the management of a merchant’s Google Business
202 // Profile (GBP) account settings. This API defines the following resource
203 // model:
204 //
205 // GbpAccount
206 func NewGbpAccountsClient(ctx context.Context, opts ...option.ClientOption) (*GbpAccountsClient, error) {
207 clientOpts := defaultGbpAccountsGRPCClientOptions()
208 if newGbpAccountsClientHook != nil {
209 hookOpts, err := newGbpAccountsClientHook(ctx, clientHookParams{})
210 if err != nil {
211 return nil, err
212 }
213 clientOpts = append(clientOpts, hookOpts...)
214 }
215
216 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
217 if err != nil {
218 return nil, err
219 }
220 client := GbpAccountsClient{CallOptions: defaultGbpAccountsCallOptions()}
221
222 c := &gbpAccountsGRPCClient{
223 connPool: connPool,
224 gbpAccountsClient: accountspb.NewGbpAccountsServiceClient(connPool),
225 CallOptions: &client.CallOptions,
226 logger: internaloption.GetLogger(opts),
227 }
228 c.setGoogleClientInfo()
229
230 client.internalClient = c
231
232 return &client, nil
233 }
234
235 // Connection returns a connection to the API service.
236 //
237 // Deprecated: Connections are now pooled so this method does not always
238 // return the same resource.
239 func (c *gbpAccountsGRPCClient) Connection() *grpc.ClientConn {
240 return c.connPool.Conn()
241 }
242
243 // setGoogleClientInfo sets the name and version of the application in
244 // the `x-goog-api-client` header passed on each request. Intended for
245 // use by Google-written clients.
246 func (c *gbpAccountsGRPCClient) setGoogleClientInfo(keyval ...string) {
247 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
248 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
249 c.xGoogHeaders = []string{
250 "x-goog-api-client", gax.XGoogHeader(kv...),
251 }
252 }
253
254 // Close closes the connection to the API service. The user should invoke this when
255 // the client is no longer required.
256 func (c *gbpAccountsGRPCClient) Close() error {
257 return c.connPool.Close()
258 }
259
260 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
261 type gbpAccountsRESTClient struct {
262 // The http endpoint to connect to.
263 endpoint string
264
265 // The http client.
266 httpClient *http.Client
267
268 // The x-goog-* headers to be sent with each request.
269 xGoogHeaders []string
270
271 // Points back to the CallOptions field of the containing GbpAccountsClient
272 CallOptions **GbpAccountsCallOptions
273
274 logger *slog.Logger
275 }
276
277 // NewGbpAccountsRESTClient creates a new gbp accounts service rest client.
278 //
279 // The service facilitates the management of a merchant’s Google Business
280 // Profile (GBP) account settings. This API defines the following resource
281 // model:
282 //
283 // GbpAccount
284 func NewGbpAccountsRESTClient(ctx context.Context, opts ...option.ClientOption) (*GbpAccountsClient, error) {
285 clientOpts := append(defaultGbpAccountsRESTClientOptions(), opts...)
286 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
287 if err != nil {
288 return nil, err
289 }
290
291 callOpts := defaultGbpAccountsRESTCallOptions()
292 c := &gbpAccountsRESTClient{
293 endpoint: endpoint,
294 httpClient: httpClient,
295 CallOptions: &callOpts,
296 logger: internaloption.GetLogger(opts),
297 }
298 c.setGoogleClientInfo()
299
300 return &GbpAccountsClient{internalClient: c, CallOptions: callOpts}, nil
301 }
302
303 func defaultGbpAccountsRESTClientOptions() []option.ClientOption {
304 return []option.ClientOption{
305 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
306 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
307 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
308 internaloption.WithDefaultUniverseDomain("googleapis.com"),
309 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
310 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
311 internaloption.EnableNewAuthLibrary(),
312 }
313 }
314
315 // setGoogleClientInfo sets the name and version of the application in
316 // the `x-goog-api-client` header passed on each request. Intended for
317 // use by Google-written clients.
318 func (c *gbpAccountsRESTClient) setGoogleClientInfo(keyval ...string) {
319 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
320 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
321 c.xGoogHeaders = []string{
322 "x-goog-api-client", gax.XGoogHeader(kv...),
323 }
324 }
325
326 // Close closes the connection to the API service. The user should invoke this when
327 // the client is no longer required.
328 func (c *gbpAccountsRESTClient) Close() error {
329 // Replace httpClient with nil to force cleanup.
330 c.httpClient = nil
331 return nil
332 }
333
334 // Connection returns a connection to the API service.
335 //
336 // Deprecated: This method always returns nil.
337 func (c *gbpAccountsRESTClient) Connection() *grpc.ClientConn {
338 return nil
339 }
340 func (c *gbpAccountsGRPCClient) ListGbpAccounts(ctx context.Context, req *accountspb.ListGbpAccountsRequest, opts ...gax.CallOption) *GbpAccountIterator {
341 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
342
343 hds = append(c.xGoogHeaders, hds...)
344 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
345 opts = append((*c.CallOptions).ListGbpAccounts[0:len((*c.CallOptions).ListGbpAccounts):len((*c.CallOptions).ListGbpAccounts)], opts...)
346 it := &GbpAccountIterator{}
347 req = proto.Clone(req).(*accountspb.ListGbpAccountsRequest)
348 it.InternalFetch = func(pageSize int, pageToken string) ([]*accountspb.GbpAccount, string, error) {
349 resp := &accountspb.ListGbpAccountsResponse{}
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 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
359 var err error
360 resp, err = executeRPC(ctx, c.gbpAccountsClient.ListGbpAccounts, req, settings.GRPC, c.logger, "ListGbpAccounts")
361 return err
362 }, opts...)
363 if err != nil {
364 return nil, "", err
365 }
366
367 it.Response = resp
368 return resp.GetGbpAccounts(), resp.GetNextPageToken(), nil
369 }
370 fetch := func(pageSize int, pageToken string) (string, error) {
371 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
372 if err != nil {
373 return "", err
374 }
375 it.items = append(it.items, items...)
376 return nextPageToken, nil
377 }
378
379 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
380 it.pageInfo.MaxSize = int(req.GetPageSize())
381 it.pageInfo.Token = req.GetPageToken()
382
383 return it
384 }
385
386 func (c *gbpAccountsGRPCClient) LinkGbpAccount(ctx context.Context, req *accountspb.LinkGbpAccountRequest, opts ...gax.CallOption) (*accountspb.LinkGbpAccountResponse, error) {
387 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
388
389 hds = append(c.xGoogHeaders, hds...)
390 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
391 opts = append((*c.CallOptions).LinkGbpAccount[0:len((*c.CallOptions).LinkGbpAccount):len((*c.CallOptions).LinkGbpAccount)], opts...)
392 var resp *accountspb.LinkGbpAccountResponse
393 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
394 var err error
395 resp, err = executeRPC(ctx, c.gbpAccountsClient.LinkGbpAccount, req, settings.GRPC, c.logger, "LinkGbpAccount")
396 return err
397 }, opts...)
398 if err != nil {
399 return nil, err
400 }
401 return resp, nil
402 }
403
404 // ListGbpAccounts list the GBP accounts for a given merchant.
405 func (c *gbpAccountsRESTClient) ListGbpAccounts(ctx context.Context, req *accountspb.ListGbpAccountsRequest, opts ...gax.CallOption) *GbpAccountIterator {
406 it := &GbpAccountIterator{}
407 req = proto.Clone(req).(*accountspb.ListGbpAccountsRequest)
408 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
409 it.InternalFetch = func(pageSize int, pageToken string) ([]*accountspb.GbpAccount, string, error) {
410 resp := &accountspb.ListGbpAccountsResponse{}
411 if pageToken != "" {
412 req.PageToken = pageToken
413 }
414 if pageSize > math.MaxInt32 {
415 req.PageSize = math.MaxInt32
416 } else if pageSize != 0 {
417 req.PageSize = int32(pageSize)
418 }
419 baseUrl, err := url.Parse(c.endpoint)
420 if err != nil {
421 return nil, "", err
422 }
423 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v/gbpAccounts", req.GetParent())
424
425 params := url.Values{}
426 params.Add("$alt", "json;enum-encoding=int")
427 if req.GetPageSize() != 0 {
428 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
429 }
430 if req.GetPageToken() != "" {
431 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
432 }
433
434 baseUrl.RawQuery = params.Encode()
435
436 // Build HTTP headers from client and context metadata.
437 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
438 headers := gax.BuildHeaders(ctx, hds...)
439 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
440 if settings.Path != "" {
441 baseUrl.Path = settings.Path
442 }
443 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
444 if err != nil {
445 return err
446 }
447 httpReq.Header = headers
448
449 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListGbpAccounts")
450 if err != nil {
451 return err
452 }
453 if err := unm.Unmarshal(buf, resp); err != nil {
454 return err
455 }
456
457 return nil
458 }, opts...)
459 if e != nil {
460 return nil, "", e
461 }
462 it.Response = resp
463 return resp.GetGbpAccounts(), resp.GetNextPageToken(), nil
464 }
465
466 fetch := func(pageSize int, pageToken string) (string, error) {
467 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
468 if err != nil {
469 return "", err
470 }
471 it.items = append(it.items, items...)
472 return nextPageToken, nil
473 }
474
475 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
476 it.pageInfo.MaxSize = int(req.GetPageSize())
477 it.pageInfo.Token = req.GetPageToken()
478
479 return it
480 }
481
482 // LinkGbpAccount link the specified merchant to a GBP account for all countries.
483 func (c *gbpAccountsRESTClient) LinkGbpAccount(ctx context.Context, req *accountspb.LinkGbpAccountRequest, opts ...gax.CallOption) (*accountspb.LinkGbpAccountResponse, error) {
484 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
485 jsonReq, err := m.Marshal(req)
486 if err != nil {
487 return nil, err
488 }
489
490 baseUrl, err := url.Parse(c.endpoint)
491 if err != nil {
492 return nil, err
493 }
494 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v/gbpAccounts:linkGbpAccount", req.GetParent())
495
496 params := url.Values{}
497 params.Add("$alt", "json;enum-encoding=int")
498
499 baseUrl.RawQuery = params.Encode()
500
501 // Build HTTP headers from client and context metadata.
502 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
503
504 hds = append(c.xGoogHeaders, hds...)
505 hds = append(hds, "Content-Type", "application/json")
506 headers := gax.BuildHeaders(ctx, hds...)
507 opts = append((*c.CallOptions).LinkGbpAccount[0:len((*c.CallOptions).LinkGbpAccount):len((*c.CallOptions).LinkGbpAccount)], opts...)
508 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
509 resp := &accountspb.LinkGbpAccountResponse{}
510 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
511 if settings.Path != "" {
512 baseUrl.Path = settings.Path
513 }
514 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
515 if err != nil {
516 return err
517 }
518 httpReq = httpReq.WithContext(ctx)
519 httpReq.Header = headers
520
521 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "LinkGbpAccount")
522 if err != nil {
523 return err
524 }
525
526 if err := unm.Unmarshal(buf, resp); err != nil {
527 return err
528 }
529
530 return nil
531 }, opts...)
532 if e != nil {
533 return nil, e
534 }
535 return resp, nil
536 }