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