...

Source file src/cloud.google.com/go/longrunning/autogen/operations_client.go

Documentation: cloud.google.com/go/longrunning/autogen

     1  // Copyright 2024 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 longrunning
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"math"
    25  	"net/http"
    26  	"net/url"
    27  	"time"
    28  
    29  	longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb"
    30  	gax "github.com/googleapis/gax-go/v2"
    31  	"google.golang.org/api/googleapi"
    32  	"google.golang.org/api/iterator"
    33  	"google.golang.org/api/option"
    34  	"google.golang.org/api/option/internaloption"
    35  	gtransport "google.golang.org/api/transport/grpc"
    36  	httptransport "google.golang.org/api/transport/http"
    37  	"google.golang.org/grpc"
    38  	"google.golang.org/grpc/codes"
    39  	"google.golang.org/protobuf/encoding/protojson"
    40  	"google.golang.org/protobuf/proto"
    41  )
    42  
    43  var newOperationsClientHook clientHook
    44  
    45  // OperationsCallOptions contains the retry settings for each method of OperationsClient.
    46  type OperationsCallOptions struct {
    47  	ListOperations  []gax.CallOption
    48  	GetOperation    []gax.CallOption
    49  	DeleteOperation []gax.CallOption
    50  	CancelOperation []gax.CallOption
    51  	WaitOperation   []gax.CallOption
    52  }
    53  
    54  func defaultOperationsGRPCClientOptions() []option.ClientOption {
    55  	return []option.ClientOption{
    56  		internaloption.WithDefaultEndpoint("longrunning.googleapis.com:443"),
    57  		internaloption.WithDefaultEndpointTemplate("longrunning.UNIVERSE_DOMAIN:443"),
    58  		internaloption.WithDefaultMTLSEndpoint("longrunning.mtls.googleapis.com:443"),
    59  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    60  		internaloption.WithDefaultAudience("https://longrunning.googleapis.com/"),
    61  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    62  		internaloption.EnableJwtWithScope(),
    63  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    64  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    65  	}
    66  }
    67  
    68  func defaultOperationsCallOptions() *OperationsCallOptions {
    69  	return &OperationsCallOptions{
    70  		ListOperations: []gax.CallOption{
    71  			gax.WithTimeout(10000 * time.Millisecond),
    72  			gax.WithRetry(func() gax.Retryer {
    73  				return gax.OnCodes([]codes.Code{
    74  					codes.Unavailable,
    75  				}, gax.Backoff{
    76  					Initial:    500 * time.Millisecond,
    77  					Max:        10000 * time.Millisecond,
    78  					Multiplier: 2.00,
    79  				})
    80  			}),
    81  		},
    82  		GetOperation: []gax.CallOption{
    83  			gax.WithTimeout(10000 * time.Millisecond),
    84  			gax.WithRetry(func() gax.Retryer {
    85  				return gax.OnCodes([]codes.Code{
    86  					codes.Unavailable,
    87  				}, gax.Backoff{
    88  					Initial:    500 * time.Millisecond,
    89  					Max:        10000 * time.Millisecond,
    90  					Multiplier: 2.00,
    91  				})
    92  			}),
    93  		},
    94  		DeleteOperation: []gax.CallOption{
    95  			gax.WithTimeout(10000 * time.Millisecond),
    96  			gax.WithRetry(func() gax.Retryer {
    97  				return gax.OnCodes([]codes.Code{
    98  					codes.Unavailable,
    99  				}, gax.Backoff{
   100  					Initial:    500 * time.Millisecond,
   101  					Max:        10000 * time.Millisecond,
   102  					Multiplier: 2.00,
   103  				})
   104  			}),
   105  		},
   106  		CancelOperation: []gax.CallOption{
   107  			gax.WithTimeout(10000 * time.Millisecond),
   108  			gax.WithRetry(func() gax.Retryer {
   109  				return gax.OnCodes([]codes.Code{
   110  					codes.Unavailable,
   111  				}, gax.Backoff{
   112  					Initial:    500 * time.Millisecond,
   113  					Max:        10000 * time.Millisecond,
   114  					Multiplier: 2.00,
   115  				})
   116  			}),
   117  		},
   118  		WaitOperation: []gax.CallOption{},
   119  	}
   120  }
   121  
   122  func defaultOperationsRESTCallOptions() *OperationsCallOptions {
   123  	return &OperationsCallOptions{
   124  		ListOperations: []gax.CallOption{
   125  			gax.WithTimeout(10000 * time.Millisecond),
   126  			gax.WithRetry(func() gax.Retryer {
   127  				return gax.OnHTTPCodes(gax.Backoff{
   128  					Initial:    500 * time.Millisecond,
   129  					Max:        10000 * time.Millisecond,
   130  					Multiplier: 2.00,
   131  				},
   132  					http.StatusServiceUnavailable)
   133  			}),
   134  		},
   135  		GetOperation: []gax.CallOption{
   136  			gax.WithTimeout(10000 * time.Millisecond),
   137  			gax.WithRetry(func() gax.Retryer {
   138  				return gax.OnHTTPCodes(gax.Backoff{
   139  					Initial:    500 * time.Millisecond,
   140  					Max:        10000 * time.Millisecond,
   141  					Multiplier: 2.00,
   142  				},
   143  					http.StatusServiceUnavailable)
   144  			}),
   145  		},
   146  		DeleteOperation: []gax.CallOption{
   147  			gax.WithTimeout(10000 * time.Millisecond),
   148  			gax.WithRetry(func() gax.Retryer {
   149  				return gax.OnHTTPCodes(gax.Backoff{
   150  					Initial:    500 * time.Millisecond,
   151  					Max:        10000 * time.Millisecond,
   152  					Multiplier: 2.00,
   153  				},
   154  					http.StatusServiceUnavailable)
   155  			}),
   156  		},
   157  		CancelOperation: []gax.CallOption{
   158  			gax.WithTimeout(10000 * time.Millisecond),
   159  			gax.WithRetry(func() gax.Retryer {
   160  				return gax.OnHTTPCodes(gax.Backoff{
   161  					Initial:    500 * time.Millisecond,
   162  					Max:        10000 * time.Millisecond,
   163  					Multiplier: 2.00,
   164  				},
   165  					http.StatusServiceUnavailable)
   166  			}),
   167  		},
   168  		WaitOperation: []gax.CallOption{},
   169  	}
   170  }
   171  
   172  // internalOperationsClient is an interface that defines the methods available from Long Running Operations API.
   173  type internalOperationsClient interface {
   174  	Close() error
   175  	setGoogleClientInfo(...string)
   176  	Connection() *grpc.ClientConn
   177  	ListOperations(context.Context, *longrunningpb.ListOperationsRequest, ...gax.CallOption) *OperationIterator
   178  	GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error)
   179  	DeleteOperation(context.Context, *longrunningpb.DeleteOperationRequest, ...gax.CallOption) error
   180  	CancelOperation(context.Context, *longrunningpb.CancelOperationRequest, ...gax.CallOption) error
   181  	WaitOperation(context.Context, *longrunningpb.WaitOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error)
   182  }
   183  
   184  // OperationsClient is a client for interacting with Long Running Operations API.
   185  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   186  //
   187  // Manages long-running operations with an API service.
   188  //
   189  // When an API method normally takes long time to complete, it can be designed
   190  // to return Operation to the client, and the client can use this
   191  // interface to receive the real response asynchronously by polling the
   192  // operation resource, or pass the operation resource to another API (such as
   193  // Google Cloud Pub/Sub API) to receive the response.  Any API service that
   194  // returns long-running operations should implement the Operations interface
   195  // so developers can have a consistent client experience.
   196  type OperationsClient struct {
   197  	// The internal transport-dependent client.
   198  	internalClient internalOperationsClient
   199  
   200  	// The call options for this service.
   201  	CallOptions *OperationsCallOptions
   202  }
   203  
   204  // Wrapper methods routed to the internal client.
   205  
   206  // Close closes the connection to the API service. The user should invoke this when
   207  // the client is no longer required.
   208  func (c *OperationsClient) Close() error {
   209  	return c.internalClient.Close()
   210  }
   211  
   212  // setGoogleClientInfo sets the name and version of the application in
   213  // the `x-goog-api-client` header passed on each request. Intended for
   214  // use by Google-written clients.
   215  func (c *OperationsClient) setGoogleClientInfo(keyval ...string) {
   216  	c.internalClient.setGoogleClientInfo(keyval...)
   217  }
   218  
   219  // Connection returns a connection to the API service.
   220  //
   221  // Deprecated: Connections are now pooled so this method does not always
   222  // return the same resource.
   223  func (c *OperationsClient) Connection() *grpc.ClientConn {
   224  	return c.internalClient.Connection()
   225  }
   226  
   227  // ListOperations lists operations that match the specified filter in the request. If the
   228  // server doesn’t support this method, it returns UNIMPLEMENTED.
   229  //
   230  // NOTE: the name binding allows API services to override the binding
   231  // to use different resource name schemes, such as users/*/operations. To
   232  // override the binding, API services can add a binding such as
   233  // "/v1/{name=users/*}/operations" to their service configuration.
   234  // For backwards compatibility, the default name includes the operations
   235  // collection id, however overriding users must ensure the name binding
   236  // is the parent resource, without the operations collection id.
   237  func (c *OperationsClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
   238  	return c.internalClient.ListOperations(ctx, req, opts...)
   239  }
   240  
   241  // GetOperation gets the latest state of a long-running operation.  Clients can use this
   242  // method to poll the operation result at intervals as recommended by the API
   243  // service.
   244  func (c *OperationsClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   245  	return c.internalClient.GetOperation(ctx, req, opts...)
   246  }
   247  
   248  // DeleteOperation deletes a long-running operation. This method indicates that the client is
   249  // no longer interested in the operation result. It does not cancel the
   250  // operation. If the server doesn’t support this method, it returns
   251  // google.rpc.Code.UNIMPLEMENTED.
   252  func (c *OperationsClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error {
   253  	return c.internalClient.DeleteOperation(ctx, req, opts...)
   254  }
   255  
   256  // CancelOperation starts asynchronous cancellation on a long-running operation.  The server
   257  // makes a best effort to cancel the operation, but success is not
   258  // guaranteed.  If the server doesn’t support this method, it returns
   259  // google.rpc.Code.UNIMPLEMENTED.  Clients can use
   260  // Operations.GetOperation or
   261  // other methods to check whether the cancellation succeeded or whether the
   262  // operation completed despite cancellation. On successful cancellation,
   263  // the operation is not deleted; instead, it becomes an operation with
   264  // an Operation.error value with a google.rpc.Status.code of 1,
   265  // corresponding to Code.CANCELLED.
   266  func (c *OperationsClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error {
   267  	return c.internalClient.CancelOperation(ctx, req, opts...)
   268  }
   269  
   270  // WaitOperation waits until the specified long-running operation is done or reaches at most
   271  // a specified timeout, returning the latest state.  If the operation is
   272  // already done, the latest state is immediately returned.  If the timeout
   273  // specified is greater than the default HTTP/RPC timeout, the HTTP/RPC
   274  // timeout is used.  If the server does not support this method, it returns
   275  // google.rpc.Code.UNIMPLEMENTED.
   276  // Note that this method is on a best-effort basis.  It may return the latest
   277  // state before the specified timeout (including immediately), meaning even an
   278  // immediate response is no guarantee that the operation is done.
   279  func (c *OperationsClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   280  	return c.internalClient.WaitOperation(ctx, req, opts...)
   281  }
   282  
   283  // operationsGRPCClient is a client for interacting with Long Running Operations API over gRPC transport.
   284  //
   285  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   286  type operationsGRPCClient struct {
   287  	// Connection pool of gRPC connections to the service.
   288  	connPool gtransport.ConnPool
   289  
   290  	// Points back to the CallOptions field of the containing OperationsClient
   291  	CallOptions **OperationsCallOptions
   292  
   293  	// The gRPC API client.
   294  	operationsClient longrunningpb.OperationsClient
   295  
   296  	// The x-goog-* metadata to be sent with each request.
   297  	xGoogHeaders []string
   298  }
   299  
   300  // NewOperationsClient creates a new operations client based on gRPC.
   301  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   302  //
   303  // Manages long-running operations with an API service.
   304  //
   305  // When an API method normally takes long time to complete, it can be designed
   306  // to return Operation to the client, and the client can use this
   307  // interface to receive the real response asynchronously by polling the
   308  // operation resource, or pass the operation resource to another API (such as
   309  // Google Cloud Pub/Sub API) to receive the response.  Any API service that
   310  // returns long-running operations should implement the Operations interface
   311  // so developers can have a consistent client experience.
   312  func NewOperationsClient(ctx context.Context, opts ...option.ClientOption) (*OperationsClient, error) {
   313  	clientOpts := defaultOperationsGRPCClientOptions()
   314  	if newOperationsClientHook != nil {
   315  		hookOpts, err := newOperationsClientHook(ctx, clientHookParams{})
   316  		if err != nil {
   317  			return nil, err
   318  		}
   319  		clientOpts = append(clientOpts, hookOpts...)
   320  	}
   321  
   322  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   323  	if err != nil {
   324  		return nil, err
   325  	}
   326  	client := OperationsClient{CallOptions: defaultOperationsCallOptions()}
   327  
   328  	c := &operationsGRPCClient{
   329  		connPool:         connPool,
   330  		operationsClient: longrunningpb.NewOperationsClient(connPool),
   331  		CallOptions:      &client.CallOptions,
   332  	}
   333  	c.setGoogleClientInfo()
   334  
   335  	client.internalClient = c
   336  
   337  	return &client, nil
   338  }
   339  
   340  // Connection returns a connection to the API service.
   341  //
   342  // Deprecated: Connections are now pooled so this method does not always
   343  // return the same resource.
   344  func (c *operationsGRPCClient) Connection() *grpc.ClientConn {
   345  	return c.connPool.Conn()
   346  }
   347  
   348  // setGoogleClientInfo sets the name and version of the application in
   349  // the `x-goog-api-client` header passed on each request. Intended for
   350  // use by Google-written clients.
   351  func (c *operationsGRPCClient) setGoogleClientInfo(keyval ...string) {
   352  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   353  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   354  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   355  }
   356  
   357  // Close closes the connection to the API service. The user should invoke this when
   358  // the client is no longer required.
   359  func (c *operationsGRPCClient) Close() error {
   360  	return c.connPool.Close()
   361  }
   362  
   363  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   364  type operationsRESTClient struct {
   365  	// The http endpoint to connect to.
   366  	endpoint string
   367  
   368  	// The http client.
   369  	httpClient *http.Client
   370  
   371  	// The x-goog-* headers to be sent with each request.
   372  	xGoogHeaders []string
   373  
   374  	// Points back to the CallOptions field of the containing OperationsClient
   375  	CallOptions **OperationsCallOptions
   376  }
   377  
   378  // NewOperationsRESTClient creates a new operations rest client.
   379  //
   380  // Manages long-running operations with an API service.
   381  //
   382  // When an API method normally takes long time to complete, it can be designed
   383  // to return Operation to the client, and the client can use this
   384  // interface to receive the real response asynchronously by polling the
   385  // operation resource, or pass the operation resource to another API (such as
   386  // Google Cloud Pub/Sub API) to receive the response.  Any API service that
   387  // returns long-running operations should implement the Operations interface
   388  // so developers can have a consistent client experience.
   389  func NewOperationsRESTClient(ctx context.Context, opts ...option.ClientOption) (*OperationsClient, error) {
   390  	clientOpts := append(defaultOperationsRESTClientOptions(), opts...)
   391  	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
   392  	if err != nil {
   393  		return nil, err
   394  	}
   395  
   396  	callOpts := defaultOperationsRESTCallOptions()
   397  	c := &operationsRESTClient{
   398  		endpoint:    endpoint,
   399  		httpClient:  httpClient,
   400  		CallOptions: &callOpts,
   401  	}
   402  	c.setGoogleClientInfo()
   403  
   404  	return &OperationsClient{internalClient: c, CallOptions: callOpts}, nil
   405  }
   406  
   407  func defaultOperationsRESTClientOptions() []option.ClientOption {
   408  	return []option.ClientOption{
   409  		internaloption.WithDefaultEndpoint("https://longrunning.googleapis.com"),
   410  		internaloption.WithDefaultEndpointTemplate("https://longrunning.UNIVERSE_DOMAIN"),
   411  		internaloption.WithDefaultMTLSEndpoint("https://longrunning.mtls.googleapis.com"),
   412  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
   413  		internaloption.WithDefaultAudience("https://longrunning.googleapis.com/"),
   414  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
   415  	}
   416  }
   417  
   418  // setGoogleClientInfo sets the name and version of the application in
   419  // the `x-goog-api-client` header passed on each request. Intended for
   420  // use by Google-written clients.
   421  func (c *operationsRESTClient) setGoogleClientInfo(keyval ...string) {
   422  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   423  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
   424  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   425  }
   426  
   427  // Close closes the connection to the API service. The user should invoke this when
   428  // the client is no longer required.
   429  func (c *operationsRESTClient) Close() error {
   430  	// Replace httpClient with nil to force cleanup.
   431  	c.httpClient = nil
   432  	return nil
   433  }
   434  
   435  // Connection returns a connection to the API service.
   436  //
   437  // Deprecated: This method always returns nil.
   438  func (c *operationsRESTClient) Connection() *grpc.ClientConn {
   439  	return nil
   440  }
   441  func (c *operationsGRPCClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
   442  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   443  
   444  	hds = append(c.xGoogHeaders, hds...)
   445  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   446  	opts = append((*c.CallOptions).ListOperations[0:len((*c.CallOptions).ListOperations):len((*c.CallOptions).ListOperations)], opts...)
   447  	it := &OperationIterator{}
   448  	req = proto.Clone(req).(*longrunningpb.ListOperationsRequest)
   449  	it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
   450  		resp := &longrunningpb.ListOperationsResponse{}
   451  		if pageToken != "" {
   452  			req.PageToken = pageToken
   453  		}
   454  		if pageSize > math.MaxInt32 {
   455  			req.PageSize = math.MaxInt32
   456  		} else if pageSize != 0 {
   457  			req.PageSize = int32(pageSize)
   458  		}
   459  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   460  			var err error
   461  			resp, err = c.operationsClient.ListOperations(ctx, req, settings.GRPC...)
   462  			return err
   463  		}, opts...)
   464  		if err != nil {
   465  			return nil, "", err
   466  		}
   467  
   468  		it.Response = resp
   469  		return resp.GetOperations(), resp.GetNextPageToken(), nil
   470  	}
   471  	fetch := func(pageSize int, pageToken string) (string, error) {
   472  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   473  		if err != nil {
   474  			return "", err
   475  		}
   476  		it.items = append(it.items, items...)
   477  		return nextPageToken, nil
   478  	}
   479  
   480  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   481  	it.pageInfo.MaxSize = int(req.GetPageSize())
   482  	it.pageInfo.Token = req.GetPageToken()
   483  
   484  	return it
   485  }
   486  
   487  func (c *operationsGRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   488  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   489  
   490  	hds = append(c.xGoogHeaders, hds...)
   491  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   492  	opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
   493  	var resp *longrunningpb.Operation
   494  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   495  		var err error
   496  		resp, err = c.operationsClient.GetOperation(ctx, req, settings.GRPC...)
   497  		return err
   498  	}, opts...)
   499  	if err != nil {
   500  		return nil, err
   501  	}
   502  	return resp, nil
   503  }
   504  
   505  func (c *operationsGRPCClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error {
   506  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   507  
   508  	hds = append(c.xGoogHeaders, hds...)
   509  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   510  	opts = append((*c.CallOptions).DeleteOperation[0:len((*c.CallOptions).DeleteOperation):len((*c.CallOptions).DeleteOperation)], opts...)
   511  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   512  		var err error
   513  		_, err = c.operationsClient.DeleteOperation(ctx, req, settings.GRPC...)
   514  		return err
   515  	}, opts...)
   516  	return err
   517  }
   518  
   519  func (c *operationsGRPCClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error {
   520  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   521  
   522  	hds = append(c.xGoogHeaders, hds...)
   523  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   524  	opts = append((*c.CallOptions).CancelOperation[0:len((*c.CallOptions).CancelOperation):len((*c.CallOptions).CancelOperation)], opts...)
   525  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   526  		var err error
   527  		_, err = c.operationsClient.CancelOperation(ctx, req, settings.GRPC...)
   528  		return err
   529  	}, opts...)
   530  	return err
   531  }
   532  
   533  func (c *operationsGRPCClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   534  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
   535  	opts = append((*c.CallOptions).WaitOperation[0:len((*c.CallOptions).WaitOperation):len((*c.CallOptions).WaitOperation)], opts...)
   536  	var resp *longrunningpb.Operation
   537  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   538  		var err error
   539  		resp, err = c.operationsClient.WaitOperation(ctx, req, settings.GRPC...)
   540  		return err
   541  	}, opts...)
   542  	if err != nil {
   543  		return nil, err
   544  	}
   545  	return resp, nil
   546  }
   547  
   548  // ListOperations lists operations that match the specified filter in the request. If the
   549  // server doesn’t support this method, it returns UNIMPLEMENTED.
   550  //
   551  // NOTE: the name binding allows API services to override the binding
   552  // to use different resource name schemes, such as users/*/operations. To
   553  // override the binding, API services can add a binding such as
   554  // "/v1/{name=users/*}/operations" to their service configuration.
   555  // For backwards compatibility, the default name includes the operations
   556  // collection id, however overriding users must ensure the name binding
   557  // is the parent resource, without the operations collection id.
   558  func (c *operationsRESTClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
   559  	it := &OperationIterator{}
   560  	req = proto.Clone(req).(*longrunningpb.ListOperationsRequest)
   561  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   562  	it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
   563  		resp := &longrunningpb.ListOperationsResponse{}
   564  		if pageToken != "" {
   565  			req.PageToken = pageToken
   566  		}
   567  		if pageSize > math.MaxInt32 {
   568  			req.PageSize = math.MaxInt32
   569  		} else if pageSize != 0 {
   570  			req.PageSize = int32(pageSize)
   571  		}
   572  		baseUrl, err := url.Parse(c.endpoint)
   573  		if err != nil {
   574  			return nil, "", err
   575  		}
   576  		baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   577  
   578  		params := url.Values{}
   579  		if req.GetFilter() != "" {
   580  			params.Add("filter", fmt.Sprintf("%v", req.GetFilter()))
   581  		}
   582  		if req.GetPageSize() != 0 {
   583  			params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
   584  		}
   585  		if req.GetPageToken() != "" {
   586  			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
   587  		}
   588  
   589  		baseUrl.RawQuery = params.Encode()
   590  
   591  		// Build HTTP headers from client and context metadata.
   592  		hds := append(c.xGoogHeaders, "Content-Type", "application/json")
   593  		headers := gax.BuildHeaders(ctx, hds...)
   594  		e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   595  			if settings.Path != "" {
   596  				baseUrl.Path = settings.Path
   597  			}
   598  			httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   599  			if err != nil {
   600  				return err
   601  			}
   602  			httpReq.Header = headers
   603  
   604  			httpRsp, err := c.httpClient.Do(httpReq)
   605  			if err != nil {
   606  				return err
   607  			}
   608  			defer httpRsp.Body.Close()
   609  
   610  			if err = googleapi.CheckResponse(httpRsp); err != nil {
   611  				return err
   612  			}
   613  
   614  			buf, err := io.ReadAll(httpRsp.Body)
   615  			if err != nil {
   616  				return err
   617  			}
   618  
   619  			if err := unm.Unmarshal(buf, resp); err != nil {
   620  				return err
   621  			}
   622  
   623  			return nil
   624  		}, opts...)
   625  		if e != nil {
   626  			return nil, "", e
   627  		}
   628  		it.Response = resp
   629  		return resp.GetOperations(), resp.GetNextPageToken(), nil
   630  	}
   631  
   632  	fetch := func(pageSize int, pageToken string) (string, error) {
   633  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   634  		if err != nil {
   635  			return "", err
   636  		}
   637  		it.items = append(it.items, items...)
   638  		return nextPageToken, nil
   639  	}
   640  
   641  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   642  	it.pageInfo.MaxSize = int(req.GetPageSize())
   643  	it.pageInfo.Token = req.GetPageToken()
   644  
   645  	return it
   646  }
   647  
   648  // GetOperation gets the latest state of a long-running operation.  Clients can use this
   649  // method to poll the operation result at intervals as recommended by the API
   650  // service.
   651  func (c *operationsRESTClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   652  	baseUrl, err := url.Parse(c.endpoint)
   653  	if err != nil {
   654  		return nil, err
   655  	}
   656  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   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  	opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
   665  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   666  	resp := &longrunningpb.Operation{}
   667  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   668  		if settings.Path != "" {
   669  			baseUrl.Path = settings.Path
   670  		}
   671  		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   672  		if err != nil {
   673  			return err
   674  		}
   675  		httpReq = httpReq.WithContext(ctx)
   676  		httpReq.Header = headers
   677  
   678  		httpRsp, err := c.httpClient.Do(httpReq)
   679  		if err != nil {
   680  			return err
   681  		}
   682  		defer httpRsp.Body.Close()
   683  
   684  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   685  			return err
   686  		}
   687  
   688  		buf, err := io.ReadAll(httpRsp.Body)
   689  		if err != nil {
   690  			return err
   691  		}
   692  
   693  		if err := unm.Unmarshal(buf, resp); err != nil {
   694  			return err
   695  		}
   696  
   697  		return nil
   698  	}, opts...)
   699  	if e != nil {
   700  		return nil, e
   701  	}
   702  	return resp, nil
   703  }
   704  
   705  // DeleteOperation deletes a long-running operation. This method indicates that the client is
   706  // no longer interested in the operation result. It does not cancel the
   707  // operation. If the server doesn’t support this method, it returns
   708  // google.rpc.Code.UNIMPLEMENTED.
   709  func (c *operationsRESTClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error {
   710  	baseUrl, err := url.Parse(c.endpoint)
   711  	if err != nil {
   712  		return err
   713  	}
   714  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   715  
   716  	// Build HTTP headers from client and context metadata.
   717  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   718  
   719  	hds = append(c.xGoogHeaders, hds...)
   720  	hds = append(hds, "Content-Type", "application/json")
   721  	headers := gax.BuildHeaders(ctx, hds...)
   722  	return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   723  		if settings.Path != "" {
   724  			baseUrl.Path = settings.Path
   725  		}
   726  		httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
   727  		if err != nil {
   728  			return err
   729  		}
   730  		httpReq = httpReq.WithContext(ctx)
   731  		httpReq.Header = headers
   732  
   733  		httpRsp, err := c.httpClient.Do(httpReq)
   734  		if err != nil {
   735  			return err
   736  		}
   737  		defer httpRsp.Body.Close()
   738  
   739  		// Returns nil if there is no error, otherwise wraps
   740  		// the response code and body into a non-nil error
   741  		return googleapi.CheckResponse(httpRsp)
   742  	}, opts...)
   743  }
   744  
   745  // CancelOperation starts asynchronous cancellation on a long-running operation.  The server
   746  // makes a best effort to cancel the operation, but success is not
   747  // guaranteed.  If the server doesn’t support this method, it returns
   748  // google.rpc.Code.UNIMPLEMENTED.  Clients can use
   749  // Operations.GetOperation or
   750  // other methods to check whether the cancellation succeeded or whether the
   751  // operation completed despite cancellation. On successful cancellation,
   752  // the operation is not deleted; instead, it becomes an operation with
   753  // an Operation.error value with a google.rpc.Status.code of 1,
   754  // corresponding to Code.CANCELLED.
   755  func (c *operationsRESTClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error {
   756  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   757  	jsonReq, err := m.Marshal(req)
   758  	if err != nil {
   759  		return err
   760  	}
   761  
   762  	baseUrl, err := url.Parse(c.endpoint)
   763  	if err != nil {
   764  		return err
   765  	}
   766  	baseUrl.Path += fmt.Sprintf("/v1/%v:cancel", req.GetName())
   767  
   768  	// Build HTTP headers from client and context metadata.
   769  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   770  
   771  	hds = append(c.xGoogHeaders, hds...)
   772  	hds = append(hds, "Content-Type", "application/json")
   773  	headers := gax.BuildHeaders(ctx, hds...)
   774  	return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   775  		if settings.Path != "" {
   776  			baseUrl.Path = settings.Path
   777  		}
   778  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   779  		if err != nil {
   780  			return err
   781  		}
   782  		httpReq = httpReq.WithContext(ctx)
   783  		httpReq.Header = headers
   784  
   785  		httpRsp, err := c.httpClient.Do(httpReq)
   786  		if err != nil {
   787  			return err
   788  		}
   789  		defer httpRsp.Body.Close()
   790  
   791  		// Returns nil if there is no error, otherwise wraps
   792  		// the response code and body into a non-nil error
   793  		return googleapi.CheckResponse(httpRsp)
   794  	}, opts...)
   795  }
   796  
   797  // WaitOperation waits until the specified long-running operation is done or reaches at most
   798  // a specified timeout, returning the latest state.  If the operation is
   799  // already done, the latest state is immediately returned.  If the timeout
   800  // specified is greater than the default HTTP/RPC timeout, the HTTP/RPC
   801  // timeout is used.  If the server does not support this method, it returns
   802  // google.rpc.Code.UNIMPLEMENTED.
   803  // Note that this method is on a best-effort basis.  It may return the latest
   804  // state before the specified timeout (including immediately), meaning even an
   805  // immediate response is no guarantee that the operation is done.
   806  func (c *operationsRESTClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   807  	baseUrl, err := url.Parse(c.endpoint)
   808  	if err != nil {
   809  		return nil, err
   810  	}
   811  	baseUrl.Path += fmt.Sprintf("")
   812  
   813  	params := url.Values{}
   814  	if req.GetName() != "" {
   815  		params.Add("name", fmt.Sprintf("%v", req.GetName()))
   816  	}
   817  	if req.GetTimeout() != nil {
   818  		timeout, err := protojson.Marshal(req.GetTimeout())
   819  		if err != nil {
   820  			return nil, err
   821  		}
   822  		params.Add("timeout", string(timeout[1:len(timeout)-1]))
   823  	}
   824  
   825  	baseUrl.RawQuery = params.Encode()
   826  
   827  	// Build HTTP headers from client and context metadata.
   828  	hds := append(c.xGoogHeaders, "Content-Type", "application/json")
   829  	headers := gax.BuildHeaders(ctx, hds...)
   830  	opts = append((*c.CallOptions).WaitOperation[0:len((*c.CallOptions).WaitOperation):len((*c.CallOptions).WaitOperation)], opts...)
   831  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   832  	resp := &longrunningpb.Operation{}
   833  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   834  		if settings.Path != "" {
   835  			baseUrl.Path = settings.Path
   836  		}
   837  		httpReq, err := http.NewRequest("", baseUrl.String(), nil)
   838  		if err != nil {
   839  			return err
   840  		}
   841  		httpReq = httpReq.WithContext(ctx)
   842  		httpReq.Header = headers
   843  
   844  		httpRsp, err := c.httpClient.Do(httpReq)
   845  		if err != nil {
   846  			return err
   847  		}
   848  		defer httpRsp.Body.Close()
   849  
   850  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   851  			return err
   852  		}
   853  
   854  		buf, err := io.ReadAll(httpRsp.Body)
   855  		if err != nil {
   856  			return err
   857  		}
   858  
   859  		if err := unm.Unmarshal(buf, resp); err != nil {
   860  			return err
   861  		}
   862  
   863  		return nil
   864  	}, opts...)
   865  	if e != nil {
   866  		return nil, e
   867  	}
   868  	return resp, nil
   869  }
   870  

View as plain text