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