Debian Codegraph @

shopping/merchant/accounts/apiv1beta/business_identity_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/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 newBusinessIdentityClientHook clientHook
41
42 // BusinessIdentityCallOptions contains the retry settings for each method of BusinessIdentityClient.
43 type BusinessIdentityCallOptions struct {
44 GetBusinessIdentity []gax.CallOption
45 UpdateBusinessIdentity []gax.CallOption
46 }
47
48 func defaultBusinessIdentityGRPCClientOptions() []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 defaultBusinessIdentityCallOptions() *BusinessIdentityCallOptions {
64 return &BusinessIdentityCallOptions{
65 GetBusinessIdentity: []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 UpdateBusinessIdentity: []gax.CallOption{
78 gax.WithTimeout(60000 * time.Millisecond),
79 gax.WithRetry(func() gax.Retryer {
80 return gax.OnCodes([]codes.Code{
81 codes.Unavailable,
82 }, gax.Backoff{
83 Initial: 1000 * time.Millisecond,
84 Max: 10000 * time.Millisecond,
85 Multiplier: 1.30,
86 })
87 }),
88 },
89 }
90 }
91
92 func defaultBusinessIdentityRESTCallOptions() *BusinessIdentityCallOptions {
93 return &BusinessIdentityCallOptions{
94 GetBusinessIdentity: []gax.CallOption{
95 gax.WithTimeout(60000 * time.Millisecond),
96 gax.WithRetry(func() gax.Retryer {
97 return gax.OnHTTPCodes(gax.Backoff{
98 Initial: 1000 * time.Millisecond,
99 Max: 10000 * time.Millisecond,
100 Multiplier: 1.30,
101 },
102 http.StatusServiceUnavailable)
103 }),
104 },
105 UpdateBusinessIdentity: []gax.CallOption{
106 gax.WithTimeout(60000 * time.Millisecond),
107 gax.WithRetry(func() gax.Retryer {
108 return gax.OnHTTPCodes(gax.Backoff{
109 Initial: 1000 * time.Millisecond,
110 Max: 10000 * time.Millisecond,
111 Multiplier: 1.30,
112 },
113 http.StatusServiceUnavailable)
114 }),
115 },
116 }
117 }
118
119 // internalBusinessIdentityClient is an interface that defines the methods available from Merchant API.
120 type internalBusinessIdentityClient interface {
121 Close() error
122 setGoogleClientInfo(...string)
123 Connection() *grpc.ClientConn
124 GetBusinessIdentity(context.Context, *accountspb.GetBusinessIdentityRequest, ...gax.CallOption) (*accountspb.BusinessIdentity, error)
125 UpdateBusinessIdentity(context.Context, *accountspb.UpdateBusinessIdentityRequest, ...gax.CallOption) (*accountspb.BusinessIdentity, error)
126 }
127
128 // BusinessIdentityClient is a client for interacting with Merchant API.
129 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
130 //
131 // Service to support business
132 // identity (at https://support.google.com/merchants/answer/12564247) API.
133 type BusinessIdentityClient struct {
134 // The internal transport-dependent client.
135 internalClient internalBusinessIdentityClient
136
137 // The call options for this service.
138 CallOptions *BusinessIdentityCallOptions
139 }
140
141 // Wrapper methods routed to the internal client.
142
143 // Close closes the connection to the API service. The user should invoke this when
144 // the client is no longer required.
145 func (c *BusinessIdentityClient) Close() error {
146 return c.internalClient.Close()
147 }
148
149 // setGoogleClientInfo sets the name and version of the application in
150 // the `x-goog-api-client` header passed on each request. Intended for
151 // use by Google-written clients.
152 func (c *BusinessIdentityClient) setGoogleClientInfo(keyval ...string) {
153 c.internalClient.setGoogleClientInfo(keyval...)
154 }
155
156 // Connection returns a connection to the API service.
157 //
158 // Deprecated: Connections are now pooled so this method does not always
159 // return the same resource.
160 func (c *BusinessIdentityClient) Connection() *grpc.ClientConn {
161 return c.internalClient.Connection()
162 }
163
164 // GetBusinessIdentity retrieves the business identity of an account.
165 func (c *BusinessIdentityClient) GetBusinessIdentity(ctx context.Context, req *accountspb.GetBusinessIdentityRequest, opts ...gax.CallOption) (*accountspb.BusinessIdentity, error) {
166 return c.internalClient.GetBusinessIdentity(ctx, req, opts...)
167 }
168
169 // UpdateBusinessIdentity updates the business identity of an account. Executing this method requires
170 // admin access.
171 func (c *BusinessIdentityClient) UpdateBusinessIdentity(ctx context.Context, req *accountspb.UpdateBusinessIdentityRequest, opts ...gax.CallOption) (*accountspb.BusinessIdentity, error) {
172 return c.internalClient.UpdateBusinessIdentity(ctx, req, opts...)
173 }
174
175 // businessIdentityGRPCClient is a client for interacting with Merchant API over gRPC transport.
176 //
177 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
178 type businessIdentityGRPCClient struct {
179 // Connection pool of gRPC connections to the service.
180 connPool gtransport.ConnPool
181
182 // Points back to the CallOptions field of the containing BusinessIdentityClient
183 CallOptions **BusinessIdentityCallOptions
184
185 // The gRPC API client.
186 businessIdentityClient accountspb.BusinessIdentityServiceClient
187
188 // The x-goog-* metadata to be sent with each request.
189 xGoogHeaders []string
190
191 logger *slog.Logger
192 }
193
194 // NewBusinessIdentityClient creates a new business identity service client based on gRPC.
195 // The returned client must be Closed when it is done being used to clean up its underlying connections.
196 //
197 // Service to support business
198 // identity (at https://support.google.com/merchants/answer/12564247) API.
199 func NewBusinessIdentityClient(ctx context.Context, opts ...option.ClientOption) (*BusinessIdentityClient, error) {
200 clientOpts := defaultBusinessIdentityGRPCClientOptions()
201 if newBusinessIdentityClientHook != nil {
202 hookOpts, err := newBusinessIdentityClientHook(ctx, clientHookParams{})
203 if err != nil {
204 return nil, err
205 }
206 clientOpts = append(clientOpts, hookOpts...)
207 }
208
209 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
210 if err != nil {
211 return nil, err
212 }
213 client := BusinessIdentityClient{CallOptions: defaultBusinessIdentityCallOptions()}
214
215 c := &businessIdentityGRPCClient{
216 connPool: connPool,
217 businessIdentityClient: accountspb.NewBusinessIdentityServiceClient(connPool),
218 CallOptions: &client.CallOptions,
219 logger: internaloption.GetLogger(opts),
220 }
221 c.setGoogleClientInfo()
222
223 client.internalClient = c
224
225 return &client, nil
226 }
227
228 // Connection returns a connection to the API service.
229 //
230 // Deprecated: Connections are now pooled so this method does not always
231 // return the same resource.
232 func (c *businessIdentityGRPCClient) Connection() *grpc.ClientConn {
233 return c.connPool.Conn()
234 }
235
236 // setGoogleClientInfo sets the name and version of the application in
237 // the `x-goog-api-client` header passed on each request. Intended for
238 // use by Google-written clients.
239 func (c *businessIdentityGRPCClient) setGoogleClientInfo(keyval ...string) {
240 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
241 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
242 c.xGoogHeaders = []string{
243 "x-goog-api-client", gax.XGoogHeader(kv...),
244 }
245 }
246
247 // Close closes the connection to the API service. The user should invoke this when
248 // the client is no longer required.
249 func (c *businessIdentityGRPCClient) Close() error {
250 return c.connPool.Close()
251 }
252
253 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
254 type businessIdentityRESTClient struct {
255 // The http endpoint to connect to.
256 endpoint string
257
258 // The http client.
259 httpClient *http.Client
260
261 // The x-goog-* headers to be sent with each request.
262 xGoogHeaders []string
263
264 // Points back to the CallOptions field of the containing BusinessIdentityClient
265 CallOptions **BusinessIdentityCallOptions
266
267 logger *slog.Logger
268 }
269
270 // NewBusinessIdentityRESTClient creates a new business identity service rest client.
271 //
272 // Service to support business
273 // identity (at https://support.google.com/merchants/answer/12564247) API.
274 func NewBusinessIdentityRESTClient(ctx context.Context, opts ...option.ClientOption) (*BusinessIdentityClient, error) {
275 clientOpts := append(defaultBusinessIdentityRESTClientOptions(), opts...)
276 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
277 if err != nil {
278 return nil, err
279 }
280
281 callOpts := defaultBusinessIdentityRESTCallOptions()
282 c := &businessIdentityRESTClient{
283 endpoint: endpoint,
284 httpClient: httpClient,
285 CallOptions: &callOpts,
286 logger: internaloption.GetLogger(opts),
287 }
288 c.setGoogleClientInfo()
289
290 return &BusinessIdentityClient{internalClient: c, CallOptions: callOpts}, nil
291 }
292
293 func defaultBusinessIdentityRESTClientOptions() []option.ClientOption {
294 return []option.ClientOption{
295 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
296 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
297 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
298 internaloption.WithDefaultUniverseDomain("googleapis.com"),
299 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
300 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
301 internaloption.EnableNewAuthLibrary(),
302 }
303 }
304
305 // setGoogleClientInfo sets the name and version of the application in
306 // the `x-goog-api-client` header passed on each request. Intended for
307 // use by Google-written clients.
308 func (c *businessIdentityRESTClient) setGoogleClientInfo(keyval ...string) {
309 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
310 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
311 c.xGoogHeaders = []string{
312 "x-goog-api-client", gax.XGoogHeader(kv...),
313 }
314 }
315
316 // Close closes the connection to the API service. The user should invoke this when
317 // the client is no longer required.
318 func (c *businessIdentityRESTClient) Close() error {
319 // Replace httpClient with nil to force cleanup.
320 c.httpClient = nil
321 return nil
322 }
323
324 // Connection returns a connection to the API service.
325 //
326 // Deprecated: This method always returns nil.
327 func (c *businessIdentityRESTClient) Connection() *grpc.ClientConn {
328 return nil
329 }
330 func (c *businessIdentityGRPCClient) GetBusinessIdentity(ctx context.Context, req *accountspb.GetBusinessIdentityRequest, opts ...gax.CallOption) (*accountspb.BusinessIdentity, error) {
331 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
332
333 hds = append(c.xGoogHeaders, hds...)
334 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
335 opts = append((*c.CallOptions).GetBusinessIdentity[0:len((*c.CallOptions).GetBusinessIdentity):len((*c.CallOptions).GetBusinessIdentity)], opts...)
336 var resp *accountspb.BusinessIdentity
337 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
338 var err error
339 resp, err = executeRPC(ctx, c.businessIdentityClient.GetBusinessIdentity, req, settings.GRPC, c.logger, "GetBusinessIdentity")
340 return err
341 }, opts...)
342 if err != nil {
343 return nil, err
344 }
345 return resp, nil
346 }
347
348 func (c *businessIdentityGRPCClient) UpdateBusinessIdentity(ctx context.Context, req *accountspb.UpdateBusinessIdentityRequest, opts ...gax.CallOption) (*accountspb.BusinessIdentity, error) {
349 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "business_identity.name", url.QueryEscape(req.GetBusinessIdentity().GetName()))}
350
351 hds = append(c.xGoogHeaders, hds...)
352 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
353 opts = append((*c.CallOptions).UpdateBusinessIdentity[0:len((*c.CallOptions).UpdateBusinessIdentity):len((*c.CallOptions).UpdateBusinessIdentity)], opts...)
354 var resp *accountspb.BusinessIdentity
355 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
356 var err error
357 resp, err = executeRPC(ctx, c.businessIdentityClient.UpdateBusinessIdentity, req, settings.GRPC, c.logger, "UpdateBusinessIdentity")
358 return err
359 }, opts...)
360 if err != nil {
361 return nil, err
362 }
363 return resp, nil
364 }
365
366 // GetBusinessIdentity retrieves the business identity of an account.
367 func (c *businessIdentityRESTClient) GetBusinessIdentity(ctx context.Context, req *accountspb.GetBusinessIdentityRequest, opts ...gax.CallOption) (*accountspb.BusinessIdentity, error) {
368 baseUrl, err := url.Parse(c.endpoint)
369 if err != nil {
370 return nil, err
371 }
372 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v", req.GetName())
373
374 params := url.Values{}
375 params.Add("$alt", "json;enum-encoding=int")
376
377 baseUrl.RawQuery = params.Encode()
378
379 // Build HTTP headers from client and context metadata.
380 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
381
382 hds = append(c.xGoogHeaders, hds...)
383 hds = append(hds, "Content-Type", "application/json")
384 headers := gax.BuildHeaders(ctx, hds...)
385 opts = append((*c.CallOptions).GetBusinessIdentity[0:len((*c.CallOptions).GetBusinessIdentity):len((*c.CallOptions).GetBusinessIdentity)], opts...)
386 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
387 resp := &accountspb.BusinessIdentity{}
388 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
389 if settings.Path != "" {
390 baseUrl.Path = settings.Path
391 }
392 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
393 if err != nil {
394 return err
395 }
396 httpReq = httpReq.WithContext(ctx)
397 httpReq.Header = headers
398
399 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetBusinessIdentity")
400 if err != nil {
401 return err
402 }
403
404 if err := unm.Unmarshal(buf, resp); err != nil {
405 return err
406 }
407
408 return nil
409 }, opts...)
410 if e != nil {
411 return nil, e
412 }
413 return resp, nil
414 }
415
416 // UpdateBusinessIdentity updates the business identity of an account. Executing this method requires
417 // admin access.
418 func (c *businessIdentityRESTClient) UpdateBusinessIdentity(ctx context.Context, req *accountspb.UpdateBusinessIdentityRequest, opts ...gax.CallOption) (*accountspb.BusinessIdentity, error) {
419 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
420 body := req.GetBusinessIdentity()
421 jsonReq, err := m.Marshal(body)
422 if err != nil {
423 return nil, err
424 }
425
426 baseUrl, err := url.Parse(c.endpoint)
427 if err != nil {
428 return nil, err
429 }
430 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v", req.GetBusinessIdentity().GetName())
431
432 params := url.Values{}
433 params.Add("$alt", "json;enum-encoding=int")
434 if req.GetUpdateMask() != nil {
435 field, err := protojson.Marshal(req.GetUpdateMask())
436 if err != nil {
437 return nil, err
438 }
439 params.Add("updateMask", string(field[1:len(field)-1]))
440 }
441
442 baseUrl.RawQuery = params.Encode()
443
444 // Build HTTP headers from client and context metadata.
445 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "business_identity.name", url.QueryEscape(req.GetBusinessIdentity().GetName()))}
446
447 hds = append(c.xGoogHeaders, hds...)
448 hds = append(hds, "Content-Type", "application/json")
449 headers := gax.BuildHeaders(ctx, hds...)
450 opts = append((*c.CallOptions).UpdateBusinessIdentity[0:len((*c.CallOptions).UpdateBusinessIdentity):len((*c.CallOptions).UpdateBusinessIdentity)], opts...)
451 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
452 resp := &accountspb.BusinessIdentity{}
453 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
454 if settings.Path != "" {
455 baseUrl.Path = settings.Path
456 }
457 httpReq, err := http.NewRequest("PATCH", baseUrl.String(), bytes.NewReader(jsonReq))
458 if err != nil {
459 return err
460 }
461 httpReq = httpReq.WithContext(ctx)
462 httpReq.Header = headers
463
464 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "UpdateBusinessIdentity")
465 if err != nil {
466 return err
467 }
468
469 if err := unm.Unmarshal(buf, resp); err != nil {
470 return err
471 }
472
473 return nil
474 }, opts...)
475 if e != nil {
476 return nil, e
477 }
478 return resp, nil
479 }