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.
 
 
 

602 lines
23 KiB

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