Skip to content

Commit

Permalink
fix(docs): add an operation polling example (#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz authored Sep 25, 2023
1 parent 26c6885 commit bd12eba
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,51 @@
// fmt.Println("Domain: %s", aErr.Domain())
// }
// }
//
// # Polling Operations
//
// If an API call returns an Operation, that means it could take some time to
// complete the work initiated by the API call. Applications that are interested
// in the end result of the operation they initiated should wait until the
// Operation.Done field indicates it is finished. To do this, use the service's
// Operation client, and a loop, like so:
//
// import (
// gax "github.com/googleapis/gax-go/v2"
// )
//
// // existing application code...
//
// // API call that returns an Operation.
// op, err := myApiClient.CalculateFoo().Do()
// if err != nil {
// // handle err
// }
//
// operationsService = myapi.NewOperationsService(myApiClient)
// pollingBackoff := gax.Backoff{
// Initial: time.Second,
// Max: time.Minute, // Max time between polling attempts.
// Multiplier: 2,
// }
// for {
// if op.Done {
// break
// }
// // not done, sleep with backoff, then poll again
// if err := gax.Sleep(ctx, pollingBackoff.Pause()); err != nil {
// // handle error
// }
// op, err := operationsService.Get(op.Name).Do()
// if err != nil {
// // handle error
// }
// }
//
// if op.Error != nil {
// // handle operation err
// }
//
// // Do something with the response
// fmt.Println(op.Response)
package api

0 comments on commit bd12eba

Please sign in to comment.