Debian Codegraph @

shopping/merchant/accounts/apiv1/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/apiv1/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. The user to be deleted can’t be the last admin user of that
253 // account. Also a user is protected from deletion if it
254 // is managed by Business Manager"
255 func (c *UserClient) DeleteUser(ctx context.Context, req *accountspb.DeleteUserRequest, opts ...gax.CallOption) error {
256 return c.internalClient.DeleteUser(ctx, req, opts...)
257 }
258
259 // UpdateUser updates a Merchant Center account user. Executing this method requires
260 // admin access.
261 func (c *UserClient) UpdateUser(ctx context.Context, req *accountspb.UpdateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
262 return c.internalClient.UpdateUser(ctx, req, opts...)
263 }
264
265 // ListUsers lists all users of a Merchant Center account.
266 func (c *UserClient) ListUsers(ctx context.Context, req *accountspb.ListUsersRequest, opts ...gax.CallOption) *UserIterator {
267 return c.internalClient.ListUsers(ctx, req, opts...)
268 }
269
270 // userGRPCClient is a client for interacting with Merchant API over gRPC transport.
271 //
272 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
273 type userGRPCClient struct {
274 // Connection pool of gRPC connections to the service.
275 connPool gtransport.ConnPool
276
277 // Points back to the CallOptions field of the containing UserClient
278 CallOptions **UserCallOptions
279
280 // The gRPC API client.
281 userClient accountspb.UserServiceClient
282
283 // The x-goog-* metadata to be sent with each request.
284 xGoogHeaders []string
285
286 logger *slog.Logger
287 }
288
289 // NewUserClient creates a new user service client based on gRPC.
290 // The returned client must be Closed when it is done being used to clean up its underlying connections.
291 //
292 // Service to support user API.
293 func NewUserClient(ctx context.Context, opts ...option.ClientOption) (*UserClient, error) {
294 clientOpts := defaultUserGRPCClientOptions()
295 if newUserClientHook != nil {
296 hookOpts, err := newUserClientHook(ctx, clientHookParams{})
297 if err != nil {
298 return nil, err
299 }
300 clientOpts = append(clientOpts, hookOpts...)
301 }
302
303 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
304 if err != nil {
305 return nil, err
306 }
307 client := UserClient{CallOptions: defaultUserCallOptions()}
308
309 c := &userGRPCClient{
310 connPool: connPool,
311 userClient: accountspb.NewUserServiceClient(connPool),
312 CallOptions: &client.CallOptions,
313 logger: internaloption.GetLogger(opts),
314 }
315 c.setGoogleClientInfo()
316
317 client.internalClient = c
318
319 return &client, nil
320 }
321
322 // Connection returns a connection to the API service.
323 //
324 // Deprecated: Connections are now pooled so this method does not always
325 // return the same resource.
326 func (c *userGRPCClient) Connection() *grpc.ClientConn {
327 return c.connPool.Conn()
328 }
329
330 // setGoogleClientInfo sets the name and version of the application in
331 // the `x-goog-api-client` header passed on each request. Intended for
332 // use by Google-written clients.
333 func (c *userGRPCClient) setGoogleClientInfo(keyval ...string) {
334 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
335 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
336 c.xGoogHeaders = []string{
337 "x-goog-api-client", gax.XGoogHeader(kv...),
338 }
339 }
340
341 // Close closes the connection to the API service. The user should invoke this when
342 // the client is no longer required.
343 func (c *userGRPCClient) Close() error {
344 return c.connPool.Close()
345 }
346
347 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
348 type userRESTClient struct {
349 // The http endpoint to connect to.
350 endpoint string
351
352 // The http client.
353 httpClient *http.Client
354
355 // The x-goog-* headers to be sent with each request.
356 xGoogHeaders []string
357
358 // Points back to the CallOptions field of the containing UserClient
359 CallOptions **UserCallOptions
360
361 logger *slog.Logger
362 }
363
364 // NewUserRESTClient creates a new user service rest client.
365 //
366 // Service to support user API.
367 func NewUserRESTClient(ctx context.Context, opts ...option.ClientOption) (*UserClient, error) {
368 clientOpts := append(defaultUserRESTClientOptions(), opts...)
369 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
370 if err != nil {
371 return nil, err
372 }
373
374 callOpts := defaultUserRESTCallOptions()
375 c := &userRESTClient{
376 endpoint: endpoint,
377 httpClient: httpClient,
378 CallOptions: &callOpts,
379 logger: internaloption.GetLogger(opts),
380 }
381 c.setGoogleClientInfo()
382
383 return &UserClient{internalClient: c, CallOptions: callOpts}, nil
384 }
385
386 func defaultUserRESTClientOptions() []option.ClientOption {
387 return []option.ClientOption{
388 internaloption.WithDefaultEndpoint("https://merchantapi.googleapis.com"),
389 internaloption.WithDefaultEndpointTemplate("https://merchantapi.UNIVERSE_DOMAIN"),
390 internaloption.WithDefaultMTLSEndpoint("https://merchantapi.mtls.googleapis.com"),
391 internaloption.WithDefaultUniverseDomain("googleapis.com"),
392 internaloption.WithDefaultAudience("https://merchantapi.googleapis.com/"),
393 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
394 internaloption.EnableNewAuthLibrary(),
395 }
396 }
397
398 // setGoogleClientInfo sets the name and version of the application in
399 // the `x-goog-api-client` header passed on each request. Intended for
400 // use by Google-written clients.
401 func (c *userRESTClient) setGoogleClientInfo(keyval ...string) {
402 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
403 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
404 c.xGoogHeaders = []string{
405 "x-goog-api-client", gax.XGoogHeader(kv...),
406 }
407 }
408
409 // Close closes the connection to the API service. The user should invoke this when
410 // the client is no longer required.
411 func (c *userRESTClient) Close() error {
412 // Replace httpClient with nil to force cleanup.
413 c.httpClient = nil
414 return nil
415 }
416
417 // Connection returns a connection to the API service.
418 //
419 // Deprecated: This method always returns nil.
420 func (c *userRESTClient) Connection() *grpc.ClientConn {
421 return nil
422 }
423 func (c *userGRPCClient) GetUser(ctx context.Context, req *accountspb.GetUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
424 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
425
426 hds = append(c.xGoogHeaders, hds...)
427 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
428 opts = append((*c.CallOptions).GetUser[0:len((*c.CallOptions).GetUser):len((*c.CallOptions).GetUser)], opts...)
429 var resp *accountspb.User
430 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
431 var err error
432 resp, err = executeRPC(ctx, c.userClient.GetUser, req, settings.GRPC, c.logger, "GetUser")
433 return err
434 }, opts...)
435 if err != nil {
436 return nil, err
437 }
438 return resp, nil
439 }
440
441 func (c *userGRPCClient) CreateUser(ctx context.Context, req *accountspb.CreateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
442 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
443
444 hds = append(c.xGoogHeaders, hds...)
445 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
446 opts = append((*c.CallOptions).CreateUser[0:len((*c.CallOptions).CreateUser):len((*c.CallOptions).CreateUser)], opts...)
447 var resp *accountspb.User
448 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
449 var err error
450 resp, err = executeRPC(ctx, c.userClient.CreateUser, req, settings.GRPC, c.logger, "CreateUser")
451 return err
452 }, opts...)
453 if err != nil {
454 return nil, err
455 }
456 return resp, nil
457 }
458
459 func (c *userGRPCClient) DeleteUser(ctx context.Context, req *accountspb.DeleteUserRequest, opts ...gax.CallOption) error {
460 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
461
462 hds = append(c.xGoogHeaders, hds...)
463 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
464 opts = append((*c.CallOptions).DeleteUser[0:len((*c.CallOptions).DeleteUser):len((*c.CallOptions).DeleteUser)], opts...)
465 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
466 var err error
467 _, err = executeRPC(ctx, c.userClient.DeleteUser, req, settings.GRPC, c.logger, "DeleteUser")
468 return err
469 }, opts...)
470 return err
471 }
472
473 func (c *userGRPCClient) UpdateUser(ctx context.Context, req *accountspb.UpdateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
474 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "user.name", url.QueryEscape(req.GetUser().GetName()))}
475
476 hds = append(c.xGoogHeaders, hds...)
477 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
478 opts = append((*c.CallOptions).UpdateUser[0:len((*c.CallOptions).UpdateUser):len((*c.CallOptions).UpdateUser)], opts...)
479 var resp *accountspb.User
480 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
481 var err error
482 resp, err = executeRPC(ctx, c.userClient.UpdateUser, req, settings.GRPC, c.logger, "UpdateUser")
483 return err
484 }, opts...)
485 if err != nil {
486 return nil, err
487 }
488 return resp, nil
489 }
490
491 func (c *userGRPCClient) ListUsers(ctx context.Context, req *accountspb.ListUsersRequest, opts ...gax.CallOption) *UserIterator {
492 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
493
494 hds = append(c.xGoogHeaders, hds...)
495 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
496 opts = append((*c.CallOptions).ListUsers[0:len((*c.CallOptions).ListUsers):len((*c.CallOptions).ListUsers)], opts...)
497 it := &UserIterator{}
498 req = proto.Clone(req).(*accountspb.ListUsersRequest)
499 it.InternalFetch = func(pageSize int, pageToken string) ([]*accountspb.User, string, error) {
500 resp := &accountspb.ListUsersResponse{}
501 if pageToken != "" {
502 req.PageToken = pageToken
503 }
504 if pageSize > math.MaxInt32 {
505 req.PageSize = math.MaxInt32
506 } else if pageSize != 0 {
507 req.PageSize = int32(pageSize)
508 }
509 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
510 var err error
511 resp, err = executeRPC(ctx, c.userClient.ListUsers, req, settings.GRPC, c.logger, "ListUsers")
512 return err
513 }, opts...)
514 if err != nil {
515 return nil, "", err
516 }
517
518 it.Response = resp
519 return resp.GetUsers(), resp.GetNextPageToken(), nil
520 }
521 fetch := func(pageSize int, pageToken string) (string, error) {
522 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
523 if err != nil {
524 return "", err
525 }
526 it.items = append(it.items, items...)
527 return nextPageToken, nil
528 }
529
530 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
531 it.pageInfo.MaxSize = int(req.GetPageSize())
532 it.pageInfo.Token = req.GetPageToken()
533
534 return it
535 }
536
537 // GetUser retrieves a Merchant Center account user.
538 func (c *userRESTClient) GetUser(ctx context.Context, req *accountspb.GetUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
539 baseUrl, err := url.Parse(c.endpoint)
540 if err != nil {
541 return nil, err
542 }
543 baseUrl.Path += fmt.Sprintf("/accounts/v1/%v", req.GetName())
544
545 params := url.Values{}
546 params.Add("$alt", "json;enum-encoding=int")
547
548 baseUrl.RawQuery = params.Encode()
549
550 // Build HTTP headers from client and context metadata.
551 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
552
553 hds = append(c.xGoogHeaders, hds...)
554 hds = append(hds, "Content-Type", "application/json")
555 headers := gax.BuildHeaders(ctx, hds...)
556 opts = append((*c.CallOptions).GetUser[0:len((*c.CallOptions).GetUser):len((*c.CallOptions).GetUser)], opts...)
557 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
558 resp := &accountspb.User{}
559 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
560 if settings.Path != "" {
561 baseUrl.Path = settings.Path
562 }
563 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
564 if err != nil {
565 return err
566 }
567 httpReq = httpReq.WithContext(ctx)
568 httpReq.Header = headers
569
570 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetUser")
571 if err != nil {
572 return err
573 }
574
575 if err := unm.Unmarshal(buf, resp); err != nil {
576 return err
577 }
578
579 return nil
580 }, opts...)
581 if e != nil {
582 return nil, e
583 }
584 return resp, nil
585 }
586
587 // CreateUser creates a Merchant Center account user. Executing this method requires
588 // admin access.
589 func (c *userRESTClient) CreateUser(ctx context.Context, req *accountspb.CreateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
590 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
591 body := req.GetUser()
592 jsonReq, err := m.Marshal(body)
593 if err != nil {
594 return nil, err
595 }
596
597 baseUrl, err := url.Parse(c.endpoint)
598 if err != nil {
599 return nil, err
600 }
601 baseUrl.Path += fmt.Sprintf("/accounts/v1/%v/users", req.GetParent())
602
603 params := url.Values{}
604 params.Add("$alt", "json;enum-encoding=int")
605 params.Add("userId", fmt.Sprintf("%v", req.GetUserId()))
606
607 baseUrl.RawQuery = params.Encode()
608
609 // Build HTTP headers from client and context metadata.
610 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
611
612 hds = append(c.xGoogHeaders, hds...)
613 hds = append(hds, "Content-Type", "application/json")
614 headers := gax.BuildHeaders(ctx, hds...)
615 opts = append((*c.CallOptions).CreateUser[0:len((*c.CallOptions).CreateUser):len((*c.CallOptions).CreateUser)], opts...)
616 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
617 resp := &accountspb.User{}
618 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
619 if settings.Path != "" {
620 baseUrl.Path = settings.Path
621 }
622 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
623 if err != nil {
624 return err
625 }
626 httpReq = httpReq.WithContext(ctx)
627 httpReq.Header = headers
628
629 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "CreateUser")
630 if err != nil {
631 return err
632 }
633
634 if err := unm.Unmarshal(buf, resp); err != nil {
635 return err
636 }
637
638 return nil
639 }, opts...)
640 if e != nil {
641 return nil, e
642 }
643 return resp, nil
644 }
645
646 // DeleteUser deletes a Merchant Center account user. Executing this method requires
647 // admin access. The user to be deleted can’t be the last admin user of that
648 // account. Also a user is protected from deletion if it
649 // is managed by Business Manager"
650 func (c *userRESTClient) DeleteUser(ctx context.Context, req *accountspb.DeleteUserRequest, opts ...gax.CallOption) error {
651 baseUrl, err := url.Parse(c.endpoint)
652 if err != nil {
653 return err
654 }
655 baseUrl.Path += fmt.Sprintf("/accounts/v1/%v", req.GetName())
656
657 params := url.Values{}
658 params.Add("$alt", "json;enum-encoding=int")
659
660 baseUrl.RawQuery = params.Encode()
661
662 // Build HTTP headers from client and context metadata.
663 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
664
665 hds = append(c.xGoogHeaders, hds...)
666 hds = append(hds, "Content-Type", "application/json")
667 headers := gax.BuildHeaders(ctx, hds...)
668 return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
669 if settings.Path != "" {
670 baseUrl.Path = settings.Path
671 }
672 httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
673 if err != nil {
674 return err
675 }
676 httpReq = httpReq.WithContext(ctx)
677 httpReq.Header = headers
678
679 _, err = executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "DeleteUser")
680 return err
681 }, opts...)
682 }
683
684 // UpdateUser updates a Merchant Center account user. Executing this method requires
685 // admin access.
686 func (c *userRESTClient) UpdateUser(ctx context.Context, req *accountspb.UpdateUserRequest, opts ...gax.CallOption) (*accountspb.User, error) {
687 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
688 body := req.GetUser()
689 jsonReq, err := m.Marshal(body)
690 if err != nil {
691 return nil, err
692 }
693
694 baseUrl, err := url.Parse(c.endpoint)
695 if err != nil {
696 return nil, err
697 }
698 baseUrl.Path += fmt.Sprintf("/accounts/v1/%v", req.GetUser().GetName())
699
700 params := url.Values{}
701 params.Add("$alt", "json;enum-encoding=int")
702 if req.GetUpdateMask() != nil {
703 field, err := protojson.Marshal(req.GetUpdateMask())
704 if err != nil {
705 return nil, err
706 }
707 params.Add("updateMask", string(field[1:len(field)-1]))
708 }
709
710 baseUrl.RawQuery = params.Encode()
711
712 // Build HTTP headers from client and context metadata.
713 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "user.name", url.QueryEscape(req.GetUser().GetName()))}
714
715 hds = append(c.xGoogHeaders, hds...)
716 hds = append(hds, "Content-Type", "application/json")
717 headers := gax.BuildHeaders(ctx, hds...)
718 opts = append((*c.CallOptions).UpdateUser[0:len((*c.CallOptions).UpdateUser):len((*c.CallOptions).UpdateUser)], opts...)
719 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
720 resp := &accountspb.User{}
721 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
722 if settings.Path != "" {
723 baseUrl.Path = settings.Path
724 }
725 httpReq, err := http.NewRequest("PATCH", baseUrl.String(), bytes.NewReader(jsonReq))
726 if err != nil {
727 return err
728 }
729 httpReq = httpReq.WithContext(ctx)
730 httpReq.Header = headers
731
732 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "UpdateUser")
733 if err != nil {
734 return err
735 }
736
737 if err := unm.Unmarshal(buf, resp); err != nil {
738 return err
739 }
740
741 return nil
742 }, opts...)
743 if e != nil {
744 return nil, e
745 }
746 return resp, nil
747 }
748
749 // ListUsers lists all users of a Merchant Center account.
750 func (c *userRESTClient) ListUsers(ctx context.Context, req *accountspb.ListUsersRequest, opts ...gax.CallOption) *UserIterator {
751 it := &UserIterator{}
752 req = proto.Clone(req).(*accountspb.ListUsersRequest)
753 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
754 it.InternalFetch = func(pageSize int, pageToken string) ([]*accountspb.User, string, error) {
755 resp := &accountspb.ListUsersResponse{}
756 if pageToken != "" {
757 req.PageToken = pageToken
758 }
759 if pageSize > math.MaxInt32 {
760 req.PageSize = math.MaxInt32
761 } else if pageSize != 0 {
762 req.PageSize = int32(pageSize)
763 }
764 baseUrl, err := url.Parse(c.endpoint)
765 if err != nil {
766 return nil, "", err
767 }
768 baseUrl.Path += fmt.Sprintf("/accounts/v1/%v/users", req.GetParent())
769
770 params := url.Values{}
771 params.Add("$alt", "json;enum-encoding=int")
772 if req.GetPageSize() != 0 {
773 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
774 }
775 if req.GetPageToken() != "" {
776 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
777 }
778
779 baseUrl.RawQuery = params.Encode()
780
781 // Build HTTP headers from client and context metadata.
782 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
783 headers := gax.BuildHeaders(ctx, hds...)
784 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
785 if settings.Path != "" {
786 baseUrl.Path = settings.Path
787 }
788 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
789 if err != nil {
790 return err
791 }
792 httpReq.Header = headers
793
794 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListUsers")
795 if err != nil {
796 return err
797 }
798 if err := unm.Unmarshal(buf, resp); err != nil {
799 return err
800 }
801
802 return nil
803 }, opts...)
804 if e != nil {
805 return nil, "", e
806 }
807 it.Response = resp
808 return resp.GetUsers(), resp.GetNextPageToken(), nil
809 }
810
811 fetch := func(pageSize int, pageToken string) (string, error) {
812 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
813 if err != nil {
814 return "", err
815 }
816 it.items = append(it.items, items...)
817 return nextPageToken, nil
818 }
819
820 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
821 it.pageInfo.MaxSize = int(req.GetPageSize())
822 it.pageInfo.Token = req.GetPageToken()
823
824 return it
825 }