Debian Codegraph @

shopping/merchant/accounts/apiv1beta/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
28 accountspb "cloud.google.com/go/shopping/merchant/accounts/apiv1beta/accountspb"
29 gax "github.com/googleapis/gax-go/v2"
30 "google.golang.org/api/option"
31 "google.golang.org/api/option/internaloption"
32 gtransport "google.golang.org/api/transport/grpc"
33 httptransport "google.golang.org/api/transport/http"
34 "google.golang.org/grpc"
35 "google.golang.org/protobuf/encoding/protojson"
36 )
37
38 var newShippingSettingsClientHook clientHook
39
40 // ShippingSettingsCallOptions contains the retry settings for each method of ShippingSettingsClient.
41 type ShippingSettingsCallOptions struct {
42 GetShippingSettings []gax.CallOption
43 InsertShippingSettings []gax.CallOption
44 }
45
46 func defaultShippingSettingsGRPCClientOptions() []option.ClientOption {
47 return []option.ClientOption{
48 internaloption.WithDefaultEndpoint("merchantapi.googleapis.com:443"),
49 internaloption.WithDefaultEndpointTemplate("merchantapi.UNIVERSE_DOMAIN:443"),
50 internaloption.WithDefaultMTLSEndpoint("merchantapi.mtls.googleapis.com:443"),
51 internaloption.WithDefaultUniverseDomain("googleapis.com"),
52 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
53 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
54 internaloption.EnableJwtWithScope(),
55 internaloption.EnableNewAuthLibrary(),
56 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
57 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
58 }
59 }
60
61 func defaultShippingSettingsCallOptions() *ShippingSettingsCallOptions {
62 return &ShippingSettingsCallOptions{
63 GetShippingSettings: []gax.CallOption{},
64 InsertShippingSettings: []gax.CallOption{},
65 }
66 }
67
68 func defaultShippingSettingsRESTCallOptions() *ShippingSettingsCallOptions {
69 return &ShippingSettingsCallOptions{
70 GetShippingSettings: []gax.CallOption{},
71 InsertShippingSettings: []gax.CallOption{},
72 }
73 }
74
75 // internalShippingSettingsClient is an interface that defines the methods available from Merchant API.
76 type internalShippingSettingsClient interface {
77 Close() error
78 setGoogleClientInfo(...string)
79 Connection() *grpc.ClientConn
80 GetShippingSettings(context.Context, *accountspb.GetShippingSettingsRequest, ...gax.CallOption) (*accountspb.ShippingSettings, error)
81 InsertShippingSettings(context.Context, *accountspb.InsertShippingSettingsRequest, ...gax.CallOption) (*accountspb.ShippingSettings, error)
82 }
83
84 // ShippingSettingsClient is a client for interacting with Merchant API.
85 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
86 //
87 // Service to get method call shipping setting information per Merchant API
88 // method.
89 type ShippingSettingsClient struct {
90 // The internal transport-dependent client.
91 internalClient internalShippingSettingsClient
92
93 // The call options for this service.
94 CallOptions *ShippingSettingsCallOptions
95 }
96
97 // Wrapper methods routed to the internal client.
98
99 // Close closes the connection to the API service. The user should invoke this when
100 // the client is no longer required.
101 func (c *ShippingSettingsClient) Close() error {
102 return c.internalClient.Close()
103 }
104
105 // setGoogleClientInfo sets the name and version of the application in
106 // the `x-goog-api-client` header passed on each request. Intended for
107 // use by Google-written clients.
108 func (c *ShippingSettingsClient) setGoogleClientInfo(keyval ...string) {
109 c.internalClient.setGoogleClientInfo(keyval...)
110 }
111
112 // Connection returns a connection to the API service.
113 //
114 // Deprecated: Connections are now pooled so this method does not always
115 // return the same resource.
116 func (c *ShippingSettingsClient) Connection() *grpc.ClientConn {
117 return c.internalClient.Connection()
118 }
119
120 // GetShippingSettings retrieve shipping setting information.
121 func (c *ShippingSettingsClient) GetShippingSettings(ctx context.Context, req *accountspb.GetShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
122 return c.internalClient.GetShippingSettings(ctx, req, opts...)
123 }
124
125 // InsertShippingSettings replace the shipping setting of a merchant with the request shipping
126 // setting. Executing this method requires admin access.
127 func (c *ShippingSettingsClient) InsertShippingSettings(ctx context.Context, req *accountspb.InsertShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
128 return c.internalClient.InsertShippingSettings(ctx, req, opts...)
129 }
130
131 // shippingSettingsGRPCClient is a client for interacting with Merchant API over gRPC transport.
132 //
133 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
134 type shippingSettingsGRPCClient struct {
135 // Connection pool of gRPC connections to the service.
136 connPool gtransport.ConnPool
137
138 // Points back to the CallOptions field of the containing ShippingSettingsClient
139 CallOptions **ShippingSettingsCallOptions
140
141 // The gRPC API client.
142 shippingSettingsClient accountspb.ShippingSettingsServiceClient
143
144 // The x-goog-* metadata to be sent with each request.
145 xGoogHeaders []string
146
147 logger *slog.Logger
148 }
149
150 // NewShippingSettingsClient creates a new shipping settings service client based on gRPC.
151 // The returned client must be Closed when it is done being used to clean up its underlying connections.
152 //
153 // Service to get method call shipping setting information per Merchant API
154 // method.
155 func NewShippingSettingsClient(ctx context.Context, opts ...option.ClientOption) (*ShippingSettingsClient, error) {
156 clientOpts := defaultShippingSettingsGRPCClientOptions()
157 if newShippingSettingsClientHook != nil {
158 hookOpts, err := newShippingSettingsClientHook(ctx, clientHookParams{})
159 if err != nil {
160 return nil, err
161 }
162 clientOpts = append(clientOpts, hookOpts...)
163 }
164
165 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
166 if err != nil {
167 return nil, err
168 }
169 client := ShippingSettingsClient{CallOptions: defaultShippingSettingsCallOptions()}
170
171 c := &shippingSettingsGRPCClient{
172 connPool: connPool,
173 shippingSettingsClient: accountspb.NewShippingSettingsServiceClient(connPool),
174 CallOptions: &client.CallOptions,
175 logger: internaloption.GetLogger(opts),
176 }
177 c.setGoogleClientInfo()
178
179 client.internalClient = c
180
181 return &client, nil
182 }
183
184 // Connection returns a connection to the API service.
185 //
186 // Deprecated: Connections are now pooled so this method does not always
187 // return the same resource.
188 func (c *shippingSettingsGRPCClient) Connection() *grpc.ClientConn {
189 return c.connPool.Conn()
190 }
191
192 // setGoogleClientInfo sets the name and version of the application in
193 // the `x-goog-api-client` header passed on each request. Intended for
194 // use by Google-written clients.
195 func (c *shippingSettingsGRPCClient) setGoogleClientInfo(keyval ...string) {
196 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
197 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
198 c.xGoogHeaders = []string{
199 "x-goog-api-client", gax.XGoogHeader(kv...),
200 }
201 }
202
203 // Close closes the connection to the API service. The user should invoke this when
204 // the client is no longer required.
205 func (c *shippingSettingsGRPCClient) Close() error {
206 return c.connPool.Close()
207 }
208
209 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
210 type shippingSettingsRESTClient struct {
211 // The http endpoint to connect to.
212 endpoint string
213
214 // The http client.
215 httpClient *http.Client
216
217 // The x-goog-* headers to be sent with each request.
218 xGoogHeaders []string
219
220 // Points back to the CallOptions field of the containing ShippingSettingsClient
221 CallOptions **ShippingSettingsCallOptions
222
223 logger *slog.Logger
224 }
225
226 // NewShippingSettingsRESTClient creates a new shipping settings service rest client.
227 //
228 // Service to get method call shipping setting information per Merchant API
229 // method.
230 func NewShippingSettingsRESTClient(ctx context.Context, opts ...option.ClientOption) (*ShippingSettingsClient, error) {
231 clientOpts := append(defaultShippingSettingsRESTClientOptions(), opts...)
232 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
233 if err != nil {
234 return nil, err
235 }
236
237 callOpts := defaultShippingSettingsRESTCallOptions()
238 c := &shippingSettingsRESTClient{
239 endpoint: endpoint,
240 httpClient: httpClient,
241 CallOptions: &callOpts,
242 logger: internaloption.GetLogger(opts),
243 }
244 c.setGoogleClientInfo()
245
246 return &ShippingSettingsClient{internalClient: c, CallOptions: callOpts}, nil
247 }
248
249 func defaultShippingSettingsRESTClientOptions() []option.ClientOption {
250 return []option.ClientOption{
251 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
252 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
253 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
254 internaloption.WithDefaultUniverseDomain("googleapis.com"),
255 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
256 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
257 internaloption.EnableNewAuthLibrary(),
258 }
259 }
260
261 // setGoogleClientInfo sets the name and version of the application in
262 // the `x-goog-api-client` header passed on each request. Intended for
263 // use by Google-written clients.
264 func (c *shippingSettingsRESTClient) setGoogleClientInfo(keyval ...string) {
265 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
266 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
267 c.xGoogHeaders = []string{
268 "x-goog-api-client", gax.XGoogHeader(kv...),
269 }
270 }
271
272 // Close closes the connection to the API service. The user should invoke this when
273 // the client is no longer required.
274 func (c *shippingSettingsRESTClient) Close() error {
275 // Replace httpClient with nil to force cleanup.
276 c.httpClient = nil
277 return nil
278 }
279
280 // Connection returns a connection to the API service.
281 //
282 // Deprecated: This method always returns nil.
283 func (c *shippingSettingsRESTClient) Connection() *grpc.ClientConn {
284 return nil
285 }
286 func (c *shippingSettingsGRPCClient) GetShippingSettings(ctx context.Context, req *accountspb.GetShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
287 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
288
289 hds = append(c.xGoogHeaders, hds...)
290 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
291 opts = append((*c.CallOptions).GetShippingSettings[0:len((*c.CallOptions).GetShippingSettings):len((*c.CallOptions).GetShippingSettings)], opts...)
292 var resp *accountspb.ShippingSettings
293 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
294 var err error
295 resp, err = executeRPC(ctx, c.shippingSettingsClient.GetShippingSettings, req, settings.GRPC, c.logger, "GetShippingSettings")
296 return err
297 }, opts...)
298 if err != nil {
299 return nil, err
300 }
301 return resp, nil
302 }
303
304 func (c *shippingSettingsGRPCClient) InsertShippingSettings(ctx context.Context, req *accountspb.InsertShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
305 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
306
307 hds = append(c.xGoogHeaders, hds...)
308 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
309 opts = append((*c.CallOptions).InsertShippingSettings[0:len((*c.CallOptions).InsertShippingSettings):len((*c.CallOptions).InsertShippingSettings)], opts...)
310 var resp *accountspb.ShippingSettings
311 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
312 var err error
313 resp, err = executeRPC(ctx, c.shippingSettingsClient.InsertShippingSettings, req, settings.GRPC, c.logger, "InsertShippingSettings")
314 return err
315 }, opts...)
316 if err != nil {
317 return nil, err
318 }
319 return resp, nil
320 }
321
322 // GetShippingSettings retrieve shipping setting information.
323 func (c *shippingSettingsRESTClient) GetShippingSettings(ctx context.Context, req *accountspb.GetShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
324 baseUrl, err := url.Parse(c.endpoint)
325 if err != nil {
326 return nil, err
327 }
328 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v", req.GetName())
329
330 params := url.Values{}
331 params.Add("$alt", "json;enum-encoding=int")
332
333 baseUrl.RawQuery = params.Encode()
334
335 // Build HTTP headers from client and context metadata.
336 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
337
338 hds = append(c.xGoogHeaders, hds...)
339 hds = append(hds, "Content-Type", "application/json")
340 headers := gax.BuildHeaders(ctx, hds...)
341 opts = append((*c.CallOptions).GetShippingSettings[0:len((*c.CallOptions).GetShippingSettings):len((*c.CallOptions).GetShippingSettings)], opts...)
342 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
343 resp := &accountspb.ShippingSettings{}
344 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
345 if settings.Path != "" {
346 baseUrl.Path = settings.Path
347 }
348 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
349 if err != nil {
350 return err
351 }
352 httpReq = httpReq.WithContext(ctx)
353 httpReq.Header = headers
354
355 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetShippingSettings")
356 if err != nil {
357 return err
358 }
359
360 if err := unm.Unmarshal(buf, resp); err != nil {
361 return err
362 }
363
364 return nil
365 }, opts...)
366 if e != nil {
367 return nil, e
368 }
369 return resp, nil
370 }
371
372 // InsertShippingSettings replace the shipping setting of a merchant with the request shipping
373 // setting. Executing this method requires admin access.
374 func (c *shippingSettingsRESTClient) InsertShippingSettings(ctx context.Context, req *accountspb.InsertShippingSettingsRequest, opts ...gax.CallOption) (*accountspb.ShippingSettings, error) {
375 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
376 body := req.GetShippingSetting()
377 jsonReq, err := m.Marshal(body)
378 if err != nil {
379 return nil, err
380 }
381
382 baseUrl, err := url.Parse(c.endpoint)
383 if err != nil {
384 return nil, err
385 }
386 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v/shippingSettings:insert", req.GetParent())
387
388 params := url.Values{}
389 params.Add("$alt", "json;enum-encoding=int")
390
391 baseUrl.RawQuery = params.Encode()
392
393 // Build HTTP headers from client and context metadata.
394 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
395
396 hds = append(c.xGoogHeaders, hds...)
397 hds = append(hds, "Content-Type", "application/json")
398 headers := gax.BuildHeaders(ctx, hds...)
399 opts = append((*c.CallOptions).InsertShippingSettings[0:len((*c.CallOptions).InsertShippingSettings):len((*c.CallOptions).InsertShippingSettings)], opts...)
400 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
401 resp := &accountspb.ShippingSettings{}
402 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
403 if settings.Path != "" {
404 baseUrl.Path = settings.Path
405 }
406 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
407 if err != nil {
408 return err
409 }
410 httpReq = httpReq.WithContext(ctx)
411 httpReq.Header = headers
412
413 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "InsertShippingSettings")
414 if err != nil {
415 return err
416 }
417
418 if err := unm.Unmarshal(buf, resp); err != nil {
419 return err
420 }
421
422 return nil
423 }, opts...)
424 if e != nil {
425 return nil, e
426 }
427 return resp, nil
428 }