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 newEntityTypesClientHook clientHook
39
40// EntityTypesCallOptions contains the retry settings for each method of EntityTypesClient.
41type EntityTypesCallOptions struct {
42	ListEntityTypes  []gax.CallOption
43	GetEntityType    []gax.CallOption
44	CreateEntityType []gax.CallOption
45	UpdateEntityType []gax.CallOption
46	DeleteEntityType []gax.CallOption
47}
48
49func defaultEntityTypesGRPCClientOptions() []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 defaultEntityTypesCallOptions() *EntityTypesCallOptions {
63	return &EntityTypesCallOptions{
64		ListEntityTypes: []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		GetEntityType: []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		CreateEntityType: []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		UpdateEntityType: []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		DeleteEntityType: []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// internalEntityTypesClient is an interface that defines the methods availaible from Dialogflow API.
123type internalEntityTypesClient interface {
124	Close() error
125	setGoogleClientInfo(...string)
126	Connection() *grpc.ClientConn
127	ListEntityTypes(context.Context, *cxpb.ListEntityTypesRequest, ...gax.CallOption) *EntityTypeIterator
128	GetEntityType(context.Context, *cxpb.GetEntityTypeRequest, ...gax.CallOption) (*cxpb.EntityType, error)
129	CreateEntityType(context.Context, *cxpb.CreateEntityTypeRequest, ...gax.CallOption) (*cxpb.EntityType, error)
130	UpdateEntityType(context.Context, *cxpb.UpdateEntityTypeRequest, ...gax.CallOption) (*cxpb.EntityType, error)
131	DeleteEntityType(context.Context, *cxpb.DeleteEntityTypeRequest, ...gax.CallOption) error
132}
133
134// EntityTypesClient 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 EntityTypes.
138type EntityTypesClient struct {
139	// The internal transport-dependent client.
140	internalClient internalEntityTypesClient
141
142	// The call options for this service.
143	CallOptions *EntityTypesCallOptions
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 *EntityTypesClient) 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 *EntityTypesClient) setGoogleClientInfo(keyval ...string) {
158	c.internalClient.setGoogleClientInfo(keyval...)
159}
160
161// Connection returns a connection to the API service.
162//
163// Deprecated.
164func (c *EntityTypesClient) Connection() *grpc.ClientConn {
165	return c.internalClient.Connection()
166}
167
168// ListEntityTypes returns the list of all entity types in the specified agent.
169func (c *EntityTypesClient) ListEntityTypes(ctx context.Context, req *cxpb.ListEntityTypesRequest, opts ...gax.CallOption) *EntityTypeIterator {
170	return c.internalClient.ListEntityTypes(ctx, req, opts...)
171}
172
173// GetEntityType retrieves the specified entity type.
174func (c *EntityTypesClient) GetEntityType(ctx context.Context, req *cxpb.GetEntityTypeRequest, opts ...gax.CallOption) (*cxpb.EntityType, error) {
175	return c.internalClient.GetEntityType(ctx, req, opts...)
176}
177
178// CreateEntityType creates an entity type 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 *EntityTypesClient) CreateEntityType(ctx context.Context, req *cxpb.CreateEntityTypeRequest, opts ...gax.CallOption) (*cxpb.EntityType, error) {
184	return c.internalClient.CreateEntityType(ctx, req, opts...)
185}
186
187// UpdateEntityType updates the specified entity type.
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 *EntityTypesClient) UpdateEntityType(ctx context.Context, req *cxpb.UpdateEntityTypeRequest, opts ...gax.CallOption) (*cxpb.EntityType, error) {
193	return c.internalClient.UpdateEntityType(ctx, req, opts...)
194}
195
196// DeleteEntityType deletes the specified entity type.
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 *EntityTypesClient) DeleteEntityType(ctx context.Context, req *cxpb.DeleteEntityTypeRequest, opts ...gax.CallOption) error {
202	return c.internalClient.DeleteEntityType(ctx, req, opts...)
203}
204
205// entityTypesGRPCClient 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 entityTypesGRPCClient 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 EntityTypesClient
216	CallOptions **EntityTypesCallOptions
217
218	// The gRPC API client.
219	entityTypesClient cxpb.EntityTypesClient
220
221	// The x-goog-* metadata to be sent with each request.
222	xGoogMetadata metadata.MD
223}
224
225// NewEntityTypesClient creates a new entity types 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 EntityTypes.
229func NewEntityTypesClient(ctx context.Context, opts ...option.ClientOption) (*EntityTypesClient, error) {
230	clientOpts := defaultEntityTypesGRPCClientOptions()
231	if newEntityTypesClientHook != nil {
232		hookOpts, err := newEntityTypesClientHook(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 := EntityTypesClient{CallOptions: defaultEntityTypesCallOptions()}
249
250	c := &entityTypesGRPCClient{
251		connPool:          connPool,
252		disableDeadlines:  disableDeadlines,
253		entityTypesClient: cxpb.NewEntityTypesClient(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 *entityTypesGRPCClient) 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 *entityTypesGRPCClient) 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 *entityTypesGRPCClient) Close() error {
282	return c.connPool.Close()
283}
284
285func (c *entityTypesGRPCClient) ListEntityTypes(ctx context.Context, req *cxpb.ListEntityTypesRequest, opts ...gax.CallOption) *EntityTypeIterator {
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).ListEntityTypes[0:len((*c.CallOptions).ListEntityTypes):len((*c.CallOptions).ListEntityTypes)], opts...)
289	it := &EntityTypeIterator{}
290	req = proto.Clone(req).(*cxpb.ListEntityTypesRequest)
291	it.InternalFetch = func(pageSize int, pageToken string) ([]*cxpb.EntityType, string, error) {
292		resp := &cxpb.ListEntityTypesResponse{}
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.entityTypesClient.ListEntityTypes(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.GetEntityTypes(), 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 *entityTypesGRPCClient) GetEntityType(ctx context.Context, req *cxpb.GetEntityTypeRequest, opts ...gax.CallOption) (*cxpb.EntityType, 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).GetEntityType[0:len((*c.CallOptions).GetEntityType):len((*c.CallOptions).GetEntityType)], opts...)
338	var resp *cxpb.EntityType
339	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
340		var err error
341		resp, err = c.entityTypesClient.GetEntityType(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 *entityTypesGRPCClient) CreateEntityType(ctx context.Context, req *cxpb.CreateEntityTypeRequest, opts ...gax.CallOption) (*cxpb.EntityType, 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).CreateEntityType[0:len((*c.CallOptions).CreateEntityType):len((*c.CallOptions).CreateEntityType)], opts...)
359	var resp *cxpb.EntityType
360	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
361		var err error
362		resp, err = c.entityTypesClient.CreateEntityType(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 *entityTypesGRPCClient) UpdateEntityType(ctx context.Context, req *cxpb.UpdateEntityTypeRequest, opts ...gax.CallOption) (*cxpb.EntityType, 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", "entity_type.name", url.QueryEscape(req.GetEntityType().GetName())))
378	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
379	opts = append((*c.CallOptions).UpdateEntityType[0:len((*c.CallOptions).UpdateEntityType):len((*c.CallOptions).UpdateEntityType)], opts...)
380	var resp *cxpb.EntityType
381	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
382		var err error
383		resp, err = c.entityTypesClient.UpdateEntityType(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 *entityTypesGRPCClient) DeleteEntityType(ctx context.Context, req *cxpb.DeleteEntityTypeRequest, 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).DeleteEntityType[0:len((*c.CallOptions).DeleteEntityType):len((*c.CallOptions).DeleteEntityType)], opts...)
401	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
402		var err error
403		_, err = c.entityTypesClient.DeleteEntityType(ctx, req, settings.GRPC...)
404		return err
405	}, opts...)
406	return err
407}
408
409// EntityTypeIterator manages a stream of *cxpb.EntityType.
410type EntityTypeIterator struct {
411	items    []*cxpb.EntityType
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.EntityType, nextPageToken string, err error)
427}
428
429// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
430func (it *EntityTypeIterator) 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 *EntityTypeIterator) Next() (*cxpb.EntityType, error) {
437	var item *cxpb.EntityType
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 *EntityTypeIterator) bufLen() int {
447	return len(it.items)
448}
449
450func (it *EntityTypeIterator) takeBuf() interface{} {
451	b := it.items
452	it.items = nil
453	return b
454}
455