Debian Codegraph @

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