Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

485 řádky
19 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. // 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]) method, the
  98. // Dialogflow API analyzes the input and searches
  99. // for a matching intent. If no match is found, the Dialogflow API returns a
  100. // fallback intent (`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", version.Go()}, keyval...)
  162. kv = append(kv, "gapic", version.Repo, "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. it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Intent, string, error) {
  171. var resp *dialogflowpb.ListIntentsResponse
  172. req.PageToken = pageToken
  173. if pageSize > math.MaxInt32 {
  174. req.PageSize = math.MaxInt32
  175. } else {
  176. req.PageSize = int32(pageSize)
  177. }
  178. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  179. var err error
  180. resp, err = c.intentsClient.ListIntents(ctx, req, settings.GRPC...)
  181. return err
  182. }, opts...)
  183. if err != nil {
  184. return nil, "", err
  185. }
  186. return resp.Intents, resp.NextPageToken, nil
  187. }
  188. fetch := func(pageSize int, pageToken string) (string, error) {
  189. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  190. if err != nil {
  191. return "", err
  192. }
  193. it.items = append(it.items, items...)
  194. return nextPageToken, nil
  195. }
  196. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  197. return it
  198. }
  199. // GetIntent retrieves the specified intent.
  200. func (c *IntentsClient) GetIntent(ctx context.Context, req *dialogflowpb.GetIntentRequest, opts ...gax.CallOption) (*dialogflowpb.Intent, error) {
  201. ctx = insertMetadata(ctx, c.xGoogMetadata)
  202. opts = append(c.CallOptions.GetIntent[0:len(c.CallOptions.GetIntent):len(c.CallOptions.GetIntent)], opts...)
  203. var resp *dialogflowpb.Intent
  204. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  205. var err error
  206. resp, err = c.intentsClient.GetIntent(ctx, req, settings.GRPC...)
  207. return err
  208. }, opts...)
  209. if err != nil {
  210. return nil, err
  211. }
  212. return resp, nil
  213. }
  214. // CreateIntent creates an intent in the specified agent.
  215. func (c *IntentsClient) CreateIntent(ctx context.Context, req *dialogflowpb.CreateIntentRequest, opts ...gax.CallOption) (*dialogflowpb.Intent, error) {
  216. ctx = insertMetadata(ctx, c.xGoogMetadata)
  217. opts = append(c.CallOptions.CreateIntent[0:len(c.CallOptions.CreateIntent):len(c.CallOptions.CreateIntent)], opts...)
  218. var resp *dialogflowpb.Intent
  219. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  220. var err error
  221. resp, err = c.intentsClient.CreateIntent(ctx, req, settings.GRPC...)
  222. return err
  223. }, opts...)
  224. if err != nil {
  225. return nil, err
  226. }
  227. return resp, nil
  228. }
  229. // UpdateIntent updates the specified intent.
  230. func (c *IntentsClient) UpdateIntent(ctx context.Context, req *dialogflowpb.UpdateIntentRequest, opts ...gax.CallOption) (*dialogflowpb.Intent, error) {
  231. ctx = insertMetadata(ctx, c.xGoogMetadata)
  232. opts = append(c.CallOptions.UpdateIntent[0:len(c.CallOptions.UpdateIntent):len(c.CallOptions.UpdateIntent)], opts...)
  233. var resp *dialogflowpb.Intent
  234. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  235. var err error
  236. resp, err = c.intentsClient.UpdateIntent(ctx, req, settings.GRPC...)
  237. return err
  238. }, opts...)
  239. if err != nil {
  240. return nil, err
  241. }
  242. return resp, nil
  243. }
  244. // DeleteIntent deletes the specified intent.
  245. func (c *IntentsClient) DeleteIntent(ctx context.Context, req *dialogflowpb.DeleteIntentRequest, opts ...gax.CallOption) error {
  246. ctx = insertMetadata(ctx, c.xGoogMetadata)
  247. opts = append(c.CallOptions.DeleteIntent[0:len(c.CallOptions.DeleteIntent):len(c.CallOptions.DeleteIntent)], opts...)
  248. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  249. var err error
  250. _, err = c.intentsClient.DeleteIntent(ctx, req, settings.GRPC...)
  251. return err
  252. }, opts...)
  253. return err
  254. }
  255. // BatchUpdateIntents updates/Creates multiple intents in the specified agent.
  256. //
  257. // Operation <response: [BatchUpdateIntentsResponse][google.cloud.dialogflow.v2.BatchUpdateIntentsResponse]>
  258. func (c *IntentsClient) BatchUpdateIntents(ctx context.Context, req *dialogflowpb.BatchUpdateIntentsRequest, opts ...gax.CallOption) (*BatchUpdateIntentsOperation, error) {
  259. ctx = insertMetadata(ctx, c.xGoogMetadata)
  260. opts = append(c.CallOptions.BatchUpdateIntents[0:len(c.CallOptions.BatchUpdateIntents):len(c.CallOptions.BatchUpdateIntents)], opts...)
  261. var resp *longrunningpb.Operation
  262. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  263. var err error
  264. resp, err = c.intentsClient.BatchUpdateIntents(ctx, req, settings.GRPC...)
  265. return err
  266. }, opts...)
  267. if err != nil {
  268. return nil, err
  269. }
  270. return &BatchUpdateIntentsOperation{
  271. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  272. }, nil
  273. }
  274. // BatchDeleteIntents deletes intents in the specified agent.
  275. //
  276. // Operation <response: [google.protobuf.Empty][google.protobuf.Empty]>
  277. func (c *IntentsClient) BatchDeleteIntents(ctx context.Context, req *dialogflowpb.BatchDeleteIntentsRequest, opts ...gax.CallOption) (*BatchDeleteIntentsOperation, error) {
  278. ctx = insertMetadata(ctx, c.xGoogMetadata)
  279. opts = append(c.CallOptions.BatchDeleteIntents[0:len(c.CallOptions.BatchDeleteIntents):len(c.CallOptions.BatchDeleteIntents)], opts...)
  280. var resp *longrunningpb.Operation
  281. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  282. var err error
  283. resp, err = c.intentsClient.BatchDeleteIntents(ctx, req, settings.GRPC...)
  284. return err
  285. }, opts...)
  286. if err != nil {
  287. return nil, err
  288. }
  289. return &BatchDeleteIntentsOperation{
  290. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  291. }, nil
  292. }
  293. // IntentIterator manages a stream of *dialogflowpb.Intent.
  294. type IntentIterator struct {
  295. items []*dialogflowpb.Intent
  296. pageInfo *iterator.PageInfo
  297. nextFunc func() error
  298. // InternalFetch is for use by the Google Cloud Libraries only.
  299. // It is not part of the stable interface of this package.
  300. //
  301. // InternalFetch returns results from a single call to the underlying RPC.
  302. // The number of results is no greater than pageSize.
  303. // If there are no more results, nextPageToken is empty and err is nil.
  304. InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Intent, nextPageToken string, err error)
  305. }
  306. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  307. func (it *IntentIterator) PageInfo() *iterator.PageInfo {
  308. return it.pageInfo
  309. }
  310. // Next returns the next result. Its second return value is iterator.Done if there are no more
  311. // results. Once Next returns Done, all subsequent calls will return Done.
  312. func (it *IntentIterator) Next() (*dialogflowpb.Intent, error) {
  313. var item *dialogflowpb.Intent
  314. if err := it.nextFunc(); err != nil {
  315. return item, err
  316. }
  317. item = it.items[0]
  318. it.items = it.items[1:]
  319. return item, nil
  320. }
  321. func (it *IntentIterator) bufLen() int {
  322. return len(it.items)
  323. }
  324. func (it *IntentIterator) takeBuf() interface{} {
  325. b := it.items
  326. it.items = nil
  327. return b
  328. }
  329. // BatchDeleteIntentsOperation manages a long-running operation from BatchDeleteIntents.
  330. type BatchDeleteIntentsOperation struct {
  331. lro *longrunning.Operation
  332. }
  333. // BatchDeleteIntentsOperation returns a new BatchDeleteIntentsOperation from a given name.
  334. // The name must be that of a previously created BatchDeleteIntentsOperation, possibly from a different process.
  335. func (c *IntentsClient) BatchDeleteIntentsOperation(name string) *BatchDeleteIntentsOperation {
  336. return &BatchDeleteIntentsOperation{
  337. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  338. }
  339. }
  340. // Wait blocks until the long-running operation is completed, returning any error encountered.
  341. //
  342. // See documentation of Poll for error-handling information.
  343. func (op *BatchDeleteIntentsOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  344. return op.lro.WaitWithInterval(ctx, nil, 5000*time.Millisecond, opts...)
  345. }
  346. // Poll fetches the latest state of the long-running operation.
  347. //
  348. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  349. //
  350. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  351. // the operation has completed with failure, the error is returned and op.Done will return true.
  352. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  353. func (op *BatchDeleteIntentsOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  354. return op.lro.Poll(ctx, nil, opts...)
  355. }
  356. // Metadata returns metadata associated with the long-running operation.
  357. // Metadata itself does not contact the server, but Poll does.
  358. // To get the latest metadata, call this method after a successful call to Poll.
  359. // If the metadata is not available, the returned metadata and error are both nil.
  360. func (op *BatchDeleteIntentsOperation) Metadata() (*structpbpb.Struct, error) {
  361. var meta structpbpb.Struct
  362. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  363. return nil, nil
  364. } else if err != nil {
  365. return nil, err
  366. }
  367. return &meta, nil
  368. }
  369. // Done reports whether the long-running operation has completed.
  370. func (op *BatchDeleteIntentsOperation) Done() bool {
  371. return op.lro.Done()
  372. }
  373. // Name returns the name of the long-running operation.
  374. // The name is assigned by the server and is unique within the service from which the operation is created.
  375. func (op *BatchDeleteIntentsOperation) Name() string {
  376. return op.lro.Name()
  377. }
  378. // BatchUpdateIntentsOperation manages a long-running operation from BatchUpdateIntents.
  379. type BatchUpdateIntentsOperation struct {
  380. lro *longrunning.Operation
  381. }
  382. // BatchUpdateIntentsOperation returns a new BatchUpdateIntentsOperation from a given name.
  383. // The name must be that of a previously created BatchUpdateIntentsOperation, possibly from a different process.
  384. func (c *IntentsClient) BatchUpdateIntentsOperation(name string) *BatchUpdateIntentsOperation {
  385. return &BatchUpdateIntentsOperation{
  386. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  387. }
  388. }
  389. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  390. //
  391. // See documentation of Poll for error-handling information.
  392. func (op *BatchUpdateIntentsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*dialogflowpb.BatchUpdateIntentsResponse, error) {
  393. var resp dialogflowpb.BatchUpdateIntentsResponse
  394. if err := op.lro.WaitWithInterval(ctx, &resp, 5000*time.Millisecond, opts...); err != nil {
  395. return nil, err
  396. }
  397. return &resp, nil
  398. }
  399. // Poll fetches the latest state of the long-running operation.
  400. //
  401. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  402. //
  403. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  404. // the operation has completed with failure, the error is returned and op.Done will return true.
  405. // If Poll succeeds and the operation has completed successfully,
  406. // op.Done will return true, and the response of the operation is returned.
  407. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  408. func (op *BatchUpdateIntentsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*dialogflowpb.BatchUpdateIntentsResponse, error) {
  409. var resp dialogflowpb.BatchUpdateIntentsResponse
  410. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  411. return nil, err
  412. }
  413. if !op.Done() {
  414. return nil, nil
  415. }
  416. return &resp, nil
  417. }
  418. // Metadata returns metadata associated with the long-running operation.
  419. // Metadata itself does not contact the server, but Poll does.
  420. // To get the latest metadata, call this method after a successful call to Poll.
  421. // If the metadata is not available, the returned metadata and error are both nil.
  422. func (op *BatchUpdateIntentsOperation) Metadata() (*structpbpb.Struct, error) {
  423. var meta structpbpb.Struct
  424. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  425. return nil, nil
  426. } else if err != nil {
  427. return nil, err
  428. }
  429. return &meta, nil
  430. }
  431. // Done reports whether the long-running operation has completed.
  432. func (op *BatchUpdateIntentsOperation) Done() bool {
  433. return op.lro.Done()
  434. }
  435. // Name returns the name of the long-running operation.
  436. // The name is assigned by the server and is unique within the service from which the operation is created.
  437. func (op *BatchUpdateIntentsOperation) Name() string {
  438. return op.lro.Name()
  439. }