Debian Codegraph @

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