Debian Codegraph @

shopping/merchant/accounts/apiv1/shipping_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/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 newShippingSettingsClientHook clientHook
41
42 // ShippingSettingsCallOptions contains the retry settings for each method of ShippingSettingsClient.
43 type ShippingSettingsCallOptions struct {
44 GetShippingSettings []gax.CallOption
45 InsertShippingSettings []gax.CallOption
46 }
47
48 func defaultShippingSettingsGRPCClientOptions() []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 defaultShippingSettingsCallOptions() *ShippingSettingsCallOptions {
64 return &ShippingSettingsCallOptions{
65 GetShippingSettings: []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 InsertShippingSettings: []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 defaultShippingSettingsRESTCallOptions() *ShippingSettingsCallOptions {
93 return &ShippingSettingsCallOptions{
94 GetShippingSettings: []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 InsertShippingSettings: []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 // internalShippingSettingsClient is an interface that defines the methods available from Merchant API.
120 type internalShippingSettingsClient interface {
121 Close() error
122 setGoogleClientInfo(...string)
123 Connection() *grpc.ClientConn
124 GetShippingSettings(context.Context, *accountspb.GetShippingSettingsRequest, ...gax.CallOption) (*accountspb.ShippingSettings, error)
125 InsertShippingSettings(context.Context, *accountspb.InsertShippingSettingsRequest, ...gax.CallOption) (*accountspb.ShippingSettings, error)
126 }
127
128 // ShippingSettingsClient 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 get method call shipping setting information per Merchant API
132 // method.
133 type ShippingSettingsClient struct {
134 // The internal transport-dependent client.
135 internalClient internalShippingSettingsClient
136
137 // The call options for this service.
138 CallOptions *ShippingSettingsCallOptions
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 *ShippingSettingsClient) 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 *ShippingSettingsClient) 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 *ShippingSettingsClient) Connection() *grpc.ClientConn {
161 return c.internalClient.Connection()
162 }
163
164 // GetShippingSettings retrieve shipping setting information.
165 func (c *ShippingSettingsClient) GetShippingSettings(ctx context.Context, req *accountspb.GetShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
166 return c.internalClient.GetShippingSettings(ctx, req, opts...)
167 }
168
169 // InsertShippingSettings replace the shipping setting of a business with the request shipping
170 // setting. Executing this method requires admin access.
171 func (c *ShippingSettingsClient) InsertShippingSettings(ctx context.Context, req *accountspb.InsertShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
172 return c.internalClient.InsertShippingSettings(ctx, req, opts...)
173 }
174
175 // shippingSettingsGRPCClient 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 shippingSettingsGRPCClient 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 ShippingSettingsClient
183 CallOptions **ShippingSettingsCallOptions
184
185 // The gRPC API client.
186 shippingSettingsClient accountspb.ShippingSettingsServiceClient
187
188 // The x-goog-* metadata to be sent with each request.
189 xGoogHeaders []string
190
191 logger *slog.Logger
192 }
193
194 // NewShippingSettingsClient creates a new shipping settings 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 get method call shipping setting information per Merchant API
198 // method.
199 func NewShippingSettingsClient(ctx context.Context, opts ...option.ClientOption) (*ShippingSettingsClient, error) {
200 clientOpts := defaultShippingSettingsGRPCClientOptions()
201 if newShippingSettingsClientHook != nil {
202 hookOpts, err := newShippingSettingsClientHook(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 := ShippingSettingsClient{CallOptions: defaultShippingSettingsCallOptions()}
214
215 c := &shippingSettingsGRPCClient{
216 connPool: connPool,
217 shippingSettingsClient: accountspb.NewShippingSettingsServiceClient(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 *shippingSettingsGRPCClient) 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 *shippingSettingsGRPCClient) 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 *shippingSettingsGRPCClient) 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 shippingSettingsRESTClient 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 ShippingSettingsClient
265 CallOptions **ShippingSettingsCallOptions
266
267 logger *slog.Logger
268 }
269
270 // NewShippingSettingsRESTClient creates a new shipping settings service rest client.
271 //
272 // Service to get method call shipping setting information per Merchant API
273 // method.
274 func NewShippingSettingsRESTClient(ctx context.Context, opts ...option.ClientOption) (*ShippingSettingsClient, error) {
275 clientOpts := append(defaultShippingSettingsRESTClientOptions(), opts...)
276 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
277 if err != nil {
278 return nil, err
279 }
280
281 callOpts := defaultShippingSettingsRESTCallOptions()
282 c := &shippingSettingsRESTClient{
283 endpoint: endpoint,
284 httpClient: httpClient,
285 CallOptions: &callOpts,
286 logger: internaloption.GetLogger(opts),
287 }
288 c.setGoogleClientInfo()
289
290 return &ShippingSettingsClient{internalClient: c, CallOptions: callOpts}, nil
291 }
292
293 func defaultShippingSettingsRESTClientOptions() []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 *shippingSettingsRESTClient) 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 *shippingSettingsRESTClient) 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 *shippingSettingsRESTClient) Connection() *grpc.ClientConn {
328 return nil
329 }
330 func (c *shippingSettingsGRPCClient) GetShippingSettings(ctx context.Context, req *accountspb.GetShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, 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).GetShippingSettings[0:len((*c.CallOptions).GetShippingSettings):len((*c.CallOptions).GetShippingSettings)], opts...)
336 var resp *accountspb.ShippingSettings
337 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
338 var err error
339 resp, err = executeRPC(ctx, c.shippingSettingsClient.GetShippingSettings, req, settings.GRPC, c.logger, "GetShippingSettings")
340 return err
341 }, opts...)
342 if err != nil {
343 return nil, err
344 }
345 return resp, nil
346 }
347
348 func (c *shippingSettingsGRPCClient) InsertShippingSettings(ctx context.Context, req *accountspb.InsertShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
349 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
350
351 hds = append(c.xGoogHeaders, hds...)
352 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
353 opts = append((*c.CallOptions).InsertShippingSettings[0:len((*c.CallOptions).InsertShippingSettings):len((*c.CallOptions).InsertShippingSettings)], opts...)
354 var resp *accountspb.ShippingSettings
355 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
356 var err error
357 resp, err = executeRPC(ctx, c.shippingSettingsClient.InsertShippingSettings, req, settings.GRPC, c.logger, "InsertShippingSettings")
358 return err
359 }, opts...)
360 if err != nil {
361 return nil, err
362 }
363 return resp, nil
364 }
365
366 // GetShippingSettings retrieve shipping setting information.
367 func (c *shippingSettingsRESTClient) GetShippingSettings(ctx context.Context, req *accountspb.GetShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
368 baseUrl, err := url.Parse(c.endpoint)
369 if err != nil {
370 return nil, err
371 }
372 baseUrl.Path += fmt.Sprintf("/accounts/v1/%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).GetShippingSettings[0:len((*c.CallOptions).GetShippingSettings):len((*c.CallOptions).GetShippingSettings)], opts...)
386 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
387 resp := &accountspb.ShippingSettings{}
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, "GetShippingSettings")
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 // InsertShippingSettings replace the shipping setting of a business with the request shipping
417 // setting. Executing this method requires admin access.
418 func (c *shippingSettingsRESTClient) InsertShippingSettings(ctx context.Context, req *accountspb.InsertShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
419 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
420 body := req.GetShippingSetting()
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/v1/%v/shippingSettings:insert", req.GetParent())
431
432 params := url.Values{}
433 params.Add("$alt", "json;enum-encoding=int")
434
435 baseUrl.RawQuery = params.Encode()
436
437 // Build HTTP headers from client and context metadata.
438 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
439
440 hds = append(c.xGoogHeaders, hds...)
441 hds = append(hds, "Content-Type", "application/json")
442 headers := gax.BuildHeaders(ctx, hds...)
443 opts = append((*c.CallOptions).InsertShippingSettings[0:len((*c.CallOptions).InsertShippingSettings):len((*c.CallOptions).InsertShippingSettings)], opts...)
444 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
445 resp := &accountspb.ShippingSettings{}
446 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
447 if settings.Path != "" {
448 baseUrl.Path = settings.Path
449 }
450 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
451 if err != nil {
452 return err
453 }
454 httpReq = httpReq.WithContext(ctx)
455 httpReq.Header = headers
456
457 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "InsertShippingSettings")
458 if err != nil {
459 return err
460 }
461
462 if err := unm.Unmarshal(buf, resp); err != nil {
463 return err
464 }
465
466 return nil
467 }, opts...)
468 if e != nil {
469 return nil, e
470 }
471 return resp, nil
472 }