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 newParticipantsClientHook clientHook 39 40// ParticipantsCallOptions contains the retry settings for each method of ParticipantsClient. 41type ParticipantsCallOptions struct { 42 CreateParticipant []gax.CallOption 43 GetParticipant []gax.CallOption 44 ListParticipants []gax.CallOption 45 UpdateParticipant []gax.CallOption 46 AnalyzeContent []gax.CallOption 47 SuggestArticles []gax.CallOption 48 SuggestFaqAnswers []gax.CallOption 49} 50 51func defaultParticipantsGRPCClientOptions() []option.ClientOption { 52 return []option.ClientOption{ 53 internaloption.WithDefaultEndpoint("dialogflow.googleapis.com:443"), 54 internaloption.WithDefaultMTLSEndpoint("dialogflow.mtls.googleapis.com:443"), 55 internaloption.WithDefaultAudience("https://dialogflow.googleapis.com/"), 56 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 57 internaloption.EnableJwtWithScope(), 58 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 59 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 60 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 61 } 62} 63 64func defaultParticipantsCallOptions() *ParticipantsCallOptions { 65 return &ParticipantsCallOptions{ 66 CreateParticipant: []gax.CallOption{ 67 gax.WithRetry(func() gax.Retryer { 68 return gax.OnCodes([]codes.Code{ 69 codes.Unavailable, 70 }, gax.Backoff{ 71 Initial: 100 * time.Millisecond, 72 Max: 60000 * time.Millisecond, 73 Multiplier: 1.30, 74 }) 75 }), 76 }, 77 GetParticipant: []gax.CallOption{ 78 gax.WithRetry(func() gax.Retryer { 79 return gax.OnCodes([]codes.Code{ 80 codes.Unavailable, 81 }, gax.Backoff{ 82 Initial: 100 * time.Millisecond, 83 Max: 60000 * time.Millisecond, 84 Multiplier: 1.30, 85 }) 86 }), 87 }, 88 ListParticipants: []gax.CallOption{ 89 gax.WithRetry(func() gax.Retryer { 90 return gax.OnCodes([]codes.Code{ 91 codes.Unavailable, 92 }, gax.Backoff{ 93 Initial: 100 * time.Millisecond, 94 Max: 60000 * time.Millisecond, 95 Multiplier: 1.30, 96 }) 97 }), 98 }, 99 UpdateParticipant: []gax.CallOption{ 100 gax.WithRetry(func() gax.Retryer { 101 return gax.OnCodes([]codes.Code{ 102 codes.Unavailable, 103 }, gax.Backoff{ 104 Initial: 100 * time.Millisecond, 105 Max: 60000 * time.Millisecond, 106 Multiplier: 1.30, 107 }) 108 }), 109 }, 110 AnalyzeContent: []gax.CallOption{ 111 gax.WithRetry(func() gax.Retryer { 112 return gax.OnCodes([]codes.Code{ 113 codes.Unavailable, 114 }, gax.Backoff{ 115 Initial: 100 * time.Millisecond, 116 Max: 60000 * time.Millisecond, 117 Multiplier: 1.30, 118 }) 119 }), 120 }, 121 SuggestArticles: []gax.CallOption{ 122 gax.WithRetry(func() gax.Retryer { 123 return gax.OnCodes([]codes.Code{ 124 codes.Unavailable, 125 }, gax.Backoff{ 126 Initial: 100 * time.Millisecond, 127 Max: 60000 * time.Millisecond, 128 Multiplier: 1.30, 129 }) 130 }), 131 }, 132 SuggestFaqAnswers: []gax.CallOption{ 133 gax.WithRetry(func() gax.Retryer { 134 return gax.OnCodes([]codes.Code{ 135 codes.Unavailable, 136 }, gax.Backoff{ 137 Initial: 100 * time.Millisecond, 138 Max: 60000 * time.Millisecond, 139 Multiplier: 1.30, 140 }) 141 }), 142 }, 143 } 144} 145 146// internalParticipantsClient is an interface that defines the methods availaible from Dialogflow API. 147type internalParticipantsClient interface { 148 Close() error 149 setGoogleClientInfo(...string) 150 Connection() *grpc.ClientConn 151 CreateParticipant(context.Context, *dialogflowpb.CreateParticipantRequest, ...gax.CallOption) (*dialogflowpb.Participant, error) 152 GetParticipant(context.Context, *dialogflowpb.GetParticipantRequest, ...gax.CallOption) (*dialogflowpb.Participant, error) 153 ListParticipants(context.Context, *dialogflowpb.ListParticipantsRequest, ...gax.CallOption) *ParticipantIterator 154 UpdateParticipant(context.Context, *dialogflowpb.UpdateParticipantRequest, ...gax.CallOption) (*dialogflowpb.Participant, error) 155 AnalyzeContent(context.Context, *dialogflowpb.AnalyzeContentRequest, ...gax.CallOption) (*dialogflowpb.AnalyzeContentResponse, error) 156 SuggestArticles(context.Context, *dialogflowpb.SuggestArticlesRequest, ...gax.CallOption) (*dialogflowpb.SuggestArticlesResponse, error) 157 SuggestFaqAnswers(context.Context, *dialogflowpb.SuggestFaqAnswersRequest, ...gax.CallOption) (*dialogflowpb.SuggestFaqAnswersResponse, error) 158} 159 160// ParticipantsClient is a client for interacting with Dialogflow API. 161// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 162// 163// Service for managing Participants. 164type ParticipantsClient struct { 165 // The internal transport-dependent client. 166 internalClient internalParticipantsClient 167 168 // The call options for this service. 169 CallOptions *ParticipantsCallOptions 170} 171 172// Wrapper methods routed to the internal client. 173 174// Close closes the connection to the API service. The user should invoke this when 175// the client is no longer required. 176func (c *ParticipantsClient) Close() error { 177 return c.internalClient.Close() 178} 179 180// setGoogleClientInfo sets the name and version of the application in 181// the `x-goog-api-client` header passed on each request. Intended for 182// use by Google-written clients. 183func (c *ParticipantsClient) setGoogleClientInfo(keyval ...string) { 184 c.internalClient.setGoogleClientInfo(keyval...) 185} 186 187// Connection returns a connection to the API service. 188// 189// Deprecated. 190func (c *ParticipantsClient) Connection() *grpc.ClientConn { 191 return c.internalClient.Connection() 192} 193 194// CreateParticipant creates a new participant in a conversation. 195func (c *ParticipantsClient) CreateParticipant(ctx context.Context, req *dialogflowpb.CreateParticipantRequest, opts ...gax.CallOption) (*dialogflowpb.Participant, error) { 196 return c.internalClient.CreateParticipant(ctx, req, opts...) 197} 198 199// GetParticipant retrieves a conversation participant. 200func (c *ParticipantsClient) GetParticipant(ctx context.Context, req *dialogflowpb.GetParticipantRequest, opts ...gax.CallOption) (*dialogflowpb.Participant, error) { 201 return c.internalClient.GetParticipant(ctx, req, opts...) 202} 203 204// ListParticipants returns the list of all participants in the specified conversation. 205func (c *ParticipantsClient) ListParticipants(ctx context.Context, req *dialogflowpb.ListParticipantsRequest, opts ...gax.CallOption) *ParticipantIterator { 206 return c.internalClient.ListParticipants(ctx, req, opts...) 207} 208 209// UpdateParticipant updates the specified participant. 210func (c *ParticipantsClient) UpdateParticipant(ctx context.Context, req *dialogflowpb.UpdateParticipantRequest, opts ...gax.CallOption) (*dialogflowpb.Participant, error) { 211 return c.internalClient.UpdateParticipant(ctx, req, opts...) 212} 213 214// AnalyzeContent adds a text (chat, for example), or audio (phone recording, for example) 215// message from a participant into the conversation. 216// 217// Note: Always use agent versions for production traffic 218// sent to virtual agents. See Versions and 219// environments (at https://cloud.google.com/dialogflow/es/docs/agents-versions). 220func (c *ParticipantsClient) AnalyzeContent(ctx context.Context, req *dialogflowpb.AnalyzeContentRequest, opts ...gax.CallOption) (*dialogflowpb.AnalyzeContentResponse, error) { 221 return c.internalClient.AnalyzeContent(ctx, req, opts...) 222} 223 224// SuggestArticles gets suggested articles for a participant based on specific historical 225// messages. 226func (c *ParticipantsClient) SuggestArticles(ctx context.Context, req *dialogflowpb.SuggestArticlesRequest, opts ...gax.CallOption) (*dialogflowpb.SuggestArticlesResponse, error) { 227 return c.internalClient.SuggestArticles(ctx, req, opts...) 228} 229 230// SuggestFaqAnswers gets suggested faq answers for a participant based on specific historical 231// messages. 232func (c *ParticipantsClient) SuggestFaqAnswers(ctx context.Context, req *dialogflowpb.SuggestFaqAnswersRequest, opts ...gax.CallOption) (*dialogflowpb.SuggestFaqAnswersResponse, error) { 233 return c.internalClient.SuggestFaqAnswers(ctx, req, opts...) 234} 235 236// participantsGRPCClient is a client for interacting with Dialogflow API over gRPC transport. 237// 238// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 239type participantsGRPCClient struct { 240 // Connection pool of gRPC connections to the service. 241 connPool gtransport.ConnPool 242 243 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 244 disableDeadlines bool 245 246 // Points back to the CallOptions field of the containing ParticipantsClient 247 CallOptions **ParticipantsCallOptions 248 249 // The gRPC API client. 250 participantsClient dialogflowpb.ParticipantsClient 251 252 // The x-goog-* metadata to be sent with each request. 253 xGoogMetadata metadata.MD 254} 255 256// NewParticipantsClient creates a new participants client based on gRPC. 257// The returned client must be Closed when it is done being used to clean up its underlying connections. 258// 259// Service for managing Participants. 260func NewParticipantsClient(ctx context.Context, opts ...option.ClientOption) (*ParticipantsClient, error) { 261 clientOpts := defaultParticipantsGRPCClientOptions() 262 if newParticipantsClientHook != nil { 263 hookOpts, err := newParticipantsClientHook(ctx, clientHookParams{}) 264 if err != nil { 265 return nil, err 266 } 267 clientOpts = append(clientOpts, hookOpts...) 268 } 269 270 disableDeadlines, err := checkDisableDeadlines() 271 if err != nil { 272 return nil, err 273 } 274 275 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 276 if err != nil { 277 return nil, err 278 } 279 client := ParticipantsClient{CallOptions: defaultParticipantsCallOptions()} 280 281 c := &participantsGRPCClient{ 282 connPool: connPool, 283 disableDeadlines: disableDeadlines, 284 participantsClient: dialogflowpb.NewParticipantsClient(connPool), 285 CallOptions: &client.CallOptions, 286 } 287 c.setGoogleClientInfo() 288 289 client.internalClient = c 290 291 return &client, nil 292} 293 294// Connection returns a connection to the API service. 295// 296// Deprecated. 297func (c *participantsGRPCClient) Connection() *grpc.ClientConn { 298 return c.connPool.Conn() 299} 300 301// setGoogleClientInfo sets the name and version of the application in 302// the `x-goog-api-client` header passed on each request. Intended for 303// use by Google-written clients. 304func (c *participantsGRPCClient) setGoogleClientInfo(keyval ...string) { 305 kv := append([]string{"gl-go", versionGo()}, keyval...) 306 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 307 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 308} 309 310// Close closes the connection to the API service. The user should invoke this when 311// the client is no longer required. 312func (c *participantsGRPCClient) Close() error { 313 return c.connPool.Close() 314} 315 316func (c *participantsGRPCClient) CreateParticipant(ctx context.Context, req *dialogflowpb.CreateParticipantRequest, opts ...gax.CallOption) (*dialogflowpb.Participant, error) { 317 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 318 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 319 defer cancel() 320 ctx = cctx 321 } 322 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 323 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 324 opts = append((*c.CallOptions).CreateParticipant[0:len((*c.CallOptions).CreateParticipant):len((*c.CallOptions).CreateParticipant)], opts...) 325 var resp *dialogflowpb.Participant 326 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 327 var err error 328 resp, err = c.participantsClient.CreateParticipant(ctx, req, settings.GRPC...) 329 return err 330 }, opts...) 331 if err != nil { 332 return nil, err 333 } 334 return resp, nil 335} 336 337func (c *participantsGRPCClient) GetParticipant(ctx context.Context, req *dialogflowpb.GetParticipantRequest, opts ...gax.CallOption) (*dialogflowpb.Participant, error) { 338 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 339 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 340 defer cancel() 341 ctx = cctx 342 } 343 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 344 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 345 opts = append((*c.CallOptions).GetParticipant[0:len((*c.CallOptions).GetParticipant):len((*c.CallOptions).GetParticipant)], opts...) 346 var resp *dialogflowpb.Participant 347 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 348 var err error 349 resp, err = c.participantsClient.GetParticipant(ctx, req, settings.GRPC...) 350 return err 351 }, opts...) 352 if err != nil { 353 return nil, err 354 } 355 return resp, nil 356} 357 358func (c *participantsGRPCClient) ListParticipants(ctx context.Context, req *dialogflowpb.ListParticipantsRequest, opts ...gax.CallOption) *ParticipantIterator { 359 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 360 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 361 opts = append((*c.CallOptions).ListParticipants[0:len((*c.CallOptions).ListParticipants):len((*c.CallOptions).ListParticipants)], opts...) 362 it := &ParticipantIterator{} 363 req = proto.Clone(req).(*dialogflowpb.ListParticipantsRequest) 364 it.InternalFetch = func(pageSize int, pageToken string) ([]*dialogflowpb.Participant, string, error) { 365 resp := &dialogflowpb.ListParticipantsResponse{} 366 if pageToken != "" { 367 req.PageToken = pageToken 368 } 369 if pageSize > math.MaxInt32 { 370 req.PageSize = math.MaxInt32 371 } else if pageSize != 0 { 372 req.PageSize = int32(pageSize) 373 } 374 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 375 var err error 376 resp, err = c.participantsClient.ListParticipants(ctx, req, settings.GRPC...) 377 return err 378 }, opts...) 379 if err != nil { 380 return nil, "", err 381 } 382 383 it.Response = resp 384 return resp.GetParticipants(), resp.GetNextPageToken(), nil 385 } 386 fetch := func(pageSize int, pageToken string) (string, error) { 387 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 388 if err != nil { 389 return "", err 390 } 391 it.items = append(it.items, items...) 392 return nextPageToken, nil 393 } 394 395 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 396 it.pageInfo.MaxSize = int(req.GetPageSize()) 397 it.pageInfo.Token = req.GetPageToken() 398 399 return it 400} 401 402func (c *participantsGRPCClient) UpdateParticipant(ctx context.Context, req *dialogflowpb.UpdateParticipantRequest, opts ...gax.CallOption) (*dialogflowpb.Participant, error) { 403 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 404 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 405 defer cancel() 406 ctx = cctx 407 } 408 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "participant.name", url.QueryEscape(req.GetParticipant().GetName()))) 409 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 410 opts = append((*c.CallOptions).UpdateParticipant[0:len((*c.CallOptions).UpdateParticipant):len((*c.CallOptions).UpdateParticipant)], opts...) 411 var resp *dialogflowpb.Participant 412 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 413 var err error 414 resp, err = c.participantsClient.UpdateParticipant(ctx, req, settings.GRPC...) 415 return err 416 }, opts...) 417 if err != nil { 418 return nil, err 419 } 420 return resp, nil 421} 422 423func (c *participantsGRPCClient) AnalyzeContent(ctx context.Context, req *dialogflowpb.AnalyzeContentRequest, opts ...gax.CallOption) (*dialogflowpb.AnalyzeContentResponse, error) { 424 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 425 cctx, cancel := context.WithTimeout(ctx, 220000*time.Millisecond) 426 defer cancel() 427 ctx = cctx 428 } 429 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "participant", url.QueryEscape(req.GetParticipant()))) 430 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 431 opts = append((*c.CallOptions).AnalyzeContent[0:len((*c.CallOptions).AnalyzeContent):len((*c.CallOptions).AnalyzeContent)], opts...) 432 var resp *dialogflowpb.AnalyzeContentResponse 433 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 434 var err error 435 resp, err = c.participantsClient.AnalyzeContent(ctx, req, settings.GRPC...) 436 return err 437 }, opts...) 438 if err != nil { 439 return nil, err 440 } 441 return resp, nil 442} 443 444func (c *participantsGRPCClient) SuggestArticles(ctx context.Context, req *dialogflowpb.SuggestArticlesRequest, opts ...gax.CallOption) (*dialogflowpb.SuggestArticlesResponse, error) { 445 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 446 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 447 defer cancel() 448 ctx = cctx 449 } 450 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 451 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 452 opts = append((*c.CallOptions).SuggestArticles[0:len((*c.CallOptions).SuggestArticles):len((*c.CallOptions).SuggestArticles)], opts...) 453 var resp *dialogflowpb.SuggestArticlesResponse 454 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 455 var err error 456 resp, err = c.participantsClient.SuggestArticles(ctx, req, settings.GRPC...) 457 return err 458 }, opts...) 459 if err != nil { 460 return nil, err 461 } 462 return resp, nil 463} 464 465func (c *participantsGRPCClient) SuggestFaqAnswers(ctx context.Context, req *dialogflowpb.SuggestFaqAnswersRequest, opts ...gax.CallOption) (*dialogflowpb.SuggestFaqAnswersResponse, error) { 466 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 467 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 468 defer cancel() 469 ctx = cctx 470 } 471 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 472 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 473 opts = append((*c.CallOptions).SuggestFaqAnswers[0:len((*c.CallOptions).SuggestFaqAnswers):len((*c.CallOptions).SuggestFaqAnswers)], opts...) 474 var resp *dialogflowpb.SuggestFaqAnswersResponse 475 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 476 var err error 477 resp, err = c.participantsClient.SuggestFaqAnswers(ctx, req, settings.GRPC...) 478 return err 479 }, opts...) 480 if err != nil { 481 return nil, err 482 } 483 return resp, nil 484} 485 486// ParticipantIterator manages a stream of *dialogflowpb.Participant. 487type ParticipantIterator struct { 488 items []*dialogflowpb.Participant 489 pageInfo *iterator.PageInfo 490 nextFunc func() error 491 492 // Response is the raw response for the current page. 493 // It must be cast to the RPC response type. 494 // Calling Next() or InternalFetch() updates this value. 495 Response interface{} 496 497 // InternalFetch is for use by the Google Cloud Libraries only. 498 // It is not part of the stable interface of this package. 499 // 500 // InternalFetch returns results from a single call to the underlying RPC. 501 // The number of results is no greater than pageSize. 502 // If there are no more results, nextPageToken is empty and err is nil. 503 InternalFetch func(pageSize int, pageToken string) (results []*dialogflowpb.Participant, nextPageToken string, err error) 504} 505 506// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 507func (it *ParticipantIterator) PageInfo() *iterator.PageInfo { 508 return it.pageInfo 509} 510 511// Next returns the next result. Its second return value is iterator.Done if there are no more 512// results. Once Next returns Done, all subsequent calls will return Done. 513func (it *ParticipantIterator) Next() (*dialogflowpb.Participant, error) { 514 var item *dialogflowpb.Participant 515 if err := it.nextFunc(); err != nil { 516 return item, err 517 } 518 item = it.items[0] 519 it.items = it.items[1:] 520 return item, nil 521} 522 523func (it *ParticipantIterator) bufLen() int { 524 return len(it.items) 525} 526 527func (it *ParticipantIterator) takeBuf() interface{} { 528 b := it.items 529 it.items = nil 530 return b 531} 532