選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

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