Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): add an operation polling example #2186

Merged
merged 5 commits into from Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions doc.go
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:
//
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
// 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