Debian Codegraph @

shopping/merchant/accounts/apiv1beta/user_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/iterator"
32 "google.golang.org/api/option"
33 "google.golang.org/api/option/internaloption"
34 gtransport "google.golang.org/api/transport/grpc"
35 httptransport "google.golang.org/api/transport/http"
36 "google.golang.org/grpc"
37 "google.golang.org/grpc/codes"
38 "google.golang.org/protobuf/encoding/protojson"
39 "google.golang.org/protobuf/proto"
40 )
41
42 var newUserClientHook clientHook
43
44 // UserCallOptions contains the retry settings for each method of UserClient.
45 type UserCallOptions struct {
46 GetUser []gax.CallOption
47 CreateUser []gax.CallOption
48 DeleteUser []gax.CallOption
49 UpdateUser []gax.CallOption
50 ListUsers []gax.CallOption
51 }
52
53 func defaultUserGRPCClientOptions() []option.ClientOption {
54 return []option.ClientOption{
55 internaloption.WithDefaultEndpoint("merchantapi.googleapis.com:443"),
56 internaloption.WithDefaultEndpointTemplate("merchantapi.UNIVERSE_DOMAIN:443"),
57 internaloption.WithDefaultMTLSEndpoint("merchantapi.mtls.googleapis.com:443"),
58 internaloption.WithDefaultUniverseDomain("googleapis.com"),
59 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
60 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
61 internaloption.EnableJwtWithScope(),
62 internaloption.EnableNewAuthLibrary(),
63 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
64 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
65 }
66 }
67
68 func defaultUserCallOptions() *UserCallOptions {
69 return &UserCallOptions{
70 GetUser: []gax.CallOption{
71 gax.WithTimeout(60000 * time.Millisecond),
72 gax.WithRetry(func() gax.Retryer {
73 return gax.OnCodes([]codes.Code{
74 codes.Unavailable,
75 }, gax.Backoff{
76 Initial: 1000 * time.Millisecond,
77 Max: 10000 * time.Millisecond,
78 Multiplier: 1.30,
79 })
80 }),
81 },
82 CreateUser: []gax.CallOption{
83 gax.WithTimeout(60000 * time.Millisecond),
84 gax.WithRetry(func() gax.Retryer {
85 return gax.OnCodes([]codes.Code{
86 codes.Unavailable,
87 }, gax.Backoff{
88 Initial: 1000 * time.Millisecond,
89 Max: 10000 * time.Millisecond,
90 Multiplier: 1.30,
91 })
92 }),
93 },
94 DeleteUser: []gax.CallOption{
95 gax.WithTimeout(60000 * time.Millisecond),
96 gax.WithRetry(func() gax.Retryer {
97 return gax.OnCodes([]codes.Code{
98 codes.Unavailable,
99 }, gax.Backoff{
100 Initial: 1000 * time.Millisecond,
101 Max: 10000 * time.Millisecond,
102 Multiplier: 1.30,
103 })
104 }),
105 },
106 UpdateUser: []gax.CallOption{
107 gax.WithTimeout(60000 * time.Millisecond),
108 gax.WithRetry(func() gax.Retryer {
109 return gax.OnCodes([]codes.Code{
110 codes.Unavailable,
111 }, gax.Backoff{
112 Initial: 1000 * time.Millisecond,
113 Max: 10000 * time.Millisecond,
114 Multiplier: 1.30,
115 })
116 }),
117 },
118 ListUsers: []gax.CallOption{
119 gax.WithTimeout(60000 * time.Millisecond),
120 gax.WithRetry(func() gax.Retryer {
121 return gax.OnCodes([]codes.Code{
122 codes.Unavailable,
123 }, gax.Backoff{
124 Initial: 1000 * time.Millisecond,
125 Max: 10000 * time.Millisecond,
126 Multiplier: 1.30,
127 })
128 }),
129 },
130 }
131 }
132
133 func defaultUserRESTCallOptions() *UserCallOptions {
134 return &UserCallOptions{
135 GetUser: []gax.CallOption{
136 gax.WithTimeout(60000 * time.Millisecond),
137 gax.WithRetry(func() gax.Retryer {
138 return gax.OnHTTPCodes(gax.Backoff{
139 Initial: 1000 * time.Millisecond,
140 Max: 10000 * time.Millisecond,
141 Multiplier: 1.30,
142 },
143 http.StatusServiceUnavailable)
144 }),
145 },
146 CreateUser: []gax.CallOption{
147 gax.WithTimeout(60000 * time.Millisecond),
148 gax.WithRetry(func() gax.Retryer {
149 return gax.OnHTTPCodes(gax.Backoff{
150 Initial: 1000 * time.Millisecond,
151 Max: 10000 * time.Millisecond,
152 Multiplier: 1.30,
153 },
154 http.StatusServiceUnavailable)
155 }),
156 },
157 DeleteUser: []gax.CallOption{
158 gax.WithTimeout(60000 * time.Millisecond),
159 gax.WithRetry(func() gax.Retryer {
160 return gax.OnHTTPCodes(gax.Backoff{
161 Initial: 1000 * time.Millisecond,
162 Max: 10000 * time.Millisecond,
163 Multiplier: 1.30,
164 },
165 http.StatusServiceUnavailable)
166 }),
167 },
168 UpdateUser: []gax.CallOption{
169 gax.WithTimeout(60000 * time.Millisecond),
170 gax.WithRetry(func() gax.Retryer {
171 return gax.OnHTTPCodes(gax.Backoff{
172 Initial: 1000 * time.Millisecond,
173 Max: 10000 * time.Millisecond,
174 Multiplier: 1.30,
175 },
176 http.StatusServiceUnavailable)
177 }),
178 },
179 ListUsers: []gax.CallOption{
180 gax.WithTimeout(60000 * time.Millisecond),
181 gax.WithRetry(func() gax.Retryer {
182 return gax.OnHTTPCodes(gax.Backoff{
183 Initial: 1000 * time.Millisecond,
184 Max: 10000 * time.Millisecond,
185 Multiplier: 1.30,
186 },
187 http.StatusServiceUnavailable)
188 }),
189 },
190 }
191 }
192
193 // internalUserClient is an interface that defines the methods available from Merchant API.
194 type internalUserClient interface {
195 Close() error
196 setGoogleClientInfo(...string)
197 Connection() *grpc.ClientConn
198 GetUser(context.Context, *accountspb.GetUserRequest, ...gax.CallOption) (*accountspb.User, error)
199 CreateUser(context.Context, *accountspb.CreateUserRequest, ...gax.CallOption) (*accountspb.User, error)
200 DeleteUser(context.Context, *accountspb.DeleteUserRequest, ...gax.CallOption) error
201 UpdateUser(context.Context, *accountspb.UpdateUserRequest, ...gax.CallOption) (*accountspb.User, error)
202 ListUsers(context.Context, *accountspb.ListUsersRequest, ...gax.CallOption) *UserIterator
203 }
204
205 // UserClient is a client for interacting with Merchant API.
206 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
207 //
208 // Service to support user API.
209 type UserClient struct {
210 // The internal transport-dependent client.
211 internalClient internalUserClient
212
213 // The call options for this service.
214 CallOptions *UserCallOptions
215 }
216
217 // Wrapper methods routed to the internal client.
218
219 // Close closes the connection to the API service. The user should invoke this when
220 // the client is no longer required.
221 func (c *UserClient) Close() error {
222 return c.internalClient.Close()
223 }
224
225 // setGoogleClientInfo sets the name and version of the application in
226 // the `x-goog-api-client` header passed on each request. Intended for
227 // use by Google-written clients.
228 func (c *UserClient) setGoogleClientInfo(keyval ...string) {
229 c.internalClient.setGoogleClientInfo(keyval...)
230 }
231
232 // Connection returns a connection to the API service.
233 //
234 // Deprecated: Connections are now pooled so this method does not always
235 // return the same resource.
236 func (c *UserClient) Connection() *grpc.ClientConn {
237 return c.internalClient.Connection()
238 }
239
240 // GetUser retrieves a Merchant Center account user.
241 func (c *UserClient) GetUser(ctx context.Context, req *accountspb.GetUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
242 return c.internalClient.GetUser(ctx, req, opts...)
243 }
244
245 // CreateUser creates a Merchant Center account user. Executing this method requires
246 // admin access.
247 func (c *UserClient) CreateUser(ctx context.Context, req *accountspb.CreateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
248 return c.internalClient.CreateUser(ctx, req, opts...)
249 }
250
251 // DeleteUser deletes a Merchant Center account user. Executing this method requires
252 // admin access.
253 func (c *UserClient) DeleteUser(ctx context.Context, req *accountspb.DeleteUserRequest, opts ...gax.CallOption) error {
254 return c.internalClient.DeleteUser(ctx, req, opts...)
255 }
256
257 // UpdateUser updates a Merchant Center account user. Executing this method requires
258 // admin access.
259 func (c *UserClient) UpdateUser(ctx context.Context, req *accountspb.UpdateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
260 return c.internalClient.UpdateUser(ctx, req, opts...)
261 }
262
263 // ListUsers lists all users of a Merchant Center account.
264 func (c *UserClient) ListUsers(ctx context.Context, req *accountspb.ListUsersRequest, opts ...gax.CallOption) *UserIterator {
265 return c.internalClient.ListUsers(ctx, req, opts...)
266 }
267
268 // userGRPCClient is a client for interacting with Merchant API over gRPC transport.
269 //
270 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
271 type userGRPCClient struct {
272 // Connection pool of gRPC connections to the service.
273 connPool gtransport.ConnPool
274
275 // Points back to the CallOptions field of the containing UserClient
276 CallOptions **UserCallOptions
277
278 // The gRPC API client.
279 userClient accountspb.UserServiceClient
280
281 // The x-goog-* metadata to be sent with each request.
282 xGoogHeaders []string
283
284 logger *slog.Logger
285 }
286
287 // NewUserClient creates a new user service client based on gRPC.
288 // The returned client must be Closed when it is done being used to clean up its underlying connections.
289 //
290 // Service to support user API.
291 func NewUserClient(ctx context.Context, opts ...option.ClientOption) (*UserClient, error) {
292 clientOpts := defaultUserGRPCClientOptions()
293 if newUserClientHook != nil {
294 hookOpts, err := newUserClientHook(ctx, clientHookParams{})
295 if err != nil {
296 return nil, err
297 }
298 clientOpts = append(clientOpts, hookOpts...)
299 }
300
301 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
302 if err != nil {
303 return nil, err
304 }
305 client := UserClient{CallOptions: defaultUserCallOptions()}
306
307 c := &userGRPCClient{
308 connPool: connPool,
309 userClient: accountspb.NewUserServiceClient(connPool),
310 CallOptions: &client.CallOptions,
311 logger: internaloption.GetLogger(opts),
312 }
313 c.setGoogleClientInfo()
314
315 client.internalClient = c
316
317 return &client, nil
318 }
319
320 // Connection returns a connection to the API service.
321 //
322 // Deprecated: Connections are now pooled so this method does not always
323 // return the same resource.
324 func (c *userGRPCClient) Connection() *grpc.ClientConn {
325 return c.connPool.Conn()
326 }
327
328 // setGoogleClientInfo sets the name and version of the application in
329 // the `x-goog-api-client` header passed on each request. Intended for
330 // use by Google-written clients.
331 func (c *userGRPCClient) setGoogleClientInfo(keyval ...string) {
332 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
333 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
334 c.xGoogHeaders = []string{
335 "x-goog-api-client", gax.XGoogHeader(kv...),
336 }
337 }
338
339 // Close closes the connection to the API service. The user should invoke this when
340 // the client is no longer required.
341 func (c *userGRPCClient) Close() error {
342 return c.connPool.Close()
343 }
344
345 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
346 type userRESTClient struct {
347 // The http endpoint to connect to.
348 endpoint string
349
350 // The http client.
351 httpClient *http.Client
352
353 // The x-goog-* headers to be sent with each request.
354 xGoogHeaders []string
355
356 // Points back to the CallOptions field of the containing UserClient
357 CallOptions **UserCallOptions
358
359 logger *slog.Logger
360 }
361
362 // NewUserRESTClient creates a new user service rest client.
363 //
364 // Service to support user API.
365 func NewUserRESTClient(ctx context.Context, opts ...option.ClientOption) (*UserClient, error) {
366 clientOpts := append(defaultUserRESTClientOptions(), opts...)
367 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
368 if err != nil {
369 return nil, err
370 }
371
372 callOpts := defaultUserRESTCallOptions()
373 c := &userRESTClient{
374 endpoint: endpoint,
375 httpClient: httpClient,
376 CallOptions: &callOpts,
377 logger: internaloption.GetLogger(opts),
378 }
379 c.setGoogleClientInfo()
380
381 return &UserClient{internalClient: c, CallOptions: callOpts}, nil
382 }
383
384 func defaultUserRESTClientOptions() []option.ClientOption {
385 return []option.ClientOption{
386 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
387 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
388 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
389 internaloption.WithDefaultUniverseDomain("googleapis.com"),
390 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
391 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
392 internaloption.EnableNewAuthLibrary(),
393 }
394 }
395
396 // setGoogleClientInfo sets the name and version of the application in
397 // the `x-goog-api-client` header passed on each request. Intended for
398 // use by Google-written clients.
399 func (c *userRESTClient) setGoogleClientInfo(keyval ...string) {
400 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
401 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
402 c.xGoogHeaders = []string{
403 "x-goog-api-client", gax.XGoogHeader(kv...),
404 }
405 }
406
407 // Close closes the connection to the API service. The user should invoke this when
408 // the client is no longer required.
409 func (c *userRESTClient) Close() error {
410 // Replace httpClient with nil to force cleanup.
411 c.httpClient = nil
412 return nil
413 }
414
415 // Connection returns a connection to the API service.
416 //
417 // Deprecated: This method always returns nil.
418 func (c *userRESTClient) Connection() *grpc.ClientConn {
419 return nil
420 }
421 func (c *userGRPCClient) GetUser(ctx context.Context, req *accountspb.GetUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
422 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
423
424 hds = append(c.xGoogHeaders, hds...)
425 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
426 opts = append((*c.CallOptions).GetUser[0:len((*c.CallOptions).GetUser):len((*c.CallOptions).GetUser)], opts...)
427 var resp *accountspb.User
428 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
429 var err error
430 resp, err = executeRPC(ctx, c.userClient.GetUser, req, settings.GRPC, c.logger, "GetUser")
431 return err
432 }, opts...)
433 if err != nil {
434 return nil, err
435 }
436 return resp, nil
437 }
438
439 func (c *userGRPCClient) CreateUser(ctx context.Context, req *accountspb.CreateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
440 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
441
442 hds = append(c.xGoogHeaders, hds...)
443 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
444 opts = append((*c.CallOptions).CreateUser[0:len((*c.CallOptions).CreateUser):len((*c.CallOptions).CreateUser)], opts...)
445 var resp *accountspb.User
446 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
447 var err error
448 resp, err = executeRPC(ctx, c.userClient.CreateUser, req, settings.GRPC, c.logger, "CreateUser")
449 return err
450 }, opts...)
451 if err != nil {
452 return nil, err
453 }
454 return resp, nil
455 }
456
457 func (c *userGRPCClient) DeleteUser(ctx context.Context, req *accountspb.DeleteUserRequest, opts ...gax.CallOption) error {
458 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
459
460 hds = append(c.xGoogHeaders, hds...)
461 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
462 opts = append((*c.CallOptions).DeleteUser[0:len((*c.CallOptions).DeleteUser):len((*c.CallOptions).DeleteUser)], opts...)
463 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
464 var err error
465 _, err = executeRPC(ctx, c.userClient.DeleteUser, req, settings.GRPC, c.logger, "DeleteUser")
466 return err
467 }, opts...)
468 return err
469 }
470
471 func (c *userGRPCClient) UpdateUser(ctx context.Context, req *accountspb.UpdateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
472 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "user.name", url.QueryEscape(req.GetUser().GetName()))}
473
474 hds = append(c.xGoogHeaders, hds...)
475 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
476 opts = append((*c.CallOptions).UpdateUser[0:len((*c.CallOptions).UpdateUser):len((*c.CallOptions).UpdateUser)], opts...)
477 var resp *accountspb.User
478 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
479 var err error
480 resp, err = executeRPC(ctx, c.userClient.UpdateUser, req, settings.GRPC, c.logger, "UpdateUser")
481 return err
482 }, opts...)
483 if err != nil {
484 return nil, err
485 }
486 return resp, nil
487 }
488
489 func (c *userGRPCClient) ListUsers(ctx context.Context, req *accountspb.ListUsersRequest, opts ...gax.CallOption) *UserIterator {
490 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
491
492 hds = append(c.xGoogHeaders, hds...)
493 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
494 opts = append((*c.CallOptions).ListUsers[0:len((*c.CallOptions).ListUsers):len((*c.CallOptions).ListUsers)], opts...)
495 it := &UserIterator{}
496 req = proto.Clone(req).(*accountspb.ListUsersRequest)
497 it.InternalFetch = func(pageSize int, pageToken string) ([]*accountspb.User, string, error) {
498 resp := &accountspb.ListUsersResponse{}
499 if pageToken != "" {
500 req.PageToken = pageToken
501 }
502 if pageSize > math.MaxInt32 {
503 req.PageSize = math.MaxInt32
504 } else if pageSize != 0 {
505 req.PageSize = int32(pageSize)
506 }
507 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
508 var err error
509 resp, err = executeRPC(ctx, c.userClient.ListUsers, req, settings.GRPC, c.logger, "ListUsers")
510 return err
511 }, opts...)
512 if err != nil {
513 return nil, "", err
514 }
515
516 it.Response = resp
517 return resp.GetUsers(), resp.GetNextPageToken(), nil
518 }
519 fetch := func(pageSize int, pageToken string) (string, error) {
520 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
521 if err != nil {
522 return "", err
523 }
524 it.items = append(it.items, items...)
525 return nextPageToken, nil
526 }
527
528 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
529 it.pageInfo.MaxSize = int(req.GetPageSize())
530 it.pageInfo.Token = req.GetPageToken()
531
532 return it
533 }
534
535 // GetUser retrieves a Merchant Center account user.
536 func (c *userRESTClient) GetUser(ctx context.Context, req *accountspb.GetUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
537 baseUrl, err := url.Parse(c.endpoint)
538 if err != nil {
539 return nil, err
540 }
541 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v", req.GetName())
542
543 params := url.Values{}
544 params.Add("$alt", "json;enum-encoding=int")
545
546 baseUrl.RawQuery = params.Encode()
547
548 // Build HTTP headers from client and context metadata.
549 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
550
551 hds = append(c.xGoogHeaders, hds...)
552 hds = append(hds, "Content-Type", "application/json")
553 headers := gax.BuildHeaders(ctx, hds...)
554 opts = append((*c.CallOptions).GetUser[0:len((*c.CallOptions).GetUser):len((*c.CallOptions).GetUser)], opts...)
555 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
556 resp := &accountspb.User{}
557 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
558 if settings.Path != "" {
559 baseUrl.Path = settings.Path
560 }
561 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
562 if err != nil {
563 return err
564 }
565 httpReq = httpReq.WithContext(ctx)
566 httpReq.Header = headers
567
568 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetUser")
569 if err != nil {
570 return err
571 }
572
573 if err := unm.Unmarshal(buf, resp); err != nil {
574 return err
575 }
576
577 return nil
578 }, opts...)
579 if e != nil {
580 return nil, e
581 }
582 return resp, nil
583 }
584
585 // CreateUser creates a Merchant Center account user. Executing this method requires
586 // admin access.
587 func (c *userRESTClient) CreateUser(ctx context.Context, req *accountspb.CreateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
588 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
589 body := req.GetUser()
590 jsonReq, err := m.Marshal(body)
591 if err != nil {
592 return nil, err
593 }
594
595 baseUrl, err := url.Parse(c.endpoint)
596 if err != nil {
597 return nil, err
598 }
599 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v/users", req.GetParent())
600
601 params := url.Values{}
602 params.Add("$alt", "json;enum-encoding=int")
603 params.Add("userId", fmt.Sprintf("%v", req.GetUserId()))
604
605 baseUrl.RawQuery = params.Encode()
606
607 // Build HTTP headers from client and context metadata.
608 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
609
610 hds = append(c.xGoogHeaders, hds...)
611 hds = append(hds, "Content-Type", "application/json")
612 headers := gax.BuildHeaders(ctx, hds...)
613 opts = append((*c.CallOptions).CreateUser[0:len((*c.CallOptions).CreateUser):len((*c.CallOptions).CreateUser)], opts...)
614 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
615 resp := &accountspb.User{}
616 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
617 if settings.Path != "" {
618 baseUrl.Path = settings.Path
619 }
620 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
621 if err != nil {
622 return err
623 }
624 httpReq = httpReq.WithContext(ctx)
625 httpReq.Header = headers
626
627 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "CreateUser")
628 if err != nil {
629 return err
630 }
631
632 if err := unm.Unmarshal(buf, resp); err != nil {
633 return err
634 }
635
636 return nil
637 }, opts...)
638 if e != nil {
639 return nil, e
640 }
641 return resp, nil
642 }
643
644 // DeleteUser deletes a Merchant Center account user. Executing this method requires
645 // admin access.
646 func (c *userRESTClient) DeleteUser(ctx context.Context, req *accountspb.DeleteUserRequest, opts ...gax.CallOption) error {
647 baseUrl, err := url.Parse(c.endpoint)
648 if err != nil {
649 return err
650 }
651 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v", req.GetName())
652
653 params := url.Values{}
654 params.Add("$alt", "json;enum-encoding=int")
655
656 baseUrl.RawQuery = params.Encode()
657
658 // Build HTTP headers from client and context metadata.
659 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
660
661 hds = append(c.xGoogHeaders, hds...)
662 hds = append(hds, "Content-Type", "application/json")
663 headers := gax.BuildHeaders(ctx, hds...)
664 return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
665 if settings.Path != "" {
666 baseUrl.Path = settings.Path
667 }
668 httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
669 if err != nil {
670 return err
671 }
672 httpReq = httpReq.WithContext(ctx)
673 httpReq.Header = headers
674
675 _, err = executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "DeleteUser")
676 return err
677 }, opts...)
678 }
679
680 // UpdateUser updates a Merchant Center account user. Executing this method requires
681 // admin access.
682 func (c *userRESTClient) UpdateUser(ctx context.Context, req *accountspb.UpdateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
683 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
684 body := req.GetUser()
685 jsonReq, err := m.Marshal(body)
686 if err != nil {
687 return nil, err
688 }
689
690 baseUrl, err := url.Parse(c.endpoint)
691 if err != nil {
692 return nil, err
693 }
694 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v", req.GetUser().GetName())
695
696 params := url.Values{}
697 params.Add("$alt", "json;enum-encoding=int")
698 if req.GetUpdateMask() != nil {
699 field, err := protojson.Marshal(req.GetUpdateMask())
700 if err != nil {
701 return nil, err
702 }
703 params.Add("updateMask", string(field[1:len(field)-1]))
704 }
705
706 baseUrl.RawQuery = params.Encode()
707
708 // Build HTTP headers from client and context metadata.
709 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "user.name", url.QueryEscape(req.GetUser().GetName()))}
710
711 hds = append(c.xGoogHeaders, hds...)
712 hds = append(hds, "Content-Type", "application/json")
713 headers := gax.BuildHeaders(ctx, hds...)
714 opts = append((*c.CallOptions).UpdateUser[0:len((*c.CallOptions).UpdateUser):len((*c.CallOptions).UpdateUser)], opts...)
715 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
716 resp := &accountspb.User{}
717 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
718 if settings.Path != "" {
719 baseUrl.Path = settings.Path
720 }
721 httpReq, err := http.NewRequest("PATCH", baseUrl.String(), bytes.NewReader(jsonReq))
722 if err != nil {
723 return err
724 }
725 httpReq = httpReq.WithContext(ctx)
726 httpReq.Header = headers
727
728 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "UpdateUser")
729 if err != nil {
730 return err
731 }
732
733 if err := unm.Unmarshal(buf, resp); err != nil {
734 return err
735 }
736
737 return nil
738 }, opts...)
739 if e != nil {
740 return nil, e
741 }
742 return resp, nil
743 }
744
745 // ListUsers lists all users of a Merchant Center account.
746 func (c *userRESTClient) ListUsers(ctx context.Context, req *accountspb.ListUsersRequest, opts ...gax.CallOption) *UserIterator {
747 it := &UserIterator{}
748 req = proto.Clone(req).(*accountspb.ListUsersRequest)
749 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
750 it.InternalFetch = func(pageSize int, pageToken string) ([]*accountspb.User, string, error) {
751 resp := &accountspb.ListUsersResponse{}
752 if pageToken != "" {
753 req.PageToken = pageToken
754 }
755 if pageSize > math.MaxInt32 {
756 req.PageSize = math.MaxInt32
757 } else if pageSize != 0 {
758 req.PageSize = int32(pageSize)
759 }
760 baseUrl, err := url.Parse(c.endpoint)
761 if err != nil {
762 return nil, "", err
763 }
764 baseUrl.Path += fmt.Sprintf("/accounts/v1beta/%v/users", req.GetParent())
765
766 params := url.Values{}
767 params.Add("$alt", "json;enum-encoding=int")
768 if req.GetPageSize() != 0 {
769 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
770 }
771 if req.GetPageToken() != "" {
772 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
773 }
774
775 baseUrl.RawQuery = params.Encode()
776
777 // Build HTTP headers from client and context metadata.
778 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
779 headers := gax.BuildHeaders(ctx, hds...)
780 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
781 if settings.Path != "" {
782 baseUrl.Path = settings.Path
783 }
784 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
785 if err != nil {
786 return err
787 }
788 httpReq.Header = headers
789
790 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListUsers")
791 if err != nil {
792 return err
793 }
794 if err := unm.Unmarshal(buf, resp); err != nil {
795 return err
796 }
797
798 return nil
799 }, opts...)
800 if e != nil {
801 return nil, "", e
802 }
803 it.Response = resp
804 return resp.GetUsers(), resp.GetNextPageToken(), nil
805 }
806
807 fetch := func(pageSize int, pageToken string) (string, error) {
808 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
809 if err != nil {
810 return "", err
811 }
812 it.items = append(it.items, items...)
813 return nextPageToken, nil
814 }
815
816 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
817 it.pageInfo.MaxSize = int(req.GetPageSize())
818 it.pageInfo.Token = req.GetPageToken()
819
820 return it
821 }