1// Copyright 2021 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 15// Code generated by protoc-gen-go_gapic. DO NOT EDIT. 16 17package cx 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "github.com/golang/protobuf/proto" 27 gax "github.com/googleapis/gax-go/v2" 28 "google.golang.org/api/iterator" 29 "google.golang.org/api/option" 30 "google.golang.org/api/option/internaloption" 31 gtransport "google.golang.org/api/transport/grpc" 32 cxpb "google.golang.org/genproto/googleapis/cloud/dialogflow/cx/v3beta1" 33 "google.golang.org/grpc" 34 "google.golang.org/grpc/codes" 35 "google.golang.org/grpc/metadata" 36) 37 38var newIntentsClientHook clientHook 39 40// IntentsCallOptions contains the retry settings for each method of IntentsClient. 41type IntentsCallOptions struct { 42 ListIntents []gax.CallOption 43 GetIntent []gax.CallOption 44 CreateIntent []gax.CallOption 45 UpdateIntent []gax.CallOption 46 DeleteIntent []gax.CallOption 47} 48 49func defaultIntentsClientOptions() []option.ClientOption { 50 return []option.ClientOption{ 51 internaloption.WithDefaultEndpoint("dialogflow.googleapis.com:443"), 52 internaloption.WithDefaultMTLSEndpoint("dialogflow.mtls.googleapis.com:443"), 53 internaloption.WithDefaultAudience("https://dialogflow.googleapis.com/"), 54 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 55 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 56 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 57 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 58 } 59} 60 61func defaultIntentsCallOptions() *IntentsCallOptions { 62 return &IntentsCallOptions{ 63 ListIntents: []gax.CallOption{ 64 gax.WithRetry(func() gax.Retryer { 65 return gax.OnCodes([]codes.Code{ 66 codes.Unavailable, 67 }, gax.Backoff{ 68 Initial: 100 * time.Millisecond, 69 Max: 60000 * time.Millisecond, 70 Multiplier: 1.30, 71 }) 72 }), 73 }, 74 GetIntent: []gax.CallOption{ 75 gax.WithRetry(func() gax.Retryer { 76 return gax.OnCodes([]codes.Code{ 77 codes.Unavailable, 78 }, gax.Backoff{ 79 Initial: 100 * time.Millisecond, 80 Max: 60000 * time.Millisecond, 81 Multiplier: 1.30, 82 }) 83 }), 84 }, 85 CreateIntent: []gax.CallOption{ 86 gax.WithRetry(func() gax.Retryer { 87 return gax.OnCodes([]codes.Code{ 88 codes.Unavailable, 89 }, gax.Backoff{ 90 Initial: 100 * time.Millisecond, 91 Max: 60000 * time.Millisecond, 92 Multiplier: 1.30, 93 }) 94 }), 95 }, 96 UpdateIntent: []gax.CallOption{ 97 gax.WithRetry(func() gax.Retryer { 98 return gax.OnCodes([]codes.Code{ 99 codes.Unavailable, 100 }, gax.Backoff{ 101 Initial: 100 * time.Millisecond, 102 Max: 60000 * time.Millisecond, 103 Multiplier: 1.30, 104 }) 105 }), 106 }, 107 DeleteIntent: []gax.CallOption{ 108 gax.WithRetry(func() gax.Retryer { 109 return gax.OnCodes([]codes.Code{ 110 codes.Unavailable, 111 }, gax.Backoff{ 112 Initial: 100 * time.Millisecond, 113 Max: 60000 * time.Millisecond, 114 Multiplier: 1.30, 115 }) 116 }), 117 }, 118 } 119} 120 121// IntentsClient is a client for interacting with Dialogflow API. 122// 123// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 124type IntentsClient struct { 125 // Connection pool of gRPC connections to the service. 126 connPool gtransport.ConnPool 127 128 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 129 disableDeadlines bool 130 131 // The gRPC API client. 132 intentsClient cxpb.IntentsClient 133 134 // The call options for this service. 135 CallOptions *IntentsCallOptions 136 137 // The x-goog-* metadata to be sent with each request. 138 xGoogMetadata metadata.MD 139} 140 141// NewIntentsClient creates a new intents client. 142// 143// Service for managing Intents. 144func NewIntentsClient(ctx context.Context, opts ...option.ClientOption) (*IntentsClient, error) { 145 clientOpts := defaultIntentsClientOptions() 146 147 if newIntentsClientHook != nil { 148 hookOpts, err := newIntentsClientHook(ctx, clientHookParams{}) 149 if err != nil { 150 return nil, err 151 } 152 clientOpts = append(clientOpts, hookOpts...) 153 } 154 155 disableDeadlines, err := checkDisableDeadlines() 156 if err != nil { 157 return nil, err 158 } 159 160 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 161 if err != nil { 162 return nil, err 163 } 164 c := &IntentsClient{ 165 connPool: connPool, 166 disableDeadlines: disableDeadlines, 167 CallOptions: defaultIntentsCallOptions(), 168 169 intentsClient: cxpb.NewIntentsClient(connPool), 170 } 171 c.setGoogleClientInfo() 172 173 return c, nil 174} 175 176// Connection returns a connection to the API service. 177// 178// Deprecated. 179func (c *IntentsClient) Connection() *grpc.ClientConn { 180 return c.connPool.Conn() 181} 182 183// Close closes the connection to the API service. The user should invoke this when 184// the client is no longer required. 185func (c *IntentsClient) Close() error { 186 return c.connPool.Close() 187} 188 189// setGoogleClientInfo sets the name and version of the application in 190// the `x-goog-api-client` header passed on each request. Intended for 191// use by Google-written clients. 192func (c *IntentsClient) setGoogleClientInfo(keyval ...string) { 193 kv := append([]string{"gl-go", versionGo()}, keyval...) 194 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 195 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 196} 197 198// ListIntents returns the list of all intents in the specified agent. 199func (c *IntentsClient) ListIntents(ctx context.Context, req *cxpb.ListIntentsRequest, opts ...gax.CallOption) *IntentIterator { 200 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 201 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 202 opts = append(c.CallOptions.ListIntents[0:len(c.CallOptions.ListIntents):len(c.CallOptions.ListIntents)], opts...) 203 it := &IntentIterator{} 204 req = proto.Clone(req).(*cxpb.ListIntentsRequest) 205 it.InternalFetch = func(pageSize int, pageToken string) ([]*cxpb.Intent, string, error) { 206 var resp *cxpb.ListIntentsResponse 207 req.PageToken = pageToken 208 if pageSize > math.MaxInt32 { 209 req.PageSize = math.MaxInt32 210 } else { 211 req.PageSize = int32(pageSize) 212 } 213 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 214 var err error 215 resp, err = c.intentsClient.ListIntents(ctx, req, settings.GRPC...) 216 return err 217 }, opts...) 218 if err != nil { 219 return nil, "", err 220 } 221 222 it.Response = resp 223 return resp.GetIntents(), resp.GetNextPageToken(), nil 224 } 225 fetch := func(pageSize int, pageToken string) (string, error) { 226 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 227 if err != nil { 228 return "", err 229 } 230 it.items = append(it.items, items...) 231 return nextPageToken, nil 232 } 233 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 234 it.pageInfo.MaxSize = int(req.GetPageSize()) 235 it.pageInfo.Token = req.GetPageToken() 236 return it 237} 238 239// GetIntent retrieves the specified intent. 240func (c *IntentsClient) GetIntent(ctx context.Context, req *cxpb.GetIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) { 241 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 242 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 243 defer cancel() 244 ctx = cctx 245 } 246 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 247 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 248 opts = append(c.CallOptions.GetIntent[0:len(c.CallOptions.GetIntent):len(c.CallOptions.GetIntent)], opts...) 249 var resp *cxpb.Intent 250 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 251 var err error 252 resp, err = c.intentsClient.GetIntent(ctx, req, settings.GRPC...) 253 return err 254 }, opts...) 255 if err != nil { 256 return nil, err 257 } 258 return resp, nil 259} 260 261// CreateIntent creates an intent in the specified agent. 262func (c *IntentsClient) CreateIntent(ctx context.Context, req *cxpb.CreateIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) { 263 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 264 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 265 defer cancel() 266 ctx = cctx 267 } 268 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 269 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 270 opts = append(c.CallOptions.CreateIntent[0:len(c.CallOptions.CreateIntent):len(c.CallOptions.CreateIntent)], opts...) 271 var resp *cxpb.Intent 272 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 273 var err error 274 resp, err = c.intentsClient.CreateIntent(ctx, req, settings.GRPC...) 275 return err 276 }, opts...) 277 if err != nil { 278 return nil, err 279 } 280 return resp, nil 281} 282 283// UpdateIntent updates the specified intent. 284func (c *IntentsClient) UpdateIntent(ctx context.Context, req *cxpb.UpdateIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) { 285 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 286 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 287 defer cancel() 288 ctx = cctx 289 } 290 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "intent.name", url.QueryEscape(req.GetIntent().GetName()))) 291 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 292 opts = append(c.CallOptions.UpdateIntent[0:len(c.CallOptions.UpdateIntent):len(c.CallOptions.UpdateIntent)], opts...) 293 var resp *cxpb.Intent 294 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 295 var err error 296 resp, err = c.intentsClient.UpdateIntent(ctx, req, settings.GRPC...) 297 return err 298 }, opts...) 299 if err != nil { 300 return nil, err 301 } 302 return resp, nil 303} 304 305// DeleteIntent deletes the specified intent. 306func (c *IntentsClient) DeleteIntent(ctx context.Context, req *cxpb.DeleteIntentRequest, opts ...gax.CallOption) error { 307 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 308 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 309 defer cancel() 310 ctx = cctx 311 } 312 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 313 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 314 opts = append(c.CallOptions.DeleteIntent[0:len(c.CallOptions.DeleteIntent):len(c.CallOptions.DeleteIntent)], opts...) 315 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 316 var err error 317 _, err = c.intentsClient.DeleteIntent(ctx, req, settings.GRPC...) 318 return err 319 }, opts...) 320 return err 321} 322 323// IntentIterator manages a stream of *cxpb.Intent. 324type IntentIterator struct { 325 items []*cxpb.Intent 326 pageInfo *iterator.PageInfo 327 nextFunc func() error 328 329 // Response is the raw response for the current page. 330 // It must be cast to the RPC response type. 331 // Calling Next() or InternalFetch() updates this value. 332 Response interface{} 333 334 // InternalFetch is for use by the Google Cloud Libraries only. 335 // It is not part of the stable interface of this package. 336 // 337 // InternalFetch returns results from a single call to the underlying RPC. 338 // The number of results is no greater than pageSize. 339 // If there are no more results, nextPageToken is empty and err is nil. 340 InternalFetch func(pageSize int, pageToken string) (results []*cxpb.Intent, nextPageToken string, err error) 341} 342 343// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 344func (it *IntentIterator) PageInfo() *iterator.PageInfo { 345 return it.pageInfo 346} 347 348// Next returns the next result. Its second return value is iterator.Done if there are no more 349// results. Once Next returns Done, all subsequent calls will return Done. 350func (it *IntentIterator) Next() (*cxpb.Intent, error) { 351 var item *cxpb.Intent 352 if err := it.nextFunc(); err != nil { 353 return item, err 354 } 355 item = it.items[0] 356 it.items = it.items[1:] 357 return item, nil 358} 359 360func (it *IntentIterator) bufLen() int { 361 return len(it.items) 362} 363 364func (it *IntentIterator) takeBuf() interface{} { 365 b := it.items 366 it.items = nil 367 return b 368} 369