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.
 
 
 

136 lines
4.6 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. gax "github.com/googleapis/gax-go/v2"
  20. "google.golang.org/api/option"
  21. "google.golang.org/api/transport"
  22. visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1p1beta1"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/metadata"
  26. )
  27. // ImageAnnotatorCallOptions contains the retry settings for each method of ImageAnnotatorClient.
  28. type ImageAnnotatorCallOptions struct {
  29. BatchAnnotateImages []gax.CallOption
  30. }
  31. func defaultImageAnnotatorClientOptions() []option.ClientOption {
  32. return []option.ClientOption{
  33. option.WithEndpoint("vision.googleapis.com:443"),
  34. option.WithScopes(DefaultAuthScopes()...),
  35. }
  36. }
  37. func defaultImageAnnotatorCallOptions() *ImageAnnotatorCallOptions {
  38. retry := map[[2]string][]gax.CallOption{
  39. {"default", "idempotent"}: {
  40. gax.WithRetry(func() gax.Retryer {
  41. return gax.OnCodes([]codes.Code{
  42. codes.DeadlineExceeded,
  43. codes.Unavailable,
  44. }, gax.Backoff{
  45. Initial: 100 * time.Millisecond,
  46. Max: 60000 * time.Millisecond,
  47. Multiplier: 1.3,
  48. })
  49. }),
  50. },
  51. }
  52. return &ImageAnnotatorCallOptions{
  53. BatchAnnotateImages: retry[[2]string{"default", "idempotent"}],
  54. }
  55. }
  56. // ImageAnnotatorClient is a client for interacting with Google Cloud Vision API.
  57. //
  58. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  59. type ImageAnnotatorClient struct {
  60. // The connection to the service.
  61. conn *grpc.ClientConn
  62. // The gRPC API client.
  63. imageAnnotatorClient visionpb.ImageAnnotatorClient
  64. // The call options for this service.
  65. CallOptions *ImageAnnotatorCallOptions
  66. // The x-goog-* metadata to be sent with each request.
  67. xGoogMetadata metadata.MD
  68. }
  69. // NewImageAnnotatorClient creates a new image annotator client.
  70. //
  71. // Service that performs Google Cloud Vision API detection tasks over client
  72. // images, such as face, landmark, logo, label, and text detection. The
  73. // ImageAnnotator service returns detected entities from the images.
  74. func NewImageAnnotatorClient(ctx context.Context, opts ...option.ClientOption) (*ImageAnnotatorClient, error) {
  75. conn, err := transport.DialGRPC(ctx, append(defaultImageAnnotatorClientOptions(), opts...)...)
  76. if err != nil {
  77. return nil, err
  78. }
  79. c := &ImageAnnotatorClient{
  80. conn: conn,
  81. CallOptions: defaultImageAnnotatorCallOptions(),
  82. imageAnnotatorClient: visionpb.NewImageAnnotatorClient(conn),
  83. }
  84. c.setGoogleClientInfo()
  85. return c, nil
  86. }
  87. // Connection returns the client's connection to the API service.
  88. func (c *ImageAnnotatorClient) Connection() *grpc.ClientConn {
  89. return c.conn
  90. }
  91. // Close closes the connection to the API service. The user should invoke this when
  92. // the client is no longer required.
  93. func (c *ImageAnnotatorClient) Close() error {
  94. return c.conn.Close()
  95. }
  96. // setGoogleClientInfo sets the name and version of the application in
  97. // the `x-goog-api-client` header passed on each request. Intended for
  98. // use by Google-written clients.
  99. func (c *ImageAnnotatorClient) setGoogleClientInfo(keyval ...string) {
  100. kv := append([]string{"gl-go", versionGo()}, keyval...)
  101. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  102. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  103. }
  104. // BatchAnnotateImages run image detection and annotation for a batch of images.
  105. func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) {
  106. ctx = insertMetadata(ctx, c.xGoogMetadata)
  107. opts = append(c.CallOptions.BatchAnnotateImages[0:len(c.CallOptions.BatchAnnotateImages):len(c.CallOptions.BatchAnnotateImages)], opts...)
  108. var resp *visionpb.BatchAnnotateImagesResponse
  109. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  110. var err error
  111. resp, err = c.imageAnnotatorClient.BatchAnnotateImages(ctx, req, settings.GRPC...)
  112. return err
  113. }, opts...)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return resp, nil
  118. }