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