Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

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