You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

273 lines
10 KiB

  1. // Copyright 2019 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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package longrunning
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "github.com/golang/protobuf/proto"
  21. gax "github.com/googleapis/gax-go/v2"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // OperationsCallOptions contains the retry settings for each method of OperationsClient.
  31. type OperationsCallOptions struct {
  32. GetOperation []gax.CallOption
  33. ListOperations []gax.CallOption
  34. CancelOperation []gax.CallOption
  35. DeleteOperation []gax.CallOption
  36. }
  37. func defaultOperationsClientOptions() []option.ClientOption {
  38. return []option.ClientOption{
  39. option.WithEndpoint("longrunning.googleapis.com:443"),
  40. option.WithScopes(DefaultAuthScopes()...),
  41. }
  42. }
  43. func defaultOperationsCallOptions() *OperationsCallOptions {
  44. retry := map[[2]string][]gax.CallOption{
  45. {"default", "idempotent"}: {
  46. gax.WithRetry(func() gax.Retryer {
  47. return gax.OnCodes([]codes.Code{
  48. codes.DeadlineExceeded,
  49. codes.Unavailable,
  50. }, gax.Backoff{
  51. Initial: 100 * time.Millisecond,
  52. Max: 60000 * time.Millisecond,
  53. Multiplier: 1.3,
  54. })
  55. }),
  56. },
  57. }
  58. return &OperationsCallOptions{
  59. GetOperation: retry[[2]string{"default", "idempotent"}],
  60. ListOperations: retry[[2]string{"default", "idempotent"}],
  61. CancelOperation: retry[[2]string{"default", "idempotent"}],
  62. DeleteOperation: retry[[2]string{"default", "idempotent"}],
  63. }
  64. }
  65. // OperationsClient is a client for interacting with Google Long Running Operations API.
  66. //
  67. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  68. type OperationsClient struct {
  69. // The connection to the service.
  70. conn *grpc.ClientConn
  71. // The gRPC API client.
  72. operationsClient longrunningpb.OperationsClient
  73. // The call options for this service.
  74. CallOptions *OperationsCallOptions
  75. // The x-goog-* metadata to be sent with each request.
  76. xGoogMetadata metadata.MD
  77. }
  78. // NewOperationsClient creates a new operations client.
  79. //
  80. // Manages long-running operations with an API service.
  81. //
  82. // When an API method normally takes long time to complete, it can be designed
  83. // to return [Operation][google.longrunning.Operation] to the client, and the
  84. // client can use this interface to receive the real response asynchronously by
  85. // polling the operation resource, or pass the operation resource to another API
  86. // (such as Google Cloud Pub/Sub API) to receive the response. Any API service
  87. // that returns long-running operations should implement the Operations
  88. // interface so developers can have a consistent client experience.
  89. func NewOperationsClient(ctx context.Context, opts ...option.ClientOption) (*OperationsClient, error) {
  90. conn, err := transport.DialGRPC(ctx, append(defaultOperationsClientOptions(), opts...)...)
  91. if err != nil {
  92. return nil, err
  93. }
  94. c := &OperationsClient{
  95. conn: conn,
  96. CallOptions: defaultOperationsCallOptions(),
  97. operationsClient: longrunningpb.NewOperationsClient(conn),
  98. }
  99. c.SetGoogleClientInfo()
  100. return c, nil
  101. }
  102. // Connection returns the client's connection to the API service.
  103. func (c *OperationsClient) Connection() *grpc.ClientConn {
  104. return c.conn
  105. }
  106. // Close closes the connection to the API service. The user should invoke this when
  107. // the client is no longer required.
  108. func (c *OperationsClient) Close() error {
  109. return c.conn.Close()
  110. }
  111. // SetGoogleClientInfo sets the name and version of the application in
  112. // the `x-goog-api-client` header passed on each request. Intended for
  113. // use by Google-written clients.
  114. func (c *OperationsClient) SetGoogleClientInfo(keyval ...string) {
  115. kv := append([]string{"gl-go", versionGo()}, keyval...)
  116. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  117. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  118. }
  119. // GetOperation gets the latest state of a long-running operation. Clients can use this
  120. // method to poll the operation result at intervals as recommended by the API
  121. // service.
  122. func (c *OperationsClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
  123. ctx = insertMetadata(ctx, c.xGoogMetadata)
  124. opts = append(c.CallOptions.GetOperation[0:len(c.CallOptions.GetOperation):len(c.CallOptions.GetOperation)], opts...)
  125. var resp *longrunningpb.Operation
  126. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  127. var err error
  128. resp, err = c.operationsClient.GetOperation(ctx, req, settings.GRPC...)
  129. return err
  130. }, opts...)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return resp, nil
  135. }
  136. // ListOperations lists operations that match the specified filter in the request. If the
  137. // server doesn't support this method, it returns UNIMPLEMENTED.
  138. //
  139. // NOTE: the name binding below allows API services to override the binding
  140. // to use different resource name schemes, such as users/*/operations.
  141. func (c *OperationsClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
  142. ctx = insertMetadata(ctx, c.xGoogMetadata)
  143. opts = append(c.CallOptions.ListOperations[0:len(c.CallOptions.ListOperations):len(c.CallOptions.ListOperations)], opts...)
  144. it := &OperationIterator{}
  145. req = proto.Clone(req).(*longrunningpb.ListOperationsRequest)
  146. it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
  147. var resp *longrunningpb.ListOperationsResponse
  148. req.PageToken = pageToken
  149. if pageSize > math.MaxInt32 {
  150. req.PageSize = math.MaxInt32
  151. } else {
  152. req.PageSize = int32(pageSize)
  153. }
  154. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  155. var err error
  156. resp, err = c.operationsClient.ListOperations(ctx, req, settings.GRPC...)
  157. return err
  158. }, opts...)
  159. if err != nil {
  160. return nil, "", err
  161. }
  162. return resp.Operations, resp.NextPageToken, nil
  163. }
  164. fetch := func(pageSize int, pageToken string) (string, error) {
  165. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  166. if err != nil {
  167. return "", err
  168. }
  169. it.items = append(it.items, items...)
  170. return nextPageToken, nil
  171. }
  172. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  173. it.pageInfo.MaxSize = int(req.PageSize)
  174. return it
  175. }
  176. // CancelOperation starts asynchronous cancellation on a long-running operation. The server
  177. // makes a best effort to cancel the operation, but success is not
  178. // guaranteed. If the server doesn't support this method, it returns
  179. // google.rpc.Code.UNIMPLEMENTED. Clients can use
  180. // [Operations.GetOperation][google.longrunning.Operations.GetOperation] or
  181. // other methods to check whether the cancellation succeeded or whether the
  182. // operation completed despite cancellation. On successful cancellation,
  183. // the operation is not deleted; instead, it becomes an operation with
  184. // an [Operation.error][google.longrunning.Operation.error] value with a
  185. // [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to
  186. // Code.CANCELLED.
  187. func (c *OperationsClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error {
  188. ctx = insertMetadata(ctx, c.xGoogMetadata)
  189. opts = append(c.CallOptions.CancelOperation[0:len(c.CallOptions.CancelOperation):len(c.CallOptions.CancelOperation)], opts...)
  190. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  191. var err error
  192. _, err = c.operationsClient.CancelOperation(ctx, req, settings.GRPC...)
  193. return err
  194. }, opts...)
  195. return err
  196. }
  197. // DeleteOperation deletes a long-running operation. This method indicates that the client is
  198. // no longer interested in the operation result. It does not cancel the
  199. // operation. If the server doesn't support this method, it returns
  200. // google.rpc.Code.UNIMPLEMENTED.
  201. func (c *OperationsClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error {
  202. ctx = insertMetadata(ctx, c.xGoogMetadata)
  203. opts = append(c.CallOptions.DeleteOperation[0:len(c.CallOptions.DeleteOperation):len(c.CallOptions.DeleteOperation)], opts...)
  204. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  205. var err error
  206. _, err = c.operationsClient.DeleteOperation(ctx, req, settings.GRPC...)
  207. return err
  208. }, opts...)
  209. return err
  210. }
  211. // OperationIterator manages a stream of *longrunningpb.Operation.
  212. type OperationIterator struct {
  213. items []*longrunningpb.Operation
  214. pageInfo *iterator.PageInfo
  215. nextFunc func() error
  216. // InternalFetch is for use by the Google Cloud Libraries only.
  217. // It is not part of the stable interface of this package.
  218. //
  219. // InternalFetch returns results from a single call to the underlying RPC.
  220. // The number of results is no greater than pageSize.
  221. // If there are no more results, nextPageToken is empty and err is nil.
  222. InternalFetch func(pageSize int, pageToken string) (results []*longrunningpb.Operation, nextPageToken string, err error)
  223. }
  224. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  225. func (it *OperationIterator) PageInfo() *iterator.PageInfo {
  226. return it.pageInfo
  227. }
  228. // Next returns the next result. Its second return value is iterator.Done if there are no more
  229. // results. Once Next returns Done, all subsequent calls will return Done.
  230. func (it *OperationIterator) Next() (*longrunningpb.Operation, error) {
  231. var item *longrunningpb.Operation
  232. if err := it.nextFunc(); err != nil {
  233. return item, err
  234. }
  235. item = it.items[0]
  236. it.items = it.items[1:]
  237. return item, nil
  238. }
  239. func (it *OperationIterator) bufLen() int {
  240. return len(it.items)
  241. }
  242. func (it *OperationIterator) takeBuf() interface{} {
  243. b := it.items
  244. it.items = nil
  245. return b
  246. }