No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

250 líneas
9.8 KiB

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