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.
 
 
 

488 lines
19 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. // IntentsCallOptions contains the retry settings for each method of IntentsClient.
  35. type IntentsCallOptions struct {
  36. ListIntents []gax.CallOption
  37. GetIntent []gax.CallOption
  38. CreateIntent []gax.CallOption
  39. UpdateIntent []gax.CallOption
  40. DeleteIntent []gax.CallOption
  41. BatchUpdateIntents []gax.CallOption
  42. BatchDeleteIntents []gax.CallOption
  43. }
  44. func defaultIntentsClientOptions() []option.ClientOption {
  45. return []option.ClientOption{
  46. option.WithEndpoint("dialogflow.googleapis.com:443"),
  47. option.WithScopes(DefaultAuthScopes()...),
  48. }
  49. }
  50. func defaultIntentsCallOptions() *IntentsCallOptions {
  51. retry := map[[2]string][]gax.CallOption{
  52. {"default", "idempotent"}: {
  53. gax.WithRetry(func() gax.Retryer {
  54. return gax.OnCodes([]codes.Code{
  55. codes.DeadlineExceeded,
  56. codes.Unavailable,
  57. }, gax.Backoff{
  58. Initial: 100 * time.Millisecond,
  59. Max: 60000 * time.Millisecond,
  60. Multiplier: 1.3,
  61. })
  62. }),
  63. },
  64. }
  65. return &IntentsCallOptions{
  66. ListIntents: retry[[2]string{"default", "idempotent"}],
  67. GetIntent: retry[[2]string{"default", "idempotent"}],
  68. CreateIntent: retry[[2]string{"default", "non_idempotent"}],
  69. UpdateIntent: retry[[2]string{"default", "non_idempotent"}],
  70. DeleteIntent: retry[[2]string{"default", "idempotent"}],
  71. BatchUpdateIntents: retry[[2]string{"default", "non_idempotent"}],
  72. BatchDeleteIntents: retry[[2]string{"default", "idempotent"}],
  73. }
  74. }
  75. // IntentsClient is a client for interacting with Dialogflow API.
  76. //
  77. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  78. type IntentsClient struct {
  79. // The connection to the service.
  80. conn *grpc.ClientConn
  81. // The gRPC API client.
  82. intentsClient dialogflowpb.IntentsClient
  83. // LROClient is used internally to handle longrunning operations.
  84. // It is exposed so that its CallOptions can be modified if required.
  85. // Users should not Close this client.
  86. LROClient *lroauto.OperationsClient
  87. // The call options for this service.
  88. CallOptions *IntentsCallOptions
  89. // The x-goog-* metadata to be sent with each request.
  90. xGoogMetadata metadata.MD
  91. }
  92. // NewIntentsClient creates a new intents client.
  93. //
  94. // An intent represents a mapping between input from a user and an action to
  95. // be taken by your application. When you pass user input to the
  96. // [DetectIntent][google.cloud.dialogflow.v2.Sessions.DetectIntent] (or
  97. // [StreamingDetectIntent][google.cloud.dialogflow.v2.Sessions.StreamingDetectIntent])
  98. // method, the Dialogflow API analyzes the input and searches for a matching
  99. // intent. If no match is found, the Dialogflow API returns a fallback intent
  100. // (`is_fallback` = true).
  101. //
  102. // You can provide additional information for the Dialogflow API to use to
  103. // match user input to an intent by adding the following to your intent.
  104. //
  105. // * **Contexts** - provide additional context for intent analysis. For
  106. // example, if an intent is related to an object in your application that
  107. // plays music, you can provide a context to determine when to match the
  108. // intent if the user input is “turn it off”. You can include a context
  109. // that matches the intent when there is previous user input of
  110. // "play music", and not when there is previous user input of
  111. // "turn on the light".
  112. //
  113. // * **Events** - allow for matching an intent by using an event name
  114. // instead of user input. Your application can provide an event name and
  115. // related parameters to the Dialogflow API to match an intent. For
  116. // example, when your application starts, you can send a welcome event
  117. // with a user name parameter to the Dialogflow API to match an intent with
  118. // a personalized welcome message for the user.
  119. //
  120. // * **Training phrases** - provide examples of user input to train the
  121. // Dialogflow API agent to better match intents.
  122. //
  123. // For more information about intents, see the
  124. // [Dialogflow documentation](https://dialogflow.com/docs/intents).
  125. func NewIntentsClient(ctx context.Context, opts ...option.ClientOption) (*IntentsClient, error) {
  126. conn, err := transport.DialGRPC(ctx, append(defaultIntentsClientOptions(), opts...)...)
  127. if err != nil {
  128. return nil, err
  129. }
  130. c := &IntentsClient{
  131. conn: conn,
  132. CallOptions: defaultIntentsCallOptions(),
  133. intentsClient: dialogflowpb.NewIntentsClient(conn),
  134. }
  135. c.setGoogleClientInfo()
  136. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  137. if err != nil {
  138. // This error "should not happen", since we are just reusing old connection
  139. // and never actually need to dial.
  140. // If this does happen, we could leak conn. However, we cannot close conn:
  141. // If the user invoked the function with option.WithGRPCConn,
  142. // we would close a connection that's still in use.
  143. // TODO(pongad): investigate error conditions.
  144. return nil, err
  145. }
  146. return c, nil
  147. }
  148. // Connection returns the client's connection to the API service.
  149. func (c *IntentsClient) Connection() *grpc.ClientConn {
  150. return c.conn
  151. }
  152. // Close closes the connection to the API service. The user should invoke this when
  153. // the client is no longer required.
  154. func (c *IntentsClient) Close() error {
  155. return c.conn.Close()
  156. }
  157. // setGoogleClientInfo sets the name and version of the application in
  158. // the `x-goog-api-client` header passed on each request. Intended for
  159. // use by Google-written clients.
  160. func (c *IntentsClient) setGoogleClientInfo(keyval ...string) {
  161. kv := append([]string{"gl-go", versionGo()}, keyval...)
  162. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  163. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  164. }
  165. // ListIntents returns the list of all intents in the specified agent.
  166. func (c *IntentsClient) ListIntents(ctx context.Context, req *dialogflowpb.ListIntentsRequest, opts ...gax.CallOption) *IntentIterator {
  167. ctx = insertMetadata(ctx, c.xGoogMetadata)
  168. opts = append(c.CallOptions.ListIntents[0:len(c.CallOptions.ListIntents):len(c.CallOptions.ListIntents)], opts...)
  169. it := &IntentIterator{}
  170. req = proto.Clone(req).(*dialogflowpb.ListIntentsRequest)
  171. it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Intent, string, error) {
  172. var resp *dialogflowpb.ListIntentsResponse
  173. req.PageToken = pageToken
  174. if pageSize > math.MaxInt32 {
  175. req.PageSize = math.MaxInt32
  176. } else {
  177. req.PageSize = int32(pageSize)
  178. }
  179. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  180. var err error
  181. resp, err = c.intentsClient.ListIntents(ctx, req, settings.GRPC...)
  182. return err
  183. }, opts...)
  184. if err != nil {
  185. return nil, "", err
  186. }
  187. return resp.Intents, resp.NextPageToken, nil
  188. }
  189. fetch := func(pageSize int, pageToken string) (string, error) {
  190. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  191. if err != nil {
  192. return "", err
  193. }
  194. it.items = append(it.items, items...)
  195. return nextPageToken, nil
  196. }
  197. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  198. it.pageInfo.MaxSize = int(req.PageSize)
  199. return it
  200. }
  201. // GetIntent retrieves the specified intent.
  202. func (c *IntentsClient) GetIntent(ctx context.Context, req *dialogflowpb.GetIntentRequest, opts ...gax.CallOption) (*dialogflowpb.Intent, error) {
  203. ctx = insertMetadata(ctx, c.xGoogMetadata)
  204. opts = append(c.CallOptions.GetIntent[0:len(c.CallOptions.GetIntent):len(c.CallOptions.GetIntent)], opts...)
  205. var resp *dialogflowpb.Intent
  206. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  207. var err error
  208. resp, err = c.intentsClient.GetIntent(ctx, req, settings.GRPC...)
  209. return err
  210. }, opts...)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return resp, nil
  215. }
  216. // CreateIntent creates an intent in the specified agent.
  217. func (c *IntentsClient) CreateIntent(ctx context.Context, req *dialogflowpb.CreateIntentRequest, opts ...gax.CallOption) (*dialogflowpb.Intent, error) {
  218. ctx = insertMetadata(ctx, c.xGoogMetadata)
  219. opts = append(c.CallOptions.CreateIntent[0:len(c.CallOptions.CreateIntent):len(c.CallOptions.CreateIntent)], opts...)
  220. var resp *dialogflowpb.Intent
  221. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  222. var err error
  223. resp, err = c.intentsClient.CreateIntent(ctx, req, settings.GRPC...)
  224. return err
  225. }, opts...)
  226. if err != nil {
  227. return nil, err
  228. }
  229. return resp, nil
  230. }
  231. // UpdateIntent updates the specified intent.
  232. func (c *IntentsClient) UpdateIntent(ctx context.Context, req *dialogflowpb.UpdateIntentRequest, opts ...gax.CallOption) (*dialogflowpb.Intent, error) {
  233. ctx = insertMetadata(ctx, c.xGoogMetadata)
  234. opts = append(c.CallOptions.UpdateIntent[0:len(c.CallOptions.UpdateIntent):len(c.CallOptions.UpdateIntent)], opts...)
  235. var resp *dialogflowpb.Intent
  236. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  237. var err error
  238. resp, err = c.intentsClient.UpdateIntent(ctx, req, settings.GRPC...)
  239. return err
  240. }, opts...)
  241. if err != nil {
  242. return nil, err
  243. }
  244. return resp, nil
  245. }
  246. // DeleteIntent deletes the specified intent.
  247. func (c *IntentsClient) DeleteIntent(ctx context.Context, req *dialogflowpb.DeleteIntentRequest, opts ...gax.CallOption) error {
  248. ctx = insertMetadata(ctx, c.xGoogMetadata)
  249. opts = append(c.CallOptions.DeleteIntent[0:len(c.CallOptions.DeleteIntent):len(c.CallOptions.DeleteIntent)], opts...)
  250. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  251. var err error
  252. _, err = c.intentsClient.DeleteIntent(ctx, req, settings.GRPC...)
  253. return err
  254. }, opts...)
  255. return err
  256. }
  257. // BatchUpdateIntents updates/Creates multiple intents in the specified agent.
  258. //
  259. // Operation <response:
  260. // [BatchUpdateIntentsResponse][google.cloud.dialogflow.v2.BatchUpdateIntentsResponse]>
  261. func (c *IntentsClient) BatchUpdateIntents(ctx context.Context, req *dialogflowpb.BatchUpdateIntentsRequest, opts ...gax.CallOption) (*BatchUpdateIntentsOperation, error) {
  262. ctx = insertMetadata(ctx, c.xGoogMetadata)
  263. opts = append(c.CallOptions.BatchUpdateIntents[0:len(c.CallOptions.BatchUpdateIntents):len(c.CallOptions.BatchUpdateIntents)], opts...)
  264. var resp *longrunningpb.Operation
  265. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  266. var err error
  267. resp, err = c.intentsClient.BatchUpdateIntents(ctx, req, settings.GRPC...)
  268. return err
  269. }, opts...)
  270. if err != nil {
  271. return nil, err
  272. }
  273. return &BatchUpdateIntentsOperation{
  274. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  275. }, nil
  276. }
  277. // BatchDeleteIntents deletes intents in the specified agent.
  278. //
  279. // Operation <response: [google.protobuf.Empty][google.protobuf.Empty]>
  280. func (c *IntentsClient) BatchDeleteIntents(ctx context.Context, req *dialogflowpb.BatchDeleteIntentsRequest, opts ...gax.CallOption) (*BatchDeleteIntentsOperation, error) {
  281. ctx = insertMetadata(ctx, c.xGoogMetadata)
  282. opts = append(c.CallOptions.BatchDeleteIntents[0:len(c.CallOptions.BatchDeleteIntents):len(c.CallOptions.BatchDeleteIntents)], opts...)
  283. var resp *longrunningpb.Operation
  284. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  285. var err error
  286. resp, err = c.intentsClient.BatchDeleteIntents(ctx, req, settings.GRPC...)
  287. return err
  288. }, opts...)
  289. if err != nil {
  290. return nil, err
  291. }
  292. return &BatchDeleteIntentsOperation{
  293. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  294. }, nil
  295. }
  296. // IntentIterator manages a stream of *dialogflowpb.Intent.
  297. type IntentIterator struct {
  298. items []*dialogflowpb.Intent
  299. pageInfo *iterator.PageInfo
  300. nextFunc func() error
  301. // InternalFetch is for use by the Google Cloud Libraries only.
  302. // It is not part of the stable interface of this package.
  303. //
  304. // InternalFetch returns results from a single call to the underlying RPC.
  305. // The number of results is no greater than pageSize.
  306. // If there are no more results, nextPageToken is empty and err is nil.
  307. InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Intent, nextPageToken string, err error)
  308. }
  309. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  310. func (it *IntentIterator) PageInfo() *iterator.PageInfo {
  311. return it.pageInfo
  312. }
  313. // Next returns the next result. Its second return value is iterator.Done if there are no more
  314. // results. Once Next returns Done, all subsequent calls will return Done.
  315. func (it *IntentIterator) Next() (*dialogflowpb.Intent, error) {
  316. var item *dialogflowpb.Intent
  317. if err := it.nextFunc(); err != nil {
  318. return item, err
  319. }
  320. item = it.items[0]
  321. it.items = it.items[1:]
  322. return item, nil
  323. }
  324. func (it *IntentIterator) bufLen() int {
  325. return len(it.items)
  326. }
  327. func (it *IntentIterator) takeBuf() interface{} {
  328. b := it.items
  329. it.items = nil
  330. return b
  331. }
  332. // BatchDeleteIntentsOperation manages a long-running operation from BatchDeleteIntents.
  333. type BatchDeleteIntentsOperation struct {
  334. lro *longrunning.Operation
  335. }
  336. // BatchDeleteIntentsOperation returns a new BatchDeleteIntentsOperation from a given name.
  337. // The name must be that of a previously created BatchDeleteIntentsOperation, possibly from a different process.
  338. func (c *IntentsClient) BatchDeleteIntentsOperation(name string) *BatchDeleteIntentsOperation {
  339. return &BatchDeleteIntentsOperation{
  340. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  341. }
  342. }
  343. // Wait blocks until the long-running operation is completed, returning any error encountered.
  344. //
  345. // See documentation of Poll for error-handling information.
  346. func (op *BatchDeleteIntentsOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  347. return op.lro.WaitWithInterval(ctx, nil, 5000*time.Millisecond, opts...)
  348. }
  349. // Poll fetches the latest state of the long-running operation.
  350. //
  351. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  352. //
  353. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  354. // the operation has completed with failure, the error is returned and op.Done will return true.
  355. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  356. func (op *BatchDeleteIntentsOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  357. return op.lro.Poll(ctx, nil, opts...)
  358. }
  359. // Metadata returns metadata associated with the long-running operation.
  360. // Metadata itself does not contact the server, but Poll does.
  361. // To get the latest metadata, call this method after a successful call to Poll.
  362. // If the metadata is not available, the returned metadata and error are both nil.
  363. func (op *BatchDeleteIntentsOperation) Metadata() (*structpbpb.Struct, error) {
  364. var meta structpbpb.Struct
  365. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  366. return nil, nil
  367. } else if err != nil {
  368. return nil, err
  369. }
  370. return &meta, nil
  371. }
  372. // Done reports whether the long-running operation has completed.
  373. func (op *BatchDeleteIntentsOperation) Done() bool {
  374. return op.lro.Done()
  375. }
  376. // Name returns the name of the long-running operation.
  377. // The name is assigned by the server and is unique within the service from which the operation is created.
  378. func (op *BatchDeleteIntentsOperation) Name() string {
  379. return op.lro.Name()
  380. }
  381. // BatchUpdateIntentsOperation manages a long-running operation from BatchUpdateIntents.
  382. type BatchUpdateIntentsOperation struct {
  383. lro *longrunning.Operation
  384. }
  385. // BatchUpdateIntentsOperation returns a new BatchUpdateIntentsOperation from a given name.
  386. // The name must be that of a previously created BatchUpdateIntentsOperation, possibly from a different process.
  387. func (c *IntentsClient) BatchUpdateIntentsOperation(name string) *BatchUpdateIntentsOperation {
  388. return &BatchUpdateIntentsOperation{
  389. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  390. }
  391. }
  392. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  393. //
  394. // See documentation of Poll for error-handling information.
  395. func (op *BatchUpdateIntentsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*dialogflowpb.BatchUpdateIntentsResponse, error) {
  396. var resp dialogflowpb.BatchUpdateIntentsResponse
  397. if err := op.lro.WaitWithInterval(ctx, &resp, 5000*time.Millisecond, opts...); err != nil {
  398. return nil, err
  399. }
  400. return &resp, nil
  401. }
  402. // Poll fetches the latest state of the long-running operation.
  403. //
  404. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  405. //
  406. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  407. // the operation has completed with failure, the error is returned and op.Done will return true.
  408. // If Poll succeeds and the operation has completed successfully,
  409. // op.Done will return true, and the response of the operation is returned.
  410. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  411. func (op *BatchUpdateIntentsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*dialogflowpb.BatchUpdateIntentsResponse, error) {
  412. var resp dialogflowpb.BatchUpdateIntentsResponse
  413. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  414. return nil, err
  415. }
  416. if !op.Done() {
  417. return nil, nil
  418. }
  419. return &resp, nil
  420. }
  421. // Metadata returns metadata associated with the long-running operation.
  422. // Metadata itself does not contact the server, but Poll does.
  423. // To get the latest metadata, call this method after a successful call to Poll.
  424. // If the metadata is not available, the returned metadata and error are both nil.
  425. func (op *BatchUpdateIntentsOperation) Metadata() (*structpbpb.Struct, error) {
  426. var meta structpbpb.Struct
  427. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  428. return nil, nil
  429. } else if err != nil {
  430. return nil, err
  431. }
  432. return &meta, nil
  433. }
  434. // Done reports whether the long-running operation has completed.
  435. func (op *BatchUpdateIntentsOperation) Done() bool {
  436. return op.lro.Done()
  437. }
  438. // Name returns the name of the long-running operation.
  439. // The name is assigned by the server and is unique within the service from which the operation is created.
  440. func (op *BatchUpdateIntentsOperation) Name() string {
  441. return op.lro.Name()
  442. }