Debian Codegraph @

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