25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

226 satır
8.1 KiB

  1. // Copyright 2017, Google Inc. All rights reserved.
  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. // http://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 videointelligence
  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. videointelligencepb "google.golang.org/genproto/googleapis/cloud/videointelligence/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. // CallOptions contains the retry settings for each method of Client.
  32. type CallOptions struct {
  33. AnnotateVideo []gax.CallOption
  34. }
  35. func defaultClientOptions() []option.ClientOption {
  36. return []option.ClientOption{
  37. option.WithEndpoint("videointelligence.googleapis.com:443"),
  38. option.WithScopes(DefaultAuthScopes()...),
  39. }
  40. }
  41. func defaultCallOptions() *CallOptions {
  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: 1000 * time.Millisecond,
  50. Max: 120000 * time.Millisecond,
  51. Multiplier: 2.5,
  52. })
  53. }),
  54. },
  55. }
  56. return &CallOptions{
  57. AnnotateVideo: retry[[2]string{"default", "idempotent"}],
  58. }
  59. }
  60. // Client is a client for interacting with Cloud Video Intelligence API.
  61. type Client struct {
  62. // The connection to the service.
  63. conn *grpc.ClientConn
  64. // The gRPC API client.
  65. client videointelligencepb.VideoIntelligenceServiceClient
  66. // LROClient is used internally to handle longrunning operations.
  67. // It is exposed so that its CallOptions can be modified if required.
  68. // Users should not Close this client.
  69. LROClient *lroauto.OperationsClient
  70. // The call options for this service.
  71. CallOptions *CallOptions
  72. // The metadata to be sent with each request.
  73. Metadata metadata.MD
  74. }
  75. // NewClient creates a new video intelligence service client.
  76. //
  77. // Service that implements Google Cloud Video Intelligence API.
  78. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  79. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  80. if err != nil {
  81. return nil, err
  82. }
  83. c := &Client{
  84. conn: conn,
  85. CallOptions: defaultCallOptions(),
  86. client: videointelligencepb.NewVideoIntelligenceServiceClient(conn),
  87. }
  88. c.setGoogleClientInfo()
  89. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  90. if err != nil {
  91. // This error "should not happen", since we are just reusing old connection
  92. // and never actually need to dial.
  93. // If this does happen, we could leak conn. However, we cannot close conn:
  94. // If the user invoked the function with option.WithGRPCConn,
  95. // we would close a connection that's still in use.
  96. // TODO(pongad): investigate error conditions.
  97. return nil, err
  98. }
  99. return c, nil
  100. }
  101. // Connection returns the client's connection to the API service.
  102. func (c *Client) Connection() *grpc.ClientConn {
  103. return c.conn
  104. }
  105. // Close closes the connection to the API service. The user should invoke this when
  106. // the client is no longer required.
  107. func (c *Client) Close() error {
  108. return c.conn.Close()
  109. }
  110. // setGoogleClientInfo sets the name and version of the application in
  111. // the `x-goog-api-client` header passed on each request. Intended for
  112. // use by Google-written clients.
  113. func (c *Client) setGoogleClientInfo(keyval ...string) {
  114. kv := append([]string{"gl-go", version.Go()}, keyval...)
  115. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  116. c.Metadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  117. }
  118. // AnnotateVideo performs asynchronous video annotation. Progress and results can be
  119. // retrieved through the google.longrunning.Operations interface.
  120. // Operation.metadata contains AnnotateVideoProgress (progress).
  121. // Operation.response contains AnnotateVideoResponse (results).
  122. func (c *Client) AnnotateVideo(ctx context.Context, req *videointelligencepb.AnnotateVideoRequest, opts ...gax.CallOption) (*AnnotateVideoOperation, error) {
  123. ctx = insertMetadata(ctx, c.Metadata)
  124. opts = append(c.CallOptions.AnnotateVideo[0:len(c.CallOptions.AnnotateVideo):len(c.CallOptions.AnnotateVideo)], 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.client.AnnotateVideo(ctx, req, settings.GRPC...)
  129. return err
  130. }, opts...)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return &AnnotateVideoOperation{
  135. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  136. }, nil
  137. }
  138. // AnnotateVideoOperation manages a long-running operation from AnnotateVideo.
  139. type AnnotateVideoOperation struct {
  140. lro *longrunning.Operation
  141. }
  142. // AnnotateVideoOperation returns a new AnnotateVideoOperation from a given name.
  143. // The name must be that of a previously created AnnotateVideoOperation, possibly from a different process.
  144. func (c *Client) AnnotateVideoOperation(name string) *AnnotateVideoOperation {
  145. return &AnnotateVideoOperation{
  146. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  147. }
  148. }
  149. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  150. //
  151. // See documentation of Poll for error-handling information.
  152. func (op *AnnotateVideoOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*videointelligencepb.AnnotateVideoResponse, error) {
  153. var resp videointelligencepb.AnnotateVideoResponse
  154. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  155. return nil, err
  156. }
  157. return &resp, nil
  158. }
  159. // Poll fetches the latest state of the long-running operation.
  160. //
  161. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  162. //
  163. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  164. // the operation has completed with failure, the error is returned and op.Done will return true.
  165. // If Poll succeeds and the operation has completed successfully,
  166. // op.Done will return true, and the response of the operation is returned.
  167. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  168. func (op *AnnotateVideoOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*videointelligencepb.AnnotateVideoResponse, error) {
  169. var resp videointelligencepb.AnnotateVideoResponse
  170. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  171. return nil, err
  172. }
  173. if !op.Done() {
  174. return nil, nil
  175. }
  176. return &resp, nil
  177. }
  178. // Metadata returns metadata associated with the long-running operation.
  179. // Metadata itself does not contact the server, but Poll does.
  180. // To get the latest metadata, call this method after a successful call to Poll.
  181. // If the metadata is not available, the returned metadata and error are both nil.
  182. func (op *AnnotateVideoOperation) Metadata() (*videointelligencepb.AnnotateVideoProgress, error) {
  183. var meta videointelligencepb.AnnotateVideoProgress
  184. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  185. return nil, nil
  186. } else if err != nil {
  187. return nil, err
  188. }
  189. return &meta, nil
  190. }
  191. // Done reports whether the long-running operation has completed.
  192. func (op *AnnotateVideoOperation) Done() bool {
  193. return op.lro.Done()
  194. }
  195. // Name returns the name of the long-running operation.
  196. // The name is assigned by the server and is unique within the service from which the operation is created.
  197. func (op *AnnotateVideoOperation) Name() string {
  198. return op.lro.Name()
  199. }