Debian Codegraph @

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