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.
 
 
 

298 lines
11 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. "github.com/golang/protobuf/proto"
  21. gax "github.com/googleapis/gax-go/v2"
  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])
  94. // request, or as output contexts included in the returned intent. Contexts
  95. // expire when an intent is matched, after the number of DetectIntent requests
  96. // specified by the lifespan_count parameter, or after 10 minutes if no
  97. // 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", versionGo()}, keyval...)
  128. kv = append(kv, "gapic", versionClient, "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. req = proto.Clone(req).(*dialogflowpb.ListContextsRequest)
  137. it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Context, string, error) {
  138. var resp *dialogflowpb.ListContextsResponse
  139. req.PageToken = pageToken
  140. if pageSize > math.MaxInt32 {
  141. req.PageSize = math.MaxInt32
  142. } else {
  143. req.PageSize = int32(pageSize)
  144. }
  145. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  146. var err error
  147. resp, err = c.contextsClient.ListContexts(ctx, req, settings.GRPC...)
  148. return err
  149. }, opts...)
  150. if err != nil {
  151. return nil, "", err
  152. }
  153. return resp.Contexts, resp.NextPageToken, nil
  154. }
  155. fetch := func(pageSize int, pageToken string) (string, error) {
  156. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  157. if err != nil {
  158. return "", err
  159. }
  160. it.items = append(it.items, items...)
  161. return nextPageToken, nil
  162. }
  163. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  164. it.pageInfo.MaxSize = int(req.PageSize)
  165. return it
  166. }
  167. // GetContext retrieves the specified context.
  168. func (c *ContextsClient) GetContext(ctx context.Context, req *dialogflowpb.GetContextRequest, opts ...gax.CallOption) (*dialogflowpb.Context, error) {
  169. ctx = insertMetadata(ctx, c.xGoogMetadata)
  170. opts = append(c.CallOptions.GetContext[0:len(c.CallOptions.GetContext):len(c.CallOptions.GetContext)], opts...)
  171. var resp *dialogflowpb.Context
  172. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  173. var err error
  174. resp, err = c.contextsClient.GetContext(ctx, req, settings.GRPC...)
  175. return err
  176. }, opts...)
  177. if err != nil {
  178. return nil, err
  179. }
  180. return resp, nil
  181. }
  182. // CreateContext creates a context.
  183. func (c *ContextsClient) CreateContext(ctx context.Context, req *dialogflowpb.CreateContextRequest, opts ...gax.CallOption) (*dialogflowpb.Context, error) {
  184. ctx = insertMetadata(ctx, c.xGoogMetadata)
  185. opts = append(c.CallOptions.CreateContext[0:len(c.CallOptions.CreateContext):len(c.CallOptions.CreateContext)], opts...)
  186. var resp *dialogflowpb.Context
  187. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  188. var err error
  189. resp, err = c.contextsClient.CreateContext(ctx, req, settings.GRPC...)
  190. return err
  191. }, opts...)
  192. if err != nil {
  193. return nil, err
  194. }
  195. return resp, nil
  196. }
  197. // UpdateContext updates the specified context.
  198. func (c *ContextsClient) UpdateContext(ctx context.Context, req *dialogflowpb.UpdateContextRequest, opts ...gax.CallOption) (*dialogflowpb.Context, error) {
  199. ctx = insertMetadata(ctx, c.xGoogMetadata)
  200. opts = append(c.CallOptions.UpdateContext[0:len(c.CallOptions.UpdateContext):len(c.CallOptions.UpdateContext)], opts...)
  201. var resp *dialogflowpb.Context
  202. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  203. var err error
  204. resp, err = c.contextsClient.UpdateContext(ctx, req, settings.GRPC...)
  205. return err
  206. }, opts...)
  207. if err != nil {
  208. return nil, err
  209. }
  210. return resp, nil
  211. }
  212. // DeleteContext deletes the specified context.
  213. func (c *ContextsClient) DeleteContext(ctx context.Context, req *dialogflowpb.DeleteContextRequest, opts ...gax.CallOption) error {
  214. ctx = insertMetadata(ctx, c.xGoogMetadata)
  215. opts = append(c.CallOptions.DeleteContext[0:len(c.CallOptions.DeleteContext):len(c.CallOptions.DeleteContext)], opts...)
  216. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  217. var err error
  218. _, err = c.contextsClient.DeleteContext(ctx, req, settings.GRPC...)
  219. return err
  220. }, opts...)
  221. return err
  222. }
  223. // DeleteAllContexts deletes all active contexts in the specified session.
  224. func (c *ContextsClient) DeleteAllContexts(ctx context.Context, req *dialogflowpb.DeleteAllContextsRequest, opts ...gax.CallOption) error {
  225. ctx = insertMetadata(ctx, c.xGoogMetadata)
  226. opts = append(c.CallOptions.DeleteAllContexts[0:len(c.CallOptions.DeleteAllContexts):len(c.CallOptions.DeleteAllContexts)], opts...)
  227. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  228. var err error
  229. _, err = c.contextsClient.DeleteAllContexts(ctx, req, settings.GRPC...)
  230. return err
  231. }, opts...)
  232. return err
  233. }
  234. // ContextIterator manages a stream of *dialogflowpb.Context.
  235. type ContextIterator struct {
  236. items []*dialogflowpb.Context
  237. pageInfo *iterator.PageInfo
  238. nextFunc func() error
  239. // InternalFetch is for use by the Google Cloud Libraries only.
  240. // It is not part of the stable interface of this package.
  241. //
  242. // InternalFetch returns results from a single call to the underlying RPC.
  243. // The number of results is no greater than pageSize.
  244. // If there are no more results, nextPageToken is empty and err is nil.
  245. InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Context, nextPageToken string, err error)
  246. }
  247. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  248. func (it *ContextIterator) PageInfo() *iterator.PageInfo {
  249. return it.pageInfo
  250. }
  251. // Next returns the next result. Its second return value is iterator.Done if there are no more
  252. // results. Once Next returns Done, all subsequent calls will return Done.
  253. func (it *ContextIterator) Next() (*dialogflowpb.Context, error) {
  254. var item *dialogflowpb.Context
  255. if err := it.nextFunc(); err != nil {
  256. return item, err
  257. }
  258. item = it.items[0]
  259. it.items = it.items[1:]
  260. return item, nil
  261. }
  262. func (it *ContextIterator) bufLen() int {
  263. return len(it.items)
  264. }
  265. func (it *ContextIterator) takeBuf() interface{} {
  266. b := it.items
  267. it.items = nil
  268. return b
  269. }