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