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 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 defaultWebhooksClientOptions() []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 defaultWebhooksCallOptions() *WebhooksCallOptions {
62	return &WebhooksCallOptions{
63		ListWebhooks: []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		GetWebhook: []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		CreateWebhook: []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		UpdateWebhook: []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		DeleteWebhook: []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// WebhooksClient 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 WebhooksClient 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	webhooksClient cxpb.WebhooksClient
133
134	// The call options for this service.
135	CallOptions *WebhooksCallOptions
136
137	// The x-goog-* metadata to be sent with each request.
138	xGoogMetadata metadata.MD
139}
140
141// NewWebhooksClient creates a new webhooks client.
142//
143// Service for managing Webhooks.
144func NewWebhooksClient(ctx context.Context, opts ...option.ClientOption) (*WebhooksClient, error) {
145	clientOpts := defaultWebhooksClientOptions()
146
147	if newWebhooksClientHook != nil {
148		hookOpts, err := newWebhooksClientHook(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 := &WebhooksClient{
165		connPool:         connPool,
166		disableDeadlines: disableDeadlines,
167		CallOptions:      defaultWebhooksCallOptions(),
168
169		webhooksClient: cxpb.NewWebhooksClient(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 *WebhooksClient) 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 *WebhooksClient) 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 *WebhooksClient) 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// ListWebhooks returns the list of all webhooks in the specified agent.
199func (c *WebhooksClient) ListWebhooks(ctx context.Context, req *cxpb.ListWebhooksRequest, opts ...gax.CallOption) *WebhookIterator {
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.ListWebhooks[0:len(c.CallOptions.ListWebhooks):len(c.CallOptions.ListWebhooks)], opts...)
203	it := &WebhookIterator{}
204	req = proto.Clone(req).(*cxpb.ListWebhooksRequest)
205	it.InternalFetch = func(pageSize int, pageToken string) ([]*cxpb.Webhook, string, error) {
206		var resp *cxpb.ListWebhooksResponse
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.webhooksClient.ListWebhooks(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.GetWebhooks(), 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// GetWebhook retrieves the specified webhook.
240func (c *WebhooksClient) GetWebhook(ctx context.Context, req *cxpb.GetWebhookRequest, opts ...gax.CallOption) (*cxpb.Webhook, 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.GetWebhook[0:len(c.CallOptions.GetWebhook):len(c.CallOptions.GetWebhook)], opts...)
249	var resp *cxpb.Webhook
250	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
251		var err error
252		resp, err = c.webhooksClient.GetWebhook(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// CreateWebhook creates a webhook in the specified agent.
262func (c *WebhooksClient) CreateWebhook(ctx context.Context, req *cxpb.CreateWebhookRequest, opts ...gax.CallOption) (*cxpb.Webhook, 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.CreateWebhook[0:len(c.CallOptions.CreateWebhook):len(c.CallOptions.CreateWebhook)], opts...)
271	var resp *cxpb.Webhook
272	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
273		var err error
274		resp, err = c.webhooksClient.CreateWebhook(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// UpdateWebhook updates the specified webhook.
284func (c *WebhooksClient) UpdateWebhook(ctx context.Context, req *cxpb.UpdateWebhookRequest, opts ...gax.CallOption) (*cxpb.Webhook, 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", "webhook.name", url.QueryEscape(req.GetWebhook().GetName())))
291	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
292	opts = append(c.CallOptions.UpdateWebhook[0:len(c.CallOptions.UpdateWebhook):len(c.CallOptions.UpdateWebhook)], opts...)
293	var resp *cxpb.Webhook
294	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
295		var err error
296		resp, err = c.webhooksClient.UpdateWebhook(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// DeleteWebhook deletes the specified webhook.
306func (c *WebhooksClient) DeleteWebhook(ctx context.Context, req *cxpb.DeleteWebhookRequest, 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.DeleteWebhook[0:len(c.CallOptions.DeleteWebhook):len(c.CallOptions.DeleteWebhook)], opts...)
315	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
316		var err error
317		_, err = c.webhooksClient.DeleteWebhook(ctx, req, settings.GRPC...)
318		return err
319	}, opts...)
320	return err
321}
322
323// WebhookIterator manages a stream of *cxpb.Webhook.
324type WebhookIterator struct {
325	items    []*cxpb.Webhook
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.Webhook, nextPageToken string, err error)
341}
342
343// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
344func (it *WebhookIterator) 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 *WebhookIterator) Next() (*cxpb.Webhook, error) {
351	var item *cxpb.Webhook
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 *WebhookIterator) bufLen() int {
361	return len(it.items)
362}
363
364func (it *WebhookIterator) takeBuf() interface{} {
365	b := it.items
366	it.items = nil
367	return b
368}
369