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 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 defaultIntentsGRPCClientOptions() []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// internalIntentsClient is an interface that defines the methods availaible from Dialogflow API.
122type internalIntentsClient interface {
123	Close() error
124	setGoogleClientInfo(...string)
125	Connection() *grpc.ClientConn
126	ListIntents(context.Context, *cxpb.ListIntentsRequest, ...gax.CallOption) *IntentIterator
127	GetIntent(context.Context, *cxpb.GetIntentRequest, ...gax.CallOption) (*cxpb.Intent, error)
128	CreateIntent(context.Context, *cxpb.CreateIntentRequest, ...gax.CallOption) (*cxpb.Intent, error)
129	UpdateIntent(context.Context, *cxpb.UpdateIntentRequest, ...gax.CallOption) (*cxpb.Intent, error)
130	DeleteIntent(context.Context, *cxpb.DeleteIntentRequest, ...gax.CallOption) error
131}
132
133// IntentsClient is a client for interacting with Dialogflow API.
134// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
135//
136// Service for managing Intents.
137type IntentsClient struct {
138	// The internal transport-dependent client.
139	internalClient internalIntentsClient
140
141	// The call options for this service.
142	CallOptions *IntentsCallOptions
143}
144
145// Wrapper methods routed to the internal client.
146
147// Close closes the connection to the API service. The user should invoke this when
148// the client is no longer required.
149func (c *IntentsClient) Close() error {
150	return c.internalClient.Close()
151}
152
153// setGoogleClientInfo sets the name and version of the application in
154// the `x-goog-api-client` header passed on each request. Intended for
155// use by Google-written clients.
156func (c *IntentsClient) setGoogleClientInfo(keyval ...string) {
157	c.internalClient.setGoogleClientInfo(keyval...)
158}
159
160// Connection returns a connection to the API service.
161//
162// Deprecated.
163func (c *IntentsClient) Connection() *grpc.ClientConn {
164	return c.internalClient.Connection()
165}
166
167// ListIntents returns the list of all intents in the specified agent.
168func (c *IntentsClient) ListIntents(ctx context.Context, req *cxpb.ListIntentsRequest, opts ...gax.CallOption) *IntentIterator {
169	return c.internalClient.ListIntents(ctx, req, opts...)
170}
171
172// GetIntent retrieves the specified intent.
173func (c *IntentsClient) GetIntent(ctx context.Context, req *cxpb.GetIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) {
174	return c.internalClient.GetIntent(ctx, req, opts...)
175}
176
177// CreateIntent creates an intent in the specified agent.
178//
179// Note: You should always train a flow prior to sending it queries. See the
180// training
181// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training).
182func (c *IntentsClient) CreateIntent(ctx context.Context, req *cxpb.CreateIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) {
183	return c.internalClient.CreateIntent(ctx, req, opts...)
184}
185
186// UpdateIntent updates the specified intent.
187//
188// Note: You should always train a flow prior to sending it queries. See the
189// training
190// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training).
191func (c *IntentsClient) UpdateIntent(ctx context.Context, req *cxpb.UpdateIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) {
192	return c.internalClient.UpdateIntent(ctx, req, opts...)
193}
194
195// DeleteIntent deletes the specified intent.
196//
197// Note: You should always train a flow prior to sending it queries. See the
198// training
199// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training).
200func (c *IntentsClient) DeleteIntent(ctx context.Context, req *cxpb.DeleteIntentRequest, opts ...gax.CallOption) error {
201	return c.internalClient.DeleteIntent(ctx, req, opts...)
202}
203
204// intentsGRPCClient is a client for interacting with Dialogflow API over gRPC transport.
205//
206// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
207type intentsGRPCClient struct {
208	// Connection pool of gRPC connections to the service.
209	connPool gtransport.ConnPool
210
211	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
212	disableDeadlines bool
213
214	// Points back to the CallOptions field of the containing IntentsClient
215	CallOptions **IntentsCallOptions
216
217	// The gRPC API client.
218	intentsClient cxpb.IntentsClient
219
220	// The x-goog-* metadata to be sent with each request.
221	xGoogMetadata metadata.MD
222}
223
224// NewIntentsClient creates a new intents client based on gRPC.
225// The returned client must be Closed when it is done being used to clean up its underlying connections.
226//
227// Service for managing Intents.
228func NewIntentsClient(ctx context.Context, opts ...option.ClientOption) (*IntentsClient, error) {
229	clientOpts := defaultIntentsGRPCClientOptions()
230	if newIntentsClientHook != nil {
231		hookOpts, err := newIntentsClientHook(ctx, clientHookParams{})
232		if err != nil {
233			return nil, err
234		}
235		clientOpts = append(clientOpts, hookOpts...)
236	}
237
238	disableDeadlines, err := checkDisableDeadlines()
239	if err != nil {
240		return nil, err
241	}
242
243	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
244	if err != nil {
245		return nil, err
246	}
247	client := IntentsClient{CallOptions: defaultIntentsCallOptions()}
248
249	c := &intentsGRPCClient{
250		connPool:         connPool,
251		disableDeadlines: disableDeadlines,
252		intentsClient:    cxpb.NewIntentsClient(connPool),
253		CallOptions:      &client.CallOptions,
254	}
255	c.setGoogleClientInfo()
256
257	client.internalClient = c
258
259	return &client, nil
260}
261
262// Connection returns a connection to the API service.
263//
264// Deprecated.
265func (c *intentsGRPCClient) Connection() *grpc.ClientConn {
266	return c.connPool.Conn()
267}
268
269// setGoogleClientInfo sets the name and version of the application in
270// the `x-goog-api-client` header passed on each request. Intended for
271// use by Google-written clients.
272func (c *intentsGRPCClient) setGoogleClientInfo(keyval ...string) {
273	kv := append([]string{"gl-go", versionGo()}, keyval...)
274	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
275	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
276}
277
278// Close closes the connection to the API service. The user should invoke this when
279// the client is no longer required.
280func (c *intentsGRPCClient) Close() error {
281	return c.connPool.Close()
282}
283
284func (c *intentsGRPCClient) ListIntents(ctx context.Context, req *cxpb.ListIntentsRequest, opts ...gax.CallOption) *IntentIterator {
285	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
286	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
287	opts = append((*c.CallOptions).ListIntents[0:len((*c.CallOptions).ListIntents):len((*c.CallOptions).ListIntents)], opts...)
288	it := &IntentIterator{}
289	req = proto.Clone(req).(*cxpb.ListIntentsRequest)
290	it.InternalFetch = func(pageSize int, pageToken string) ([]*cxpb.Intent, string, error) {
291		var resp *cxpb.ListIntentsResponse
292		req.PageToken = pageToken
293		if pageSize > math.MaxInt32 {
294			req.PageSize = math.MaxInt32
295		} else {
296			req.PageSize = int32(pageSize)
297		}
298		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
299			var err error
300			resp, err = c.intentsClient.ListIntents(ctx, req, settings.GRPC...)
301			return err
302		}, opts...)
303		if err != nil {
304			return nil, "", err
305		}
306
307		it.Response = resp
308		return resp.GetIntents(), resp.GetNextPageToken(), nil
309	}
310	fetch := func(pageSize int, pageToken string) (string, error) {
311		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
312		if err != nil {
313			return "", err
314		}
315		it.items = append(it.items, items...)
316		return nextPageToken, nil
317	}
318	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
319	it.pageInfo.MaxSize = int(req.GetPageSize())
320	it.pageInfo.Token = req.GetPageToken()
321	return it
322}
323
324func (c *intentsGRPCClient) GetIntent(ctx context.Context, req *cxpb.GetIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) {
325	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
326		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
327		defer cancel()
328		ctx = cctx
329	}
330	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
331	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
332	opts = append((*c.CallOptions).GetIntent[0:len((*c.CallOptions).GetIntent):len((*c.CallOptions).GetIntent)], opts...)
333	var resp *cxpb.Intent
334	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
335		var err error
336		resp, err = c.intentsClient.GetIntent(ctx, req, settings.GRPC...)
337		return err
338	}, opts...)
339	if err != nil {
340		return nil, err
341	}
342	return resp, nil
343}
344
345func (c *intentsGRPCClient) CreateIntent(ctx context.Context, req *cxpb.CreateIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) {
346	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
347		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
348		defer cancel()
349		ctx = cctx
350	}
351	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
352	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
353	opts = append((*c.CallOptions).CreateIntent[0:len((*c.CallOptions).CreateIntent):len((*c.CallOptions).CreateIntent)], opts...)
354	var resp *cxpb.Intent
355	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
356		var err error
357		resp, err = c.intentsClient.CreateIntent(ctx, req, settings.GRPC...)
358		return err
359	}, opts...)
360	if err != nil {
361		return nil, err
362	}
363	return resp, nil
364}
365
366func (c *intentsGRPCClient) UpdateIntent(ctx context.Context, req *cxpb.UpdateIntentRequest, opts ...gax.CallOption) (*cxpb.Intent, error) {
367	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
368		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
369		defer cancel()
370		ctx = cctx
371	}
372	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "intent.name", url.QueryEscape(req.GetIntent().GetName())))
373	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
374	opts = append((*c.CallOptions).UpdateIntent[0:len((*c.CallOptions).UpdateIntent):len((*c.CallOptions).UpdateIntent)], opts...)
375	var resp *cxpb.Intent
376	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
377		var err error
378		resp, err = c.intentsClient.UpdateIntent(ctx, req, settings.GRPC...)
379		return err
380	}, opts...)
381	if err != nil {
382		return nil, err
383	}
384	return resp, nil
385}
386
387func (c *intentsGRPCClient) DeleteIntent(ctx context.Context, req *cxpb.DeleteIntentRequest, opts ...gax.CallOption) error {
388	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
389		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
390		defer cancel()
391		ctx = cctx
392	}
393	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
394	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
395	opts = append((*c.CallOptions).DeleteIntent[0:len((*c.CallOptions).DeleteIntent):len((*c.CallOptions).DeleteIntent)], opts...)
396	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
397		var err error
398		_, err = c.intentsClient.DeleteIntent(ctx, req, settings.GRPC...)
399		return err
400	}, opts...)
401	return err
402}
403
404// IntentIterator manages a stream of *cxpb.Intent.
405type IntentIterator struct {
406	items    []*cxpb.Intent
407	pageInfo *iterator.PageInfo
408	nextFunc func() error
409
410	// Response is the raw response for the current page.
411	// It must be cast to the RPC response type.
412	// Calling Next() or InternalFetch() updates this value.
413	Response interface{}
414
415	// InternalFetch is for use by the Google Cloud Libraries only.
416	// It is not part of the stable interface of this package.
417	//
418	// InternalFetch returns results from a single call to the underlying RPC.
419	// The number of results is no greater than pageSize.
420	// If there are no more results, nextPageToken is empty and err is nil.
421	InternalFetch func(pageSize int, pageToken string) (results []*cxpb.Intent, nextPageToken string, err error)
422}
423
424// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
425func (it *IntentIterator) PageInfo() *iterator.PageInfo {
426	return it.pageInfo
427}
428
429// Next returns the next result. Its second return value is iterator.Done if there are no more
430// results. Once Next returns Done, all subsequent calls will return Done.
431func (it *IntentIterator) Next() (*cxpb.Intent, error) {
432	var item *cxpb.Intent
433	if err := it.nextFunc(); err != nil {
434		return item, err
435	}
436	item = it.items[0]
437	it.items = it.items[1:]
438	return item, nil
439}
440
441func (it *IntentIterator) bufLen() int {
442	return len(it.items)
443}
444
445func (it *IntentIterator) takeBuf() interface{} {
446	b := it.items
447	it.items = nil
448	return b
449}
450