1// Copyright 2020 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 protoc-gen-go_gapic. DO NOT EDIT.
16
17package translate
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"cloud.google.com/go/longrunning"
27	lroauto "cloud.google.com/go/longrunning/autogen"
28	"github.com/golang/protobuf/proto"
29	gax "github.com/googleapis/gax-go/v2"
30	"google.golang.org/api/iterator"
31	"google.golang.org/api/option"
32	gtransport "google.golang.org/api/transport/grpc"
33	translatepb "google.golang.org/genproto/googleapis/cloud/translate/v3"
34	longrunningpb "google.golang.org/genproto/googleapis/longrunning"
35	"google.golang.org/grpc"
36	"google.golang.org/grpc/codes"
37	"google.golang.org/grpc/metadata"
38)
39
40var newTranslationClientHook clientHook
41
42// TranslationCallOptions contains the retry settings for each method of TranslationClient.
43type TranslationCallOptions struct {
44	TranslateText         []gax.CallOption
45	DetectLanguage        []gax.CallOption
46	GetSupportedLanguages []gax.CallOption
47	BatchTranslateText    []gax.CallOption
48	CreateGlossary        []gax.CallOption
49	ListGlossaries        []gax.CallOption
50	GetGlossary           []gax.CallOption
51	DeleteGlossary        []gax.CallOption
52}
53
54func defaultTranslationClientOptions() []option.ClientOption {
55	return []option.ClientOption{
56		option.WithEndpoint("translate.googleapis.com:443"),
57		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
58		option.WithScopes(DefaultAuthScopes()...),
59		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
60			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
61	}
62}
63
64func defaultTranslationCallOptions() *TranslationCallOptions {
65	return &TranslationCallOptions{
66		TranslateText:  []gax.CallOption{},
67		DetectLanguage: []gax.CallOption{},
68		GetSupportedLanguages: []gax.CallOption{
69			gax.WithRetry(func() gax.Retryer {
70				return gax.OnCodes([]codes.Code{
71					codes.DeadlineExceeded,
72					codes.Unavailable,
73				}, gax.Backoff{
74					Initial:    100 * time.Millisecond,
75					Max:        60000 * time.Millisecond,
76					Multiplier: 1.30,
77				})
78			}),
79		},
80		BatchTranslateText: []gax.CallOption{},
81		CreateGlossary:     []gax.CallOption{},
82		ListGlossaries: []gax.CallOption{
83			gax.WithRetry(func() gax.Retryer {
84				return gax.OnCodes([]codes.Code{
85					codes.DeadlineExceeded,
86					codes.Unavailable,
87				}, gax.Backoff{
88					Initial:    100 * time.Millisecond,
89					Max:        60000 * time.Millisecond,
90					Multiplier: 1.30,
91				})
92			}),
93		},
94		GetGlossary: []gax.CallOption{
95			gax.WithRetry(func() gax.Retryer {
96				return gax.OnCodes([]codes.Code{
97					codes.DeadlineExceeded,
98					codes.Unavailable,
99				}, gax.Backoff{
100					Initial:    100 * time.Millisecond,
101					Max:        60000 * time.Millisecond,
102					Multiplier: 1.30,
103				})
104			}),
105		},
106		DeleteGlossary: []gax.CallOption{
107			gax.WithRetry(func() gax.Retryer {
108				return gax.OnCodes([]codes.Code{
109					codes.DeadlineExceeded,
110					codes.Unavailable,
111				}, gax.Backoff{
112					Initial:    100 * time.Millisecond,
113					Max:        60000 * time.Millisecond,
114					Multiplier: 1.30,
115				})
116			}),
117		},
118	}
119}
120
121// TranslationClient is a client for interacting with Cloud Translation API.
122//
123// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
124type TranslationClient struct {
125	// Connection pool of gRPC connections to the service.
126	connPool gtransport.ConnPool
127
128	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
129	disableDeadlines bool
130
131	// The gRPC API client.
132	translationClient translatepb.TranslationServiceClient
133
134	// LROClient is used internally to handle longrunning operations.
135	// It is exposed so that its CallOptions can be modified if required.
136	// Users should not Close this client.
137	LROClient *lroauto.OperationsClient
138
139	// The call options for this service.
140	CallOptions *TranslationCallOptions
141
142	// The x-goog-* metadata to be sent with each request.
143	xGoogMetadata metadata.MD
144}
145
146// NewTranslationClient creates a new translation service client.
147//
148// Provides natural language translation operations.
149func NewTranslationClient(ctx context.Context, opts ...option.ClientOption) (*TranslationClient, error) {
150	clientOpts := defaultTranslationClientOptions()
151
152	if newTranslationClientHook != nil {
153		hookOpts, err := newTranslationClientHook(ctx, clientHookParams{})
154		if err != nil {
155			return nil, err
156		}
157		clientOpts = append(clientOpts, hookOpts...)
158	}
159
160	disableDeadlines, err := checkDisableDeadlines()
161	if err != nil {
162		return nil, err
163	}
164
165	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
166	if err != nil {
167		return nil, err
168	}
169	c := &TranslationClient{
170		connPool:         connPool,
171		disableDeadlines: disableDeadlines,
172		CallOptions:      defaultTranslationCallOptions(),
173
174		translationClient: translatepb.NewTranslationServiceClient(connPool),
175	}
176	c.setGoogleClientInfo()
177
178	c.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
179	if err != nil {
180		// This error "should not happen", since we are just reusing old connection pool
181		// and never actually need to dial.
182		// If this does happen, we could leak connp. However, we cannot close conn:
183		// If the user invoked the constructor with option.WithGRPCConn,
184		// we would close a connection that's still in use.
185		// TODO: investigate error conditions.
186		return nil, err
187	}
188	return c, nil
189}
190
191// Connection returns a connection to the API service.
192//
193// Deprecated.
194func (c *TranslationClient) Connection() *grpc.ClientConn {
195	return c.connPool.Conn()
196}
197
198// Close closes the connection to the API service. The user should invoke this when
199// the client is no longer required.
200func (c *TranslationClient) Close() error {
201	return c.connPool.Close()
202}
203
204// setGoogleClientInfo sets the name and version of the application in
205// the `x-goog-api-client` header passed on each request. Intended for
206// use by Google-written clients.
207func (c *TranslationClient) setGoogleClientInfo(keyval ...string) {
208	kv := append([]string{"gl-go", versionGo()}, keyval...)
209	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
210	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
211}
212
213// TranslateText translates input text and returns translated text.
214func (c *TranslationClient) TranslateText(ctx context.Context, req *translatepb.TranslateTextRequest, opts ...gax.CallOption) (*translatepb.TranslateTextResponse, error) {
215	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
216		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
217		defer cancel()
218		ctx = cctx
219	}
220	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
221	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
222	opts = append(c.CallOptions.TranslateText[0:len(c.CallOptions.TranslateText):len(c.CallOptions.TranslateText)], opts...)
223	var resp *translatepb.TranslateTextResponse
224	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
225		var err error
226		resp, err = c.translationClient.TranslateText(ctx, req, settings.GRPC...)
227		return err
228	}, opts...)
229	if err != nil {
230		return nil, err
231	}
232	return resp, nil
233}
234
235// DetectLanguage detects the language of text within a request.
236func (c *TranslationClient) DetectLanguage(ctx context.Context, req *translatepb.DetectLanguageRequest, opts ...gax.CallOption) (*translatepb.DetectLanguageResponse, error) {
237	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
238		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
239		defer cancel()
240		ctx = cctx
241	}
242	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
243	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
244	opts = append(c.CallOptions.DetectLanguage[0:len(c.CallOptions.DetectLanguage):len(c.CallOptions.DetectLanguage)], opts...)
245	var resp *translatepb.DetectLanguageResponse
246	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
247		var err error
248		resp, err = c.translationClient.DetectLanguage(ctx, req, settings.GRPC...)
249		return err
250	}, opts...)
251	if err != nil {
252		return nil, err
253	}
254	return resp, nil
255}
256
257// GetSupportedLanguages returns a list of supported languages for translation.
258func (c *TranslationClient) GetSupportedLanguages(ctx context.Context, req *translatepb.GetSupportedLanguagesRequest, opts ...gax.CallOption) (*translatepb.SupportedLanguages, error) {
259	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
260		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
261		defer cancel()
262		ctx = cctx
263	}
264	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
265	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
266	opts = append(c.CallOptions.GetSupportedLanguages[0:len(c.CallOptions.GetSupportedLanguages):len(c.CallOptions.GetSupportedLanguages)], opts...)
267	var resp *translatepb.SupportedLanguages
268	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
269		var err error
270		resp, err = c.translationClient.GetSupportedLanguages(ctx, req, settings.GRPC...)
271		return err
272	}, opts...)
273	if err != nil {
274		return nil, err
275	}
276	return resp, nil
277}
278
279// BatchTranslateText translates a large volume of text in asynchronous batch mode.
280// This function provides real-time output as the inputs are being processed.
281// If caller cancels a request, the partial results (for an input file, it’s
282// all or nothing) may still be available on the specified output location.
283//
284// This call returns immediately and you can
285// use google.longrunning.Operation.name (at http://google.longrunning.Operation.name) to poll the status of the call.
286func (c *TranslationClient) BatchTranslateText(ctx context.Context, req *translatepb.BatchTranslateTextRequest, opts ...gax.CallOption) (*BatchTranslateTextOperation, error) {
287	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
288		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
289		defer cancel()
290		ctx = cctx
291	}
292	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
293	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
294	opts = append(c.CallOptions.BatchTranslateText[0:len(c.CallOptions.BatchTranslateText):len(c.CallOptions.BatchTranslateText)], opts...)
295	var resp *longrunningpb.Operation
296	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
297		var err error
298		resp, err = c.translationClient.BatchTranslateText(ctx, req, settings.GRPC...)
299		return err
300	}, opts...)
301	if err != nil {
302		return nil, err
303	}
304	return &BatchTranslateTextOperation{
305		lro: longrunning.InternalNewOperation(c.LROClient, resp),
306	}, nil
307}
308
309// CreateGlossary creates a glossary and returns the long-running operation. Returns
310// NOT_FOUND, if the project doesn’t exist.
311func (c *TranslationClient) CreateGlossary(ctx context.Context, req *translatepb.CreateGlossaryRequest, opts ...gax.CallOption) (*CreateGlossaryOperation, error) {
312	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
313		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
314		defer cancel()
315		ctx = cctx
316	}
317	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
318	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
319	opts = append(c.CallOptions.CreateGlossary[0:len(c.CallOptions.CreateGlossary):len(c.CallOptions.CreateGlossary)], opts...)
320	var resp *longrunningpb.Operation
321	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
322		var err error
323		resp, err = c.translationClient.CreateGlossary(ctx, req, settings.GRPC...)
324		return err
325	}, opts...)
326	if err != nil {
327		return nil, err
328	}
329	return &CreateGlossaryOperation{
330		lro: longrunning.InternalNewOperation(c.LROClient, resp),
331	}, nil
332}
333
334// ListGlossaries lists glossaries in a project. Returns NOT_FOUND, if the project doesn’t
335// exist.
336func (c *TranslationClient) ListGlossaries(ctx context.Context, req *translatepb.ListGlossariesRequest, opts ...gax.CallOption) *GlossaryIterator {
337	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
338	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
339	opts = append(c.CallOptions.ListGlossaries[0:len(c.CallOptions.ListGlossaries):len(c.CallOptions.ListGlossaries)], opts...)
340	it := &GlossaryIterator{}
341	req = proto.Clone(req).(*translatepb.ListGlossariesRequest)
342	it.InternalFetch = func(pageSize int, pageToken string) ([]*translatepb.Glossary, string, error) {
343		var resp *translatepb.ListGlossariesResponse
344		req.PageToken = pageToken
345		if pageSize > math.MaxInt32 {
346			req.PageSize = math.MaxInt32
347		} else {
348			req.PageSize = int32(pageSize)
349		}
350		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
351			var err error
352			resp, err = c.translationClient.ListGlossaries(ctx, req, settings.GRPC...)
353			return err
354		}, opts...)
355		if err != nil {
356			return nil, "", err
357		}
358
359		it.Response = resp
360		return resp.GetGlossaries(), resp.GetNextPageToken(), nil
361	}
362	fetch := func(pageSize int, pageToken string) (string, error) {
363		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
364		if err != nil {
365			return "", err
366		}
367		it.items = append(it.items, items...)
368		return nextPageToken, nil
369	}
370	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
371	it.pageInfo.MaxSize = int(req.GetPageSize())
372	it.pageInfo.Token = req.GetPageToken()
373	return it
374}
375
376// GetGlossary gets a glossary. Returns NOT_FOUND, if the glossary doesn’t
377// exist.
378func (c *TranslationClient) GetGlossary(ctx context.Context, req *translatepb.GetGlossaryRequest, opts ...gax.CallOption) (*translatepb.Glossary, error) {
379	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
380		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
381		defer cancel()
382		ctx = cctx
383	}
384	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
385	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
386	opts = append(c.CallOptions.GetGlossary[0:len(c.CallOptions.GetGlossary):len(c.CallOptions.GetGlossary)], opts...)
387	var resp *translatepb.Glossary
388	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
389		var err error
390		resp, err = c.translationClient.GetGlossary(ctx, req, settings.GRPC...)
391		return err
392	}, opts...)
393	if err != nil {
394		return nil, err
395	}
396	return resp, nil
397}
398
399// DeleteGlossary deletes a glossary, or cancels glossary construction
400// if the glossary isn’t created yet.
401// Returns NOT_FOUND, if the glossary doesn’t exist.
402func (c *TranslationClient) DeleteGlossary(ctx context.Context, req *translatepb.DeleteGlossaryRequest, opts ...gax.CallOption) (*DeleteGlossaryOperation, error) {
403	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
404		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
405		defer cancel()
406		ctx = cctx
407	}
408	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
409	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
410	opts = append(c.CallOptions.DeleteGlossary[0:len(c.CallOptions.DeleteGlossary):len(c.CallOptions.DeleteGlossary)], opts...)
411	var resp *longrunningpb.Operation
412	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
413		var err error
414		resp, err = c.translationClient.DeleteGlossary(ctx, req, settings.GRPC...)
415		return err
416	}, opts...)
417	if err != nil {
418		return nil, err
419	}
420	return &DeleteGlossaryOperation{
421		lro: longrunning.InternalNewOperation(c.LROClient, resp),
422	}, nil
423}
424
425// BatchTranslateTextOperation manages a long-running operation from BatchTranslateText.
426type BatchTranslateTextOperation struct {
427	lro *longrunning.Operation
428}
429
430// BatchTranslateTextOperation returns a new BatchTranslateTextOperation from a given name.
431// The name must be that of a previously created BatchTranslateTextOperation, possibly from a different process.
432func (c *TranslationClient) BatchTranslateTextOperation(name string) *BatchTranslateTextOperation {
433	return &BatchTranslateTextOperation{
434		lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
435	}
436}
437
438// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
439//
440// See documentation of Poll for error-handling information.
441func (op *BatchTranslateTextOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*translatepb.BatchTranslateResponse, error) {
442	var resp translatepb.BatchTranslateResponse
443	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
444		return nil, err
445	}
446	return &resp, nil
447}
448
449// Poll fetches the latest state of the long-running operation.
450//
451// Poll also fetches the latest metadata, which can be retrieved by Metadata.
452//
453// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
454// the operation has completed with failure, the error is returned and op.Done will return true.
455// If Poll succeeds and the operation has completed successfully,
456// op.Done will return true, and the response of the operation is returned.
457// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
458func (op *BatchTranslateTextOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*translatepb.BatchTranslateResponse, error) {
459	var resp translatepb.BatchTranslateResponse
460	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
461		return nil, err
462	}
463	if !op.Done() {
464		return nil, nil
465	}
466	return &resp, nil
467}
468
469// Metadata returns metadata associated with the long-running operation.
470// Metadata itself does not contact the server, but Poll does.
471// To get the latest metadata, call this method after a successful call to Poll.
472// If the metadata is not available, the returned metadata and error are both nil.
473func (op *BatchTranslateTextOperation) Metadata() (*translatepb.BatchTranslateMetadata, error) {
474	var meta translatepb.BatchTranslateMetadata
475	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
476		return nil, nil
477	} else if err != nil {
478		return nil, err
479	}
480	return &meta, nil
481}
482
483// Done reports whether the long-running operation has completed.
484func (op *BatchTranslateTextOperation) Done() bool {
485	return op.lro.Done()
486}
487
488// Name returns the name of the long-running operation.
489// The name is assigned by the server and is unique within the service from which the operation is created.
490func (op *BatchTranslateTextOperation) Name() string {
491	return op.lro.Name()
492}
493
494// CreateGlossaryOperation manages a long-running operation from CreateGlossary.
495type CreateGlossaryOperation struct {
496	lro *longrunning.Operation
497}
498
499// CreateGlossaryOperation returns a new CreateGlossaryOperation from a given name.
500// The name must be that of a previously created CreateGlossaryOperation, possibly from a different process.
501func (c *TranslationClient) CreateGlossaryOperation(name string) *CreateGlossaryOperation {
502	return &CreateGlossaryOperation{
503		lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
504	}
505}
506
507// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
508//
509// See documentation of Poll for error-handling information.
510func (op *CreateGlossaryOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*translatepb.Glossary, error) {
511	var resp translatepb.Glossary
512	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
513		return nil, err
514	}
515	return &resp, nil
516}
517
518// Poll fetches the latest state of the long-running operation.
519//
520// Poll also fetches the latest metadata, which can be retrieved by Metadata.
521//
522// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
523// the operation has completed with failure, the error is returned and op.Done will return true.
524// If Poll succeeds and the operation has completed successfully,
525// op.Done will return true, and the response of the operation is returned.
526// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
527func (op *CreateGlossaryOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*translatepb.Glossary, error) {
528	var resp translatepb.Glossary
529	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
530		return nil, err
531	}
532	if !op.Done() {
533		return nil, nil
534	}
535	return &resp, nil
536}
537
538// Metadata returns metadata associated with the long-running operation.
539// Metadata itself does not contact the server, but Poll does.
540// To get the latest metadata, call this method after a successful call to Poll.
541// If the metadata is not available, the returned metadata and error are both nil.
542func (op *CreateGlossaryOperation) Metadata() (*translatepb.CreateGlossaryMetadata, error) {
543	var meta translatepb.CreateGlossaryMetadata
544	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
545		return nil, nil
546	} else if err != nil {
547		return nil, err
548	}
549	return &meta, nil
550}
551
552// Done reports whether the long-running operation has completed.
553func (op *CreateGlossaryOperation) Done() bool {
554	return op.lro.Done()
555}
556
557// Name returns the name of the long-running operation.
558// The name is assigned by the server and is unique within the service from which the operation is created.
559func (op *CreateGlossaryOperation) Name() string {
560	return op.lro.Name()
561}
562
563// DeleteGlossaryOperation manages a long-running operation from DeleteGlossary.
564type DeleteGlossaryOperation struct {
565	lro *longrunning.Operation
566}
567
568// DeleteGlossaryOperation returns a new DeleteGlossaryOperation from a given name.
569// The name must be that of a previously created DeleteGlossaryOperation, possibly from a different process.
570func (c *TranslationClient) DeleteGlossaryOperation(name string) *DeleteGlossaryOperation {
571	return &DeleteGlossaryOperation{
572		lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
573	}
574}
575
576// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
577//
578// See documentation of Poll for error-handling information.
579func (op *DeleteGlossaryOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*translatepb.DeleteGlossaryResponse, error) {
580	var resp translatepb.DeleteGlossaryResponse
581	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
582		return nil, err
583	}
584	return &resp, nil
585}
586
587// Poll fetches the latest state of the long-running operation.
588//
589// Poll also fetches the latest metadata, which can be retrieved by Metadata.
590//
591// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
592// the operation has completed with failure, the error is returned and op.Done will return true.
593// If Poll succeeds and the operation has completed successfully,
594// op.Done will return true, and the response of the operation is returned.
595// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
596func (op *DeleteGlossaryOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*translatepb.DeleteGlossaryResponse, error) {
597	var resp translatepb.DeleteGlossaryResponse
598	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
599		return nil, err
600	}
601	if !op.Done() {
602		return nil, nil
603	}
604	return &resp, nil
605}
606
607// Metadata returns metadata associated with the long-running operation.
608// Metadata itself does not contact the server, but Poll does.
609// To get the latest metadata, call this method after a successful call to Poll.
610// If the metadata is not available, the returned metadata and error are both nil.
611func (op *DeleteGlossaryOperation) Metadata() (*translatepb.DeleteGlossaryMetadata, error) {
612	var meta translatepb.DeleteGlossaryMetadata
613	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
614		return nil, nil
615	} else if err != nil {
616		return nil, err
617	}
618	return &meta, nil
619}
620
621// Done reports whether the long-running operation has completed.
622func (op *DeleteGlossaryOperation) Done() bool {
623	return op.lro.Done()
624}
625
626// Name returns the name of the long-running operation.
627// The name is assigned by the server and is unique within the service from which the operation is created.
628func (op *DeleteGlossaryOperation) Name() string {
629	return op.lro.Name()
630}
631
632// GlossaryIterator manages a stream of *translatepb.Glossary.
633type GlossaryIterator struct {
634	items    []*translatepb.Glossary
635	pageInfo *iterator.PageInfo
636	nextFunc func() error
637
638	// Response is the raw response for the current page.
639	// It must be cast to the RPC response type.
640	// Calling Next() or InternalFetch() updates this value.
641	Response interface{}
642
643	// InternalFetch is for use by the Google Cloud Libraries only.
644	// It is not part of the stable interface of this package.
645	//
646	// InternalFetch returns results from a single call to the underlying RPC.
647	// The number of results is no greater than pageSize.
648	// If there are no more results, nextPageToken is empty and err is nil.
649	InternalFetch func(pageSize int, pageToken string) (results []*translatepb.Glossary, nextPageToken string, err error)
650}
651
652// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
653func (it *GlossaryIterator) PageInfo() *iterator.PageInfo {
654	return it.pageInfo
655}
656
657// Next returns the next result. Its second return value is iterator.Done if there are no more
658// results. Once Next returns Done, all subsequent calls will return Done.
659func (it *GlossaryIterator) Next() (*translatepb.Glossary, error) {
660	var item *translatepb.Glossary
661	if err := it.nextFunc(); err != nil {
662		return item, err
663	}
664	item = it.items[0]
665	it.items = it.items[1:]
666	return item, nil
667}
668
669func (it *GlossaryIterator) bufLen() int {
670	return len(it.items)
671}
672
673func (it *GlossaryIterator) takeBuf() interface{} {
674	b := it.items
675	it.items = nil
676	return b
677}
678