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