You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

609 lines
23 KiB

  1. // Copyright 2019 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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package dialogflow
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "cloud.google.com/go/longrunning"
  21. lroauto "cloud.google.com/go/longrunning/autogen"
  22. "github.com/golang/protobuf/proto"
  23. structpbpb "github.com/golang/protobuf/ptypes/struct"
  24. gax "github.com/googleapis/gax-go/v2"
  25. "google.golang.org/api/iterator"
  26. "google.golang.org/api/option"
  27. "google.golang.org/api/transport"
  28. dialogflowpb "google.golang.org/genproto/googleapis/cloud/dialogflow/v2"
  29. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  30. "google.golang.org/grpc"
  31. "google.golang.org/grpc/codes"
  32. "google.golang.org/grpc/metadata"
  33. )
  34. // AgentsCallOptions contains the retry settings for each method of AgentsClient.
  35. type AgentsCallOptions struct {
  36. GetAgent []gax.CallOption
  37. SearchAgents []gax.CallOption
  38. TrainAgent []gax.CallOption
  39. ExportAgent []gax.CallOption
  40. ImportAgent []gax.CallOption
  41. RestoreAgent []gax.CallOption
  42. }
  43. func defaultAgentsClientOptions() []option.ClientOption {
  44. return []option.ClientOption{
  45. option.WithEndpoint("dialogflow.googleapis.com:443"),
  46. option.WithScopes(DefaultAuthScopes()...),
  47. }
  48. }
  49. func defaultAgentsCallOptions() *AgentsCallOptions {
  50. retry := map[[2]string][]gax.CallOption{
  51. {"default", "idempotent"}: {
  52. gax.WithRetry(func() gax.Retryer {
  53. return gax.OnCodes([]codes.Code{
  54. codes.DeadlineExceeded,
  55. codes.Unavailable,
  56. }, gax.Backoff{
  57. Initial: 100 * time.Millisecond,
  58. Max: 60000 * time.Millisecond,
  59. Multiplier: 1.3,
  60. })
  61. }),
  62. },
  63. }
  64. return &AgentsCallOptions{
  65. GetAgent: retry[[2]string{"default", "idempotent"}],
  66. SearchAgents: retry[[2]string{"default", "idempotent"}],
  67. TrainAgent: retry[[2]string{"default", "idempotent"}],
  68. ExportAgent: retry[[2]string{"default", "idempotent"}],
  69. ImportAgent: retry[[2]string{"default", "non_idempotent"}],
  70. RestoreAgent: retry[[2]string{"default", "idempotent"}],
  71. }
  72. }
  73. // AgentsClient is a client for interacting with Dialogflow API.
  74. //
  75. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  76. type AgentsClient struct {
  77. // The connection to the service.
  78. conn *grpc.ClientConn
  79. // The gRPC API client.
  80. agentsClient dialogflowpb.AgentsClient
  81. // LROClient is used internally to handle longrunning operations.
  82. // It is exposed so that its CallOptions can be modified if required.
  83. // Users should not Close this client.
  84. LROClient *lroauto.OperationsClient
  85. // The call options for this service.
  86. CallOptions *AgentsCallOptions
  87. // The x-goog-* metadata to be sent with each request.
  88. xGoogMetadata metadata.MD
  89. }
  90. // NewAgentsClient creates a new agents client.
  91. //
  92. // Agents are best described as Natural Language Understanding (NLU) modules
  93. // that transform user requests into actionable data. You can include agents
  94. // in your app, product, or service to determine user intent and respond to the
  95. // user in a natural way.
  96. //
  97. // After you create an agent, you can add
  98. // [Intents][google.cloud.dialogflow.v2.Intents],
  99. // [Contexts][google.cloud.dialogflow.v2.Contexts], [Entity
  100. // Types][google.cloud.dialogflow.v2.EntityTypes],
  101. // [Webhooks][google.cloud.dialogflow.v2.WebhookRequest], and so on to manage
  102. // the flow of a conversation and match user input to predefined intents and
  103. // actions.
  104. //
  105. // You can create an agent using both Dialogflow Standard Edition and
  106. // Dialogflow Enterprise Edition. For details, see
  107. // Dialogflow Editions (at /dialogflow-enterprise/docs/editions).
  108. //
  109. // You can save your agent for backup or versioning by exporting the agent by
  110. // using the [ExportAgent][google.cloud.dialogflow.v2.Agents.ExportAgent]
  111. // method. You can import a saved agent by using the
  112. // [ImportAgent][google.cloud.dialogflow.v2.Agents.ImportAgent] method.
  113. //
  114. // Dialogflow provides several
  115. // prebuilt agents (at https://dialogflow.com/docs/prebuilt-agents) for common
  116. // conversation scenarios such as determining a date and time, converting
  117. // currency, and so on.
  118. //
  119. // For more information about agents, see the
  120. // Dialogflow documentation (at https://dialogflow.com/docs/agents).
  121. func NewAgentsClient(ctx context.Context, opts ...option.ClientOption) (*AgentsClient, error) {
  122. conn, err := transport.DialGRPC(ctx, append(defaultAgentsClientOptions(), opts...)...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. c := &AgentsClient{
  127. conn: conn,
  128. CallOptions: defaultAgentsCallOptions(),
  129. agentsClient: dialogflowpb.NewAgentsClient(conn),
  130. }
  131. c.setGoogleClientInfo()
  132. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  133. if err != nil {
  134. // This error "should not happen", since we are just reusing old connection
  135. // and never actually need to dial.
  136. // If this does happen, we could leak conn. However, we cannot close conn:
  137. // If the user invoked the function with option.WithGRPCConn,
  138. // we would close a connection that's still in use.
  139. // TODO(pongad): investigate error conditions.
  140. return nil, err
  141. }
  142. return c, nil
  143. }
  144. // Connection returns the client's connection to the API service.
  145. func (c *AgentsClient) Connection() *grpc.ClientConn {
  146. return c.conn
  147. }
  148. // Close closes the connection to the API service. The user should invoke this when
  149. // the client is no longer required.
  150. func (c *AgentsClient) Close() error {
  151. return c.conn.Close()
  152. }
  153. // setGoogleClientInfo sets the name and version of the application in
  154. // the `x-goog-api-client` header passed on each request. Intended for
  155. // use by Google-written clients.
  156. func (c *AgentsClient) setGoogleClientInfo(keyval ...string) {
  157. kv := append([]string{"gl-go", versionGo()}, keyval...)
  158. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  159. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  160. }
  161. // GetAgent retrieves the specified agent.
  162. func (c *AgentsClient) GetAgent(ctx context.Context, req *dialogflowpb.GetAgentRequest, opts ...gax.CallOption) (*dialogflowpb.Agent, error) {
  163. ctx = insertMetadata(ctx, c.xGoogMetadata)
  164. opts = append(c.CallOptions.GetAgent[0:len(c.CallOptions.GetAgent):len(c.CallOptions.GetAgent)], opts...)
  165. var resp *dialogflowpb.Agent
  166. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  167. var err error
  168. resp, err = c.agentsClient.GetAgent(ctx, req, settings.GRPC...)
  169. return err
  170. }, opts...)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return resp, nil
  175. }
  176. // SearchAgents returns the list of agents.
  177. //
  178. // Since there is at most one conversational agent per project, this method is
  179. // useful primarily for listing all agents across projects the caller has
  180. // access to. One can achieve that with a wildcard project collection id "-".
  181. // Refer to List
  182. // Sub-Collections (at https://cloud.google.com/apis/design/design_patterns#list_sub-collections).
  183. func (c *AgentsClient) SearchAgents(ctx context.Context, req *dialogflowpb.SearchAgentsRequest, opts ...gax.CallOption) *AgentIterator {
  184. ctx = insertMetadata(ctx, c.xGoogMetadata)
  185. opts = append(c.CallOptions.SearchAgents[0:len(c.CallOptions.SearchAgents):len(c.CallOptions.SearchAgents)], opts...)
  186. it := &AgentIterator{}
  187. req = proto.Clone(req).(*dialogflowpb.SearchAgentsRequest)
  188. it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Agent, string, error) {
  189. var resp *dialogflowpb.SearchAgentsResponse
  190. req.PageToken = pageToken
  191. if pageSize > math.MaxInt32 {
  192. req.PageSize = math.MaxInt32
  193. } else {
  194. req.PageSize = int32(pageSize)
  195. }
  196. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  197. var err error
  198. resp, err = c.agentsClient.SearchAgents(ctx, req, settings.GRPC...)
  199. return err
  200. }, opts...)
  201. if err != nil {
  202. return nil, "", err
  203. }
  204. return resp.Agents, resp.NextPageToken, nil
  205. }
  206. fetch := func(pageSize int, pageToken string) (string, error) {
  207. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  208. if err != nil {
  209. return "", err
  210. }
  211. it.items = append(it.items, items...)
  212. return nextPageToken, nil
  213. }
  214. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  215. it.pageInfo.MaxSize = int(req.PageSize)
  216. return it
  217. }
  218. // TrainAgent trains the specified agent.
  219. //
  220. // Operation <response: [google.protobuf.Empty][google.protobuf.Empty],
  221. // metadata: [google.protobuf.Struct][google.protobuf.Struct]>
  222. func (c *AgentsClient) TrainAgent(ctx context.Context, req *dialogflowpb.TrainAgentRequest, opts ...gax.CallOption) (*TrainAgentOperation, error) {
  223. ctx = insertMetadata(ctx, c.xGoogMetadata)
  224. opts = append(c.CallOptions.TrainAgent[0:len(c.CallOptions.TrainAgent):len(c.CallOptions.TrainAgent)], opts...)
  225. var resp *longrunningpb.Operation
  226. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  227. var err error
  228. resp, err = c.agentsClient.TrainAgent(ctx, req, settings.GRPC...)
  229. return err
  230. }, opts...)
  231. if err != nil {
  232. return nil, err
  233. }
  234. return &TrainAgentOperation{
  235. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  236. }, nil
  237. }
  238. // ExportAgent exports the specified agent to a ZIP file.
  239. //
  240. // Operation <response:
  241. // [ExportAgentResponse][google.cloud.dialogflow.v2.ExportAgentResponse],
  242. // metadata: [google.protobuf.Struct][google.protobuf.Struct]>
  243. func (c *AgentsClient) ExportAgent(ctx context.Context, req *dialogflowpb.ExportAgentRequest, opts ...gax.CallOption) (*ExportAgentOperation, error) {
  244. ctx = insertMetadata(ctx, c.xGoogMetadata)
  245. opts = append(c.CallOptions.ExportAgent[0:len(c.CallOptions.ExportAgent):len(c.CallOptions.ExportAgent)], opts...)
  246. var resp *longrunningpb.Operation
  247. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  248. var err error
  249. resp, err = c.agentsClient.ExportAgent(ctx, req, settings.GRPC...)
  250. return err
  251. }, opts...)
  252. if err != nil {
  253. return nil, err
  254. }
  255. return &ExportAgentOperation{
  256. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  257. }, nil
  258. }
  259. // ImportAgent imports the specified agent from a ZIP file.
  260. //
  261. // Uploads new intents and entity types without deleting the existing ones.
  262. // Intents and entity types with the same name are replaced with the new
  263. // versions from ImportAgentRequest.
  264. //
  265. // Operation <response: [google.protobuf.Empty][google.protobuf.Empty],
  266. // metadata: [google.protobuf.Struct][google.protobuf.Struct]>
  267. func (c *AgentsClient) ImportAgent(ctx context.Context, req *dialogflowpb.ImportAgentRequest, opts ...gax.CallOption) (*ImportAgentOperation, error) {
  268. ctx = insertMetadata(ctx, c.xGoogMetadata)
  269. opts = append(c.CallOptions.ImportAgent[0:len(c.CallOptions.ImportAgent):len(c.CallOptions.ImportAgent)], opts...)
  270. var resp *longrunningpb.Operation
  271. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  272. var err error
  273. resp, err = c.agentsClient.ImportAgent(ctx, req, settings.GRPC...)
  274. return err
  275. }, opts...)
  276. if err != nil {
  277. return nil, err
  278. }
  279. return &ImportAgentOperation{
  280. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  281. }, nil
  282. }
  283. // RestoreAgent restores the specified agent from a ZIP file.
  284. //
  285. // Replaces the current agent version with a new one. All the intents and
  286. // entity types in the older version are deleted.
  287. //
  288. // Operation <response: [google.protobuf.Empty][google.protobuf.Empty],
  289. // metadata: [google.protobuf.Struct][google.protobuf.Struct]>
  290. func (c *AgentsClient) RestoreAgent(ctx context.Context, req *dialogflowpb.RestoreAgentRequest, opts ...gax.CallOption) (*RestoreAgentOperation, error) {
  291. ctx = insertMetadata(ctx, c.xGoogMetadata)
  292. opts = append(c.CallOptions.RestoreAgent[0:len(c.CallOptions.RestoreAgent):len(c.CallOptions.RestoreAgent)], opts...)
  293. var resp *longrunningpb.Operation
  294. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  295. var err error
  296. resp, err = c.agentsClient.RestoreAgent(ctx, req, settings.GRPC...)
  297. return err
  298. }, opts...)
  299. if err != nil {
  300. return nil, err
  301. }
  302. return &RestoreAgentOperation{
  303. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  304. }, nil
  305. }
  306. // AgentIterator manages a stream of *dialogflowpb.Agent.
  307. type AgentIterator struct {
  308. items []*dialogflowpb.Agent
  309. pageInfo *iterator.PageInfo
  310. nextFunc func() error
  311. // InternalFetch is for use by the Google Cloud Libraries only.
  312. // It is not part of the stable interface of this package.
  313. //
  314. // InternalFetch returns results from a single call to the underlying RPC.
  315. // The number of results is no greater than pageSize.
  316. // If there are no more results, nextPageToken is empty and err is nil.
  317. InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Agent, nextPageToken string, err error)
  318. }
  319. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  320. func (it *AgentIterator) PageInfo() *iterator.PageInfo {
  321. return it.pageInfo
  322. }
  323. // Next returns the next result. Its second return value is iterator.Done if there are no more
  324. // results. Once Next returns Done, all subsequent calls will return Done.
  325. func (it *AgentIterator) Next() (*dialogflowpb.Agent, error) {
  326. var item *dialogflowpb.Agent
  327. if err := it.nextFunc(); err != nil {
  328. return item, err
  329. }
  330. item = it.items[0]
  331. it.items = it.items[1:]
  332. return item, nil
  333. }
  334. func (it *AgentIterator) bufLen() int {
  335. return len(it.items)
  336. }
  337. func (it *AgentIterator) takeBuf() interface{} {
  338. b := it.items
  339. it.items = nil
  340. return b
  341. }
  342. // ExportAgentOperation manages a long-running operation from ExportAgent.
  343. type ExportAgentOperation struct {
  344. lro *longrunning.Operation
  345. }
  346. // ExportAgentOperation returns a new ExportAgentOperation from a given name.
  347. // The name must be that of a previously created ExportAgentOperation, possibly from a different process.
  348. func (c *AgentsClient) ExportAgentOperation(name string) *ExportAgentOperation {
  349. return &ExportAgentOperation{
  350. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  351. }
  352. }
  353. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  354. //
  355. // See documentation of Poll for error-handling information.
  356. func (op *ExportAgentOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*dialogflowpb.ExportAgentResponse, error) {
  357. var resp dialogflowpb.ExportAgentResponse
  358. if err := op.lro.WaitWithInterval(ctx, &resp, 5000*time.Millisecond, opts...); err != nil {
  359. return nil, err
  360. }
  361. return &resp, nil
  362. }
  363. // Poll fetches the latest state of the long-running operation.
  364. //
  365. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  366. //
  367. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  368. // the operation has completed with failure, the error is returned and op.Done will return true.
  369. // If Poll succeeds and the operation has completed successfully,
  370. // op.Done will return true, and the response of the operation is returned.
  371. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  372. func (op *ExportAgentOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*dialogflowpb.ExportAgentResponse, error) {
  373. var resp dialogflowpb.ExportAgentResponse
  374. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  375. return nil, err
  376. }
  377. if !op.Done() {
  378. return nil, nil
  379. }
  380. return &resp, nil
  381. }
  382. // Metadata returns metadata associated with the long-running operation.
  383. // Metadata itself does not contact the server, but Poll does.
  384. // To get the latest metadata, call this method after a successful call to Poll.
  385. // If the metadata is not available, the returned metadata and error are both nil.
  386. func (op *ExportAgentOperation) Metadata() (*structpbpb.Struct, error) {
  387. var meta structpbpb.Struct
  388. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  389. return nil, nil
  390. } else if err != nil {
  391. return nil, err
  392. }
  393. return &meta, nil
  394. }
  395. // Done reports whether the long-running operation has completed.
  396. func (op *ExportAgentOperation) Done() bool {
  397. return op.lro.Done()
  398. }
  399. // Name returns the name of the long-running operation.
  400. // The name is assigned by the server and is unique within the service from which the operation is created.
  401. func (op *ExportAgentOperation) Name() string {
  402. return op.lro.Name()
  403. }
  404. // ImportAgentOperation manages a long-running operation from ImportAgent.
  405. type ImportAgentOperation struct {
  406. lro *longrunning.Operation
  407. }
  408. // ImportAgentOperation returns a new ImportAgentOperation from a given name.
  409. // The name must be that of a previously created ImportAgentOperation, possibly from a different process.
  410. func (c *AgentsClient) ImportAgentOperation(name string) *ImportAgentOperation {
  411. return &ImportAgentOperation{
  412. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  413. }
  414. }
  415. // Wait blocks until the long-running operation is completed, returning any error encountered.
  416. //
  417. // See documentation of Poll for error-handling information.
  418. func (op *ImportAgentOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  419. return op.lro.WaitWithInterval(ctx, nil, 5000*time.Millisecond, opts...)
  420. }
  421. // Poll fetches the latest state of the long-running operation.
  422. //
  423. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  424. //
  425. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  426. // the operation has completed with failure, the error is returned and op.Done will return true.
  427. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  428. func (op *ImportAgentOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  429. return op.lro.Poll(ctx, nil, opts...)
  430. }
  431. // Metadata returns metadata associated with the long-running operation.
  432. // Metadata itself does not contact the server, but Poll does.
  433. // To get the latest metadata, call this method after a successful call to Poll.
  434. // If the metadata is not available, the returned metadata and error are both nil.
  435. func (op *ImportAgentOperation) Metadata() (*structpbpb.Struct, error) {
  436. var meta structpbpb.Struct
  437. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  438. return nil, nil
  439. } else if err != nil {
  440. return nil, err
  441. }
  442. return &meta, nil
  443. }
  444. // Done reports whether the long-running operation has completed.
  445. func (op *ImportAgentOperation) Done() bool {
  446. return op.lro.Done()
  447. }
  448. // Name returns the name of the long-running operation.
  449. // The name is assigned by the server and is unique within the service from which the operation is created.
  450. func (op *ImportAgentOperation) Name() string {
  451. return op.lro.Name()
  452. }
  453. // RestoreAgentOperation manages a long-running operation from RestoreAgent.
  454. type RestoreAgentOperation struct {
  455. lro *longrunning.Operation
  456. }
  457. // RestoreAgentOperation returns a new RestoreAgentOperation from a given name.
  458. // The name must be that of a previously created RestoreAgentOperation, possibly from a different process.
  459. func (c *AgentsClient) RestoreAgentOperation(name string) *RestoreAgentOperation {
  460. return &RestoreAgentOperation{
  461. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  462. }
  463. }
  464. // Wait blocks until the long-running operation is completed, returning any error encountered.
  465. //
  466. // See documentation of Poll for error-handling information.
  467. func (op *RestoreAgentOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  468. return op.lro.WaitWithInterval(ctx, nil, 5000*time.Millisecond, opts...)
  469. }
  470. // Poll fetches the latest state of the long-running operation.
  471. //
  472. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  473. //
  474. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  475. // the operation has completed with failure, the error is returned and op.Done will return true.
  476. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  477. func (op *RestoreAgentOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  478. return op.lro.Poll(ctx, nil, opts...)
  479. }
  480. // Metadata returns metadata associated with the long-running operation.
  481. // Metadata itself does not contact the server, but Poll does.
  482. // To get the latest metadata, call this method after a successful call to Poll.
  483. // If the metadata is not available, the returned metadata and error are both nil.
  484. func (op *RestoreAgentOperation) Metadata() (*structpbpb.Struct, error) {
  485. var meta structpbpb.Struct
  486. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  487. return nil, nil
  488. } else if err != nil {
  489. return nil, err
  490. }
  491. return &meta, nil
  492. }
  493. // Done reports whether the long-running operation has completed.
  494. func (op *RestoreAgentOperation) Done() bool {
  495. return op.lro.Done()
  496. }
  497. // Name returns the name of the long-running operation.
  498. // The name is assigned by the server and is unique within the service from which the operation is created.
  499. func (op *RestoreAgentOperation) Name() string {
  500. return op.lro.Name()
  501. }
  502. // TrainAgentOperation manages a long-running operation from TrainAgent.
  503. type TrainAgentOperation struct {
  504. lro *longrunning.Operation
  505. }
  506. // TrainAgentOperation returns a new TrainAgentOperation from a given name.
  507. // The name must be that of a previously created TrainAgentOperation, possibly from a different process.
  508. func (c *AgentsClient) TrainAgentOperation(name string) *TrainAgentOperation {
  509. return &TrainAgentOperation{
  510. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  511. }
  512. }
  513. // Wait blocks until the long-running operation is completed, returning any error encountered.
  514. //
  515. // See documentation of Poll for error-handling information.
  516. func (op *TrainAgentOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  517. return op.lro.WaitWithInterval(ctx, nil, 5000*time.Millisecond, opts...)
  518. }
  519. // Poll fetches the latest state of the long-running operation.
  520. //
  521. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  522. //
  523. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  524. // the operation has completed with failure, the error is returned and op.Done will return true.
  525. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  526. func (op *TrainAgentOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  527. return op.lro.Poll(ctx, nil, opts...)
  528. }
  529. // Metadata returns metadata associated with the long-running operation.
  530. // Metadata itself does not contact the server, but Poll does.
  531. // To get the latest metadata, call this method after a successful call to Poll.
  532. // If the metadata is not available, the returned metadata and error are both nil.
  533. func (op *TrainAgentOperation) Metadata() (*structpbpb.Struct, error) {
  534. var meta structpbpb.Struct
  535. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  536. return nil, nil
  537. } else if err != nil {
  538. return nil, err
  539. }
  540. return &meta, nil
  541. }
  542. // Done reports whether the long-running operation has completed.
  543. func (op *TrainAgentOperation) Done() bool {
  544. return op.lro.Done()
  545. }
  546. // Name returns the name of the long-running operation.
  547. // The name is assigned by the server and is unique within the service from which the operation is created.
  548. func (op *TrainAgentOperation) Name() string {
  549. return op.lro.Name()
  550. }