Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

296 lignes
11 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. gax "github.com/googleapis/gax-go"
  21. "golang.org/x/net/context"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. dialogflowpb "google.golang.org/genproto/googleapis/cloud/dialogflow/v2"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // ContextsCallOptions contains the retry settings for each method of ContextsClient.
  31. type ContextsCallOptions struct {
  32. ListContexts []gax.CallOption
  33. GetContext []gax.CallOption
  34. CreateContext []gax.CallOption
  35. UpdateContext []gax.CallOption
  36. DeleteContext []gax.CallOption
  37. DeleteAllContexts []gax.CallOption
  38. }
  39. func defaultContextsClientOptions() []option.ClientOption {
  40. return []option.ClientOption{
  41. option.WithEndpoint("dialogflow.googleapis.com:443"),
  42. option.WithScopes(DefaultAuthScopes()...),
  43. }
  44. }
  45. func defaultContextsCallOptions() *ContextsCallOptions {
  46. retry := map[[2]string][]gax.CallOption{
  47. {"default", "idempotent"}: {
  48. gax.WithRetry(func() gax.Retryer {
  49. return gax.OnCodes([]codes.Code{
  50. codes.DeadlineExceeded,
  51. codes.Unavailable,
  52. }, gax.Backoff{
  53. Initial: 100 * time.Millisecond,
  54. Max: 60000 * time.Millisecond,
  55. Multiplier: 1.3,
  56. })
  57. }),
  58. },
  59. }
  60. return &ContextsCallOptions{
  61. ListContexts: retry[[2]string{"default", "idempotent"}],
  62. GetContext: retry[[2]string{"default", "idempotent"}],
  63. CreateContext: retry[[2]string{"default", "non_idempotent"}],
  64. UpdateContext: retry[[2]string{"default", "non_idempotent"}],
  65. DeleteContext: retry[[2]string{"default", "idempotent"}],
  66. DeleteAllContexts: retry[[2]string{"default", "idempotent"}],
  67. }
  68. }
  69. // ContextsClient is a client for interacting with Dialogflow API.
  70. //
  71. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  72. type ContextsClient struct {
  73. // The connection to the service.
  74. conn *grpc.ClientConn
  75. // The gRPC API client.
  76. contextsClient dialogflowpb.ContextsClient
  77. // The call options for this service.
  78. CallOptions *ContextsCallOptions
  79. // The x-goog-* metadata to be sent with each request.
  80. xGoogMetadata metadata.MD
  81. }
  82. // NewContextsClient creates a new contexts client.
  83. //
  84. // A context represents additional information included with user input or with
  85. // an intent returned by the Dialogflow API. Contexts are helpful for
  86. // differentiating user input which may be vague or have a different meaning
  87. // depending on additional details from your application such as user setting
  88. // and preferences, previous user input, where the user is in your application,
  89. // geographic location, and so on.
  90. //
  91. // You can include contexts as input parameters of a
  92. // [DetectIntent][google.cloud.dialogflow.v2.Sessions.DetectIntent] (or
  93. // [StreamingDetectIntent][google.cloud.dialogflow.v2.Sessions.StreamingDetectIntent]) request,
  94. // or as output contexts included in the returned intent.
  95. // Contexts expire when an intent is matched, after the number of DetectIntent
  96. // requests specified by the lifespan_count parameter, or after 10 minutes
  97. // if no intents are matched for a DetectIntent request.
  98. //
  99. // For more information about contexts, see the
  100. // Dialogflow documentation (at https://dialogflow.com/docs/contexts).
  101. func NewContextsClient(ctx context.Context, opts ...option.ClientOption) (*ContextsClient, error) {
  102. conn, err := transport.DialGRPC(ctx, append(defaultContextsClientOptions(), opts...)...)
  103. if err != nil {
  104. return nil, err
  105. }
  106. c := &ContextsClient{
  107. conn: conn,
  108. CallOptions: defaultContextsCallOptions(),
  109. contextsClient: dialogflowpb.NewContextsClient(conn),
  110. }
  111. c.setGoogleClientInfo()
  112. return c, nil
  113. }
  114. // Connection returns the client's connection to the API service.
  115. func (c *ContextsClient) Connection() *grpc.ClientConn {
  116. return c.conn
  117. }
  118. // Close closes the connection to the API service. The user should invoke this when
  119. // the client is no longer required.
  120. func (c *ContextsClient) Close() error {
  121. return c.conn.Close()
  122. }
  123. // setGoogleClientInfo sets the name and version of the application in
  124. // the `x-goog-api-client` header passed on each request. Intended for
  125. // use by Google-written clients.
  126. func (c *ContextsClient) setGoogleClientInfo(keyval ...string) {
  127. kv := append([]string{"gl-go", version.Go()}, keyval...)
  128. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  129. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  130. }
  131. // ListContexts returns the list of all contexts in the specified session.
  132. func (c *ContextsClient) ListContexts(ctx context.Context, req *dialogflowpb.ListContextsRequest, opts ...gax.CallOption) *ContextIterator {
  133. ctx = insertMetadata(ctx, c.xGoogMetadata)
  134. opts = append(c.CallOptions.ListContexts[0:len(c.CallOptions.ListContexts):len(c.CallOptions.ListContexts)], opts...)
  135. it := &ContextIterator{}
  136. it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Context, string, error) {
  137. var resp *dialogflowpb.ListContextsResponse
  138. req.PageToken = pageToken
  139. if pageSize > math.MaxInt32 {
  140. req.PageSize = math.MaxInt32
  141. } else {
  142. req.PageSize = int32(pageSize)
  143. }
  144. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  145. var err error
  146. resp, err = c.contextsClient.ListContexts(ctx, req, settings.GRPC...)
  147. return err
  148. }, opts...)
  149. if err != nil {
  150. return nil, "", err
  151. }
  152. return resp.Contexts, resp.NextPageToken, nil
  153. }
  154. fetch := func(pageSize int, pageToken string) (string, error) {
  155. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  156. if err != nil {
  157. return "", err
  158. }
  159. it.items = append(it.items, items...)
  160. return nextPageToken, nil
  161. }
  162. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  163. return it
  164. }
  165. // GetContext retrieves the specified context.
  166. func (c *ContextsClient) GetContext(ctx context.Context, req *dialogflowpb.GetContextRequest, opts ...gax.CallOption) (*dialogflowpb.Context, error) {
  167. ctx = insertMetadata(ctx, c.xGoogMetadata)
  168. opts = append(c.CallOptions.GetContext[0:len(c.CallOptions.GetContext):len(c.CallOptions.GetContext)], opts...)
  169. var resp *dialogflowpb.Context
  170. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  171. var err error
  172. resp, err = c.contextsClient.GetContext(ctx, req, settings.GRPC...)
  173. return err
  174. }, opts...)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return resp, nil
  179. }
  180. // CreateContext creates a context.
  181. func (c *ContextsClient) CreateContext(ctx context.Context, req *dialogflowpb.CreateContextRequest, opts ...gax.CallOption) (*dialogflowpb.Context, error) {
  182. ctx = insertMetadata(ctx, c.xGoogMetadata)
  183. opts = append(c.CallOptions.CreateContext[0:len(c.CallOptions.CreateContext):len(c.CallOptions.CreateContext)], opts...)
  184. var resp *dialogflowpb.Context
  185. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  186. var err error
  187. resp, err = c.contextsClient.CreateContext(ctx, req, settings.GRPC...)
  188. return err
  189. }, opts...)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return resp, nil
  194. }
  195. // UpdateContext updates the specified context.
  196. func (c *ContextsClient) UpdateContext(ctx context.Context, req *dialogflowpb.UpdateContextRequest, opts ...gax.CallOption) (*dialogflowpb.Context, error) {
  197. ctx = insertMetadata(ctx, c.xGoogMetadata)
  198. opts = append(c.CallOptions.UpdateContext[0:len(c.CallOptions.UpdateContext):len(c.CallOptions.UpdateContext)], opts...)
  199. var resp *dialogflowpb.Context
  200. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  201. var err error
  202. resp, err = c.contextsClient.UpdateContext(ctx, req, settings.GRPC...)
  203. return err
  204. }, opts...)
  205. if err != nil {
  206. return nil, err
  207. }
  208. return resp, nil
  209. }
  210. // DeleteContext deletes the specified context.
  211. func (c *ContextsClient) DeleteContext(ctx context.Context, req *dialogflowpb.DeleteContextRequest, opts ...gax.CallOption) error {
  212. ctx = insertMetadata(ctx, c.xGoogMetadata)
  213. opts = append(c.CallOptions.DeleteContext[0:len(c.CallOptions.DeleteContext):len(c.CallOptions.DeleteContext)], opts...)
  214. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  215. var err error
  216. _, err = c.contextsClient.DeleteContext(ctx, req, settings.GRPC...)
  217. return err
  218. }, opts...)
  219. return err
  220. }
  221. // DeleteAllContexts deletes all active contexts in the specified session.
  222. func (c *ContextsClient) DeleteAllContexts(ctx context.Context, req *dialogflowpb.DeleteAllContextsRequest, opts ...gax.CallOption) error {
  223. ctx = insertMetadata(ctx, c.xGoogMetadata)
  224. opts = append(c.CallOptions.DeleteAllContexts[0:len(c.CallOptions.DeleteAllContexts):len(c.CallOptions.DeleteAllContexts)], opts...)
  225. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  226. var err error
  227. _, err = c.contextsClient.DeleteAllContexts(ctx, req, settings.GRPC...)
  228. return err
  229. }, opts...)
  230. return err
  231. }
  232. // ContextIterator manages a stream of *dialogflowpb.Context.
  233. type ContextIterator struct {
  234. items []*dialogflowpb.Context
  235. pageInfo *iterator.PageInfo
  236. nextFunc func() error
  237. // InternalFetch is for use by the Google Cloud Libraries only.
  238. // It is not part of the stable interface of this package.
  239. //
  240. // InternalFetch returns results from a single call to the underlying RPC.
  241. // The number of results is no greater than pageSize.
  242. // If there are no more results, nextPageToken is empty and err is nil.
  243. InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Context, nextPageToken string, err error)
  244. }
  245. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  246. func (it *ContextIterator) PageInfo() *iterator.PageInfo {
  247. return it.pageInfo
  248. }
  249. // Next returns the next result. Its second return value is iterator.Done if there are no more
  250. // results. Once Next returns Done, all subsequent calls will return Done.
  251. func (it *ContextIterator) Next() (*dialogflowpb.Context, error) {
  252. var item *dialogflowpb.Context
  253. if err := it.nextFunc(); err != nil {
  254. return item, err
  255. }
  256. item = it.items[0]
  257. it.items = it.items[1:]
  258. return item, nil
  259. }
  260. func (it *ContextIterator) bufLen() int {
  261. return len(it.items)
  262. }
  263. func (it *ContextIterator) takeBuf() interface{} {
  264. b := it.items
  265. it.items = nil
  266. return b
  267. }