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