Debian Codegraph @

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