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