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