Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

249 строки
9.7 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 vision
  16. import (
  17. "context"
  18. "time"
  19. "cloud.google.com/go/longrunning"
  20. lroauto "cloud.google.com/go/longrunning/autogen"
  21. gax "github.com/googleapis/gax-go/v2"
  22. "google.golang.org/api/option"
  23. "google.golang.org/api/transport"
  24. visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
  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. // ImageAnnotatorCallOptions contains the retry settings for each method of ImageAnnotatorClient.
  31. type ImageAnnotatorCallOptions struct {
  32. BatchAnnotateImages []gax.CallOption
  33. AsyncBatchAnnotateFiles []gax.CallOption
  34. }
  35. func defaultImageAnnotatorClientOptions() []option.ClientOption {
  36. return []option.ClientOption{
  37. option.WithEndpoint("vision.googleapis.com:443"),
  38. option.WithScopes(DefaultAuthScopes()...),
  39. }
  40. }
  41. func defaultImageAnnotatorCallOptions() *ImageAnnotatorCallOptions {
  42. retry := map[[2]string][]gax.CallOption{
  43. {"default", "idempotent"}: {
  44. gax.WithRetry(func() gax.Retryer {
  45. return gax.OnCodes([]codes.Code{
  46. codes.DeadlineExceeded,
  47. codes.Unavailable,
  48. }, gax.Backoff{
  49. Initial: 100 * time.Millisecond,
  50. Max: 60000 * time.Millisecond,
  51. Multiplier: 1.3,
  52. })
  53. }),
  54. },
  55. }
  56. return &ImageAnnotatorCallOptions{
  57. BatchAnnotateImages: retry[[2]string{"default", "idempotent"}],
  58. AsyncBatchAnnotateFiles: retry[[2]string{"default", "idempotent"}],
  59. }
  60. }
  61. // ImageAnnotatorClient is a client for interacting with Cloud Vision API.
  62. //
  63. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  64. type ImageAnnotatorClient struct {
  65. // The connection to the service.
  66. conn *grpc.ClientConn
  67. // The gRPC API client.
  68. imageAnnotatorClient visionpb.ImageAnnotatorClient
  69. // LROClient is used internally to handle longrunning operations.
  70. // It is exposed so that its CallOptions can be modified if required.
  71. // Users should not Close this client.
  72. LROClient *lroauto.OperationsClient
  73. // The call options for this service.
  74. CallOptions *ImageAnnotatorCallOptions
  75. // The x-goog-* metadata to be sent with each request.
  76. xGoogMetadata metadata.MD
  77. }
  78. // NewImageAnnotatorClient creates a new image annotator client.
  79. //
  80. // Service that performs Google Cloud Vision API detection tasks over client
  81. // images, such as face, landmark, logo, label, and text detection. The
  82. // ImageAnnotator service returns detected entities from the images.
  83. func NewImageAnnotatorClient(ctx context.Context, opts ...option.ClientOption) (*ImageAnnotatorClient, error) {
  84. conn, err := transport.DialGRPC(ctx, append(defaultImageAnnotatorClientOptions(), opts...)...)
  85. if err != nil {
  86. return nil, err
  87. }
  88. c := &ImageAnnotatorClient{
  89. conn: conn,
  90. CallOptions: defaultImageAnnotatorCallOptions(),
  91. imageAnnotatorClient: visionpb.NewImageAnnotatorClient(conn),
  92. }
  93. c.setGoogleClientInfo()
  94. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  95. if err != nil {
  96. // This error "should not happen", since we are just reusing old connection
  97. // and never actually need to dial.
  98. // If this does happen, we could leak conn. However, we cannot close conn:
  99. // If the user invoked the function with option.WithGRPCConn,
  100. // we would close a connection that's still in use.
  101. // TODO(pongad): investigate error conditions.
  102. return nil, err
  103. }
  104. return c, nil
  105. }
  106. // Connection returns the client's connection to the API service.
  107. func (c *ImageAnnotatorClient) Connection() *grpc.ClientConn {
  108. return c.conn
  109. }
  110. // Close closes the connection to the API service. The user should invoke this when
  111. // the client is no longer required.
  112. func (c *ImageAnnotatorClient) Close() error {
  113. return c.conn.Close()
  114. }
  115. // setGoogleClientInfo sets the name and version of the application in
  116. // the `x-goog-api-client` header passed on each request. Intended for
  117. // use by Google-written clients.
  118. func (c *ImageAnnotatorClient) setGoogleClientInfo(keyval ...string) {
  119. kv := append([]string{"gl-go", versionGo()}, keyval...)
  120. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  121. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  122. }
  123. // BatchAnnotateImages run image detection and annotation for a batch of images.
  124. func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) {
  125. ctx = insertMetadata(ctx, c.xGoogMetadata)
  126. opts = append(c.CallOptions.BatchAnnotateImages[0:len(c.CallOptions.BatchAnnotateImages):len(c.CallOptions.BatchAnnotateImages)], opts...)
  127. var resp *visionpb.BatchAnnotateImagesResponse
  128. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  129. var err error
  130. resp, err = c.imageAnnotatorClient.BatchAnnotateImages(ctx, req, settings.GRPC...)
  131. return err
  132. }, opts...)
  133. if err != nil {
  134. return nil, err
  135. }
  136. return resp, nil
  137. }
  138. // AsyncBatchAnnotateFiles run asynchronous image detection and annotation for a list of generic
  139. // files, such as PDF files, which may contain multiple pages and multiple
  140. // images per page. Progress and results can be retrieved through the
  141. // google.longrunning.Operations interface.
  142. // Operation.metadata contains OperationMetadata (metadata).
  143. // Operation.response contains AsyncBatchAnnotateFilesResponse (results).
  144. func (c *ImageAnnotatorClient) AsyncBatchAnnotateFiles(ctx context.Context, req *visionpb.AsyncBatchAnnotateFilesRequest, opts ...gax.CallOption) (*AsyncBatchAnnotateFilesOperation, error) {
  145. ctx = insertMetadata(ctx, c.xGoogMetadata)
  146. opts = append(c.CallOptions.AsyncBatchAnnotateFiles[0:len(c.CallOptions.AsyncBatchAnnotateFiles):len(c.CallOptions.AsyncBatchAnnotateFiles)], opts...)
  147. var resp *longrunningpb.Operation
  148. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  149. var err error
  150. resp, err = c.imageAnnotatorClient.AsyncBatchAnnotateFiles(ctx, req, settings.GRPC...)
  151. return err
  152. }, opts...)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return &AsyncBatchAnnotateFilesOperation{
  157. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  158. }, nil
  159. }
  160. // AsyncBatchAnnotateFilesOperation manages a long-running operation from AsyncBatchAnnotateFiles.
  161. type AsyncBatchAnnotateFilesOperation struct {
  162. lro *longrunning.Operation
  163. }
  164. // AsyncBatchAnnotateFilesOperation returns a new AsyncBatchAnnotateFilesOperation from a given name.
  165. // The name must be that of a previously created AsyncBatchAnnotateFilesOperation, possibly from a different process.
  166. func (c *ImageAnnotatorClient) AsyncBatchAnnotateFilesOperation(name string) *AsyncBatchAnnotateFilesOperation {
  167. return &AsyncBatchAnnotateFilesOperation{
  168. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  169. }
  170. }
  171. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  172. //
  173. // See documentation of Poll for error-handling information.
  174. func (op *AsyncBatchAnnotateFilesOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*visionpb.AsyncBatchAnnotateFilesResponse, error) {
  175. var resp visionpb.AsyncBatchAnnotateFilesResponse
  176. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  177. return nil, err
  178. }
  179. return &resp, nil
  180. }
  181. // Poll fetches the latest state of the long-running operation.
  182. //
  183. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  184. //
  185. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  186. // the operation has completed with failure, the error is returned and op.Done will return true.
  187. // If Poll succeeds and the operation has completed successfully,
  188. // op.Done will return true, and the response of the operation is returned.
  189. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  190. func (op *AsyncBatchAnnotateFilesOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*visionpb.AsyncBatchAnnotateFilesResponse, error) {
  191. var resp visionpb.AsyncBatchAnnotateFilesResponse
  192. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  193. return nil, err
  194. }
  195. if !op.Done() {
  196. return nil, nil
  197. }
  198. return &resp, nil
  199. }
  200. // Metadata returns metadata associated with the long-running operation.
  201. // Metadata itself does not contact the server, but Poll does.
  202. // To get the latest metadata, call this method after a successful call to Poll.
  203. // If the metadata is not available, the returned metadata and error are both nil.
  204. func (op *AsyncBatchAnnotateFilesOperation) Metadata() (*visionpb.OperationMetadata, error) {
  205. var meta visionpb.OperationMetadata
  206. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  207. return nil, nil
  208. } else if err != nil {
  209. return nil, err
  210. }
  211. return &meta, nil
  212. }
  213. // Done reports whether the long-running operation has completed.
  214. func (op *AsyncBatchAnnotateFilesOperation) Done() bool {
  215. return op.lro.Done()
  216. }
  217. // Name returns the name of the long-running operation.
  218. // The name is assigned by the server and is unique within the service from which the operation is created.
  219. func (op *AsyncBatchAnnotateFilesOperation) Name() string {
  220. return op.lro.Name()
  221. }