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 "cloud.google.com/go/longrunning" 27 lroauto "cloud.google.com/go/longrunning/autogen" 28 structpb "github.com/golang/protobuf/ptypes/struct" 29 gax "github.com/googleapis/gax-go/v2" 30 "google.golang.org/api/iterator" 31 "google.golang.org/api/option" 32 "google.golang.org/api/option/internaloption" 33 gtransport "google.golang.org/api/transport/grpc" 34 cxpb "google.golang.org/genproto/googleapis/cloud/dialogflow/cx/v3beta1" 35 longrunningpb "google.golang.org/genproto/googleapis/longrunning" 36 "google.golang.org/grpc" 37 "google.golang.org/grpc/codes" 38 "google.golang.org/grpc/metadata" 39 "google.golang.org/protobuf/proto" 40) 41 42var newFlowsClientHook clientHook 43 44// FlowsCallOptions contains the retry settings for each method of FlowsClient. 45type FlowsCallOptions struct { 46 CreateFlow []gax.CallOption 47 DeleteFlow []gax.CallOption 48 ListFlows []gax.CallOption 49 GetFlow []gax.CallOption 50 UpdateFlow []gax.CallOption 51 TrainFlow []gax.CallOption 52 ValidateFlow []gax.CallOption 53 GetFlowValidationResult []gax.CallOption 54 ImportFlow []gax.CallOption 55 ExportFlow []gax.CallOption 56} 57 58func defaultFlowsGRPCClientOptions() []option.ClientOption { 59 return []option.ClientOption{ 60 internaloption.WithDefaultEndpoint("dialogflow.googleapis.com:443"), 61 internaloption.WithDefaultMTLSEndpoint("dialogflow.mtls.googleapis.com:443"), 62 internaloption.WithDefaultAudience("https://dialogflow.googleapis.com/"), 63 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 64 internaloption.EnableJwtWithScope(), 65 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 66 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 67 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 68 } 69} 70 71func defaultFlowsCallOptions() *FlowsCallOptions { 72 return &FlowsCallOptions{ 73 CreateFlow: []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 DeleteFlow: []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 ListFlows: []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 GetFlow: []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 UpdateFlow: []gax.CallOption{ 118 gax.WithRetry(func() gax.Retryer { 119 return gax.OnCodes([]codes.Code{ 120 codes.Unavailable, 121 }, gax.Backoff{ 122 Initial: 100 * time.Millisecond, 123 Max: 60000 * time.Millisecond, 124 Multiplier: 1.30, 125 }) 126 }), 127 }, 128 TrainFlow: []gax.CallOption{ 129 gax.WithRetry(func() gax.Retryer { 130 return gax.OnCodes([]codes.Code{ 131 codes.Unavailable, 132 }, gax.Backoff{ 133 Initial: 100 * time.Millisecond, 134 Max: 60000 * time.Millisecond, 135 Multiplier: 1.30, 136 }) 137 }), 138 }, 139 ValidateFlow: []gax.CallOption{ 140 gax.WithRetry(func() gax.Retryer { 141 return gax.OnCodes([]codes.Code{ 142 codes.Unavailable, 143 }, gax.Backoff{ 144 Initial: 100 * time.Millisecond, 145 Max: 60000 * time.Millisecond, 146 Multiplier: 1.30, 147 }) 148 }), 149 }, 150 GetFlowValidationResult: []gax.CallOption{ 151 gax.WithRetry(func() gax.Retryer { 152 return gax.OnCodes([]codes.Code{ 153 codes.Unavailable, 154 }, gax.Backoff{ 155 Initial: 100 * time.Millisecond, 156 Max: 60000 * time.Millisecond, 157 Multiplier: 1.30, 158 }) 159 }), 160 }, 161 ImportFlow: []gax.CallOption{ 162 gax.WithRetry(func() gax.Retryer { 163 return gax.OnCodes([]codes.Code{ 164 codes.Unavailable, 165 }, gax.Backoff{ 166 Initial: 100 * time.Millisecond, 167 Max: 60000 * time.Millisecond, 168 Multiplier: 1.30, 169 }) 170 }), 171 }, 172 ExportFlow: []gax.CallOption{ 173 gax.WithRetry(func() gax.Retryer { 174 return gax.OnCodes([]codes.Code{ 175 codes.Unavailable, 176 }, gax.Backoff{ 177 Initial: 100 * time.Millisecond, 178 Max: 60000 * time.Millisecond, 179 Multiplier: 1.30, 180 }) 181 }), 182 }, 183 } 184} 185 186// internalFlowsClient is an interface that defines the methods availaible from Dialogflow API. 187type internalFlowsClient interface { 188 Close() error 189 setGoogleClientInfo(...string) 190 Connection() *grpc.ClientConn 191 CreateFlow(context.Context, *cxpb.CreateFlowRequest, ...gax.CallOption) (*cxpb.Flow, error) 192 DeleteFlow(context.Context, *cxpb.DeleteFlowRequest, ...gax.CallOption) error 193 ListFlows(context.Context, *cxpb.ListFlowsRequest, ...gax.CallOption) *FlowIterator 194 GetFlow(context.Context, *cxpb.GetFlowRequest, ...gax.CallOption) (*cxpb.Flow, error) 195 UpdateFlow(context.Context, *cxpb.UpdateFlowRequest, ...gax.CallOption) (*cxpb.Flow, error) 196 TrainFlow(context.Context, *cxpb.TrainFlowRequest, ...gax.CallOption) (*TrainFlowOperation, error) 197 TrainFlowOperation(name string) *TrainFlowOperation 198 ValidateFlow(context.Context, *cxpb.ValidateFlowRequest, ...gax.CallOption) (*cxpb.FlowValidationResult, error) 199 GetFlowValidationResult(context.Context, *cxpb.GetFlowValidationResultRequest, ...gax.CallOption) (*cxpb.FlowValidationResult, error) 200 ImportFlow(context.Context, *cxpb.ImportFlowRequest, ...gax.CallOption) (*ImportFlowOperation, error) 201 ImportFlowOperation(name string) *ImportFlowOperation 202 ExportFlow(context.Context, *cxpb.ExportFlowRequest, ...gax.CallOption) (*ExportFlowOperation, error) 203 ExportFlowOperation(name string) *ExportFlowOperation 204} 205 206// FlowsClient is a client for interacting with Dialogflow API. 207// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 208// 209// Service for managing Flows. 210type FlowsClient struct { 211 // The internal transport-dependent client. 212 internalClient internalFlowsClient 213 214 // The call options for this service. 215 CallOptions *FlowsCallOptions 216 217 // LROClient is used internally to handle long-running operations. 218 // It is exposed so that its CallOptions can be modified if required. 219 // Users should not Close this client. 220 LROClient *lroauto.OperationsClient 221} 222 223// Wrapper methods routed to the internal client. 224 225// Close closes the connection to the API service. The user should invoke this when 226// the client is no longer required. 227func (c *FlowsClient) Close() error { 228 return c.internalClient.Close() 229} 230 231// setGoogleClientInfo sets the name and version of the application in 232// the `x-goog-api-client` header passed on each request. Intended for 233// use by Google-written clients. 234func (c *FlowsClient) setGoogleClientInfo(keyval ...string) { 235 c.internalClient.setGoogleClientInfo(keyval...) 236} 237 238// Connection returns a connection to the API service. 239// 240// Deprecated. 241func (c *FlowsClient) Connection() *grpc.ClientConn { 242 return c.internalClient.Connection() 243} 244 245// CreateFlow creates a flow in the specified agent. 246// 247// Note: You should always train a flow prior to sending it queries. See the 248// training 249// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training). 250func (c *FlowsClient) CreateFlow(ctx context.Context, req *cxpb.CreateFlowRequest, opts ...gax.CallOption) (*cxpb.Flow, error) { 251 return c.internalClient.CreateFlow(ctx, req, opts...) 252} 253 254// DeleteFlow deletes a specified flow. 255func (c *FlowsClient) DeleteFlow(ctx context.Context, req *cxpb.DeleteFlowRequest, opts ...gax.CallOption) error { 256 return c.internalClient.DeleteFlow(ctx, req, opts...) 257} 258 259// ListFlows returns the list of all flows in the specified agent. 260func (c *FlowsClient) ListFlows(ctx context.Context, req *cxpb.ListFlowsRequest, opts ...gax.CallOption) *FlowIterator { 261 return c.internalClient.ListFlows(ctx, req, opts...) 262} 263 264// GetFlow retrieves the specified flow. 265func (c *FlowsClient) GetFlow(ctx context.Context, req *cxpb.GetFlowRequest, opts ...gax.CallOption) (*cxpb.Flow, error) { 266 return c.internalClient.GetFlow(ctx, req, opts...) 267} 268 269// UpdateFlow updates the specified flow. 270// 271// Note: You should always train a flow prior to sending it queries. See the 272// training 273// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training). 274func (c *FlowsClient) UpdateFlow(ctx context.Context, req *cxpb.UpdateFlowRequest, opts ...gax.CallOption) (*cxpb.Flow, error) { 275 return c.internalClient.UpdateFlow(ctx, req, opts...) 276} 277 278// TrainFlow trains the specified flow. Note that only the flow in ‘draft’ environment 279// is trained. 280// 281// Note: You should always train a flow prior to sending it queries. See the 282// training 283// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training). 284func (c *FlowsClient) TrainFlow(ctx context.Context, req *cxpb.TrainFlowRequest, opts ...gax.CallOption) (*TrainFlowOperation, error) { 285 return c.internalClient.TrainFlow(ctx, req, opts...) 286} 287 288// TrainFlowOperation returns a new TrainFlowOperation from a given name. 289// The name must be that of a previously created TrainFlowOperation, possibly from a different process. 290func (c *FlowsClient) TrainFlowOperation(name string) *TrainFlowOperation { 291 return c.internalClient.TrainFlowOperation(name) 292} 293 294// ValidateFlow validates the specified flow and creates or updates validation results. 295// Please call this API after the training is completed to get the complete 296// validation results. 297func (c *FlowsClient) ValidateFlow(ctx context.Context, req *cxpb.ValidateFlowRequest, opts ...gax.CallOption) (*cxpb.FlowValidationResult, error) { 298 return c.internalClient.ValidateFlow(ctx, req, opts...) 299} 300 301// GetFlowValidationResult gets the latest flow validation result. Flow validation is performed 302// when ValidateFlow is called. 303func (c *FlowsClient) GetFlowValidationResult(ctx context.Context, req *cxpb.GetFlowValidationResultRequest, opts ...gax.CallOption) (*cxpb.FlowValidationResult, error) { 304 return c.internalClient.GetFlowValidationResult(ctx, req, opts...) 305} 306 307// ImportFlow imports the specified flow to the specified agent from a binary file. 308// 309// Note: You should always train a flow prior to sending it queries. See the 310// training 311// documentation (at https://cloud.google.com/dialogflow/cx/docs/concept/training). 312func (c *FlowsClient) ImportFlow(ctx context.Context, req *cxpb.ImportFlowRequest, opts ...gax.CallOption) (*ImportFlowOperation, error) { 313 return c.internalClient.ImportFlow(ctx, req, opts...) 314} 315 316// ImportFlowOperation returns a new ImportFlowOperation from a given name. 317// The name must be that of a previously created ImportFlowOperation, possibly from a different process. 318func (c *FlowsClient) ImportFlowOperation(name string) *ImportFlowOperation { 319 return c.internalClient.ImportFlowOperation(name) 320} 321 322// ExportFlow exports the specified flow to a binary file. 323// 324// Note that resources (e.g. intents, entities, webhooks) that the flow 325// references will also be exported. 326func (c *FlowsClient) ExportFlow(ctx context.Context, req *cxpb.ExportFlowRequest, opts ...gax.CallOption) (*ExportFlowOperation, error) { 327 return c.internalClient.ExportFlow(ctx, req, opts...) 328} 329 330// ExportFlowOperation returns a new ExportFlowOperation from a given name. 331// The name must be that of a previously created ExportFlowOperation, possibly from a different process. 332func (c *FlowsClient) ExportFlowOperation(name string) *ExportFlowOperation { 333 return c.internalClient.ExportFlowOperation(name) 334} 335 336// flowsGRPCClient is a client for interacting with Dialogflow API over gRPC transport. 337// 338// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 339type flowsGRPCClient struct { 340 // Connection pool of gRPC connections to the service. 341 connPool gtransport.ConnPool 342 343 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 344 disableDeadlines bool 345 346 // Points back to the CallOptions field of the containing FlowsClient 347 CallOptions **FlowsCallOptions 348 349 // The gRPC API client. 350 flowsClient cxpb.FlowsClient 351 352 // LROClient is used internally to handle long-running operations. 353 // It is exposed so that its CallOptions can be modified if required. 354 // Users should not Close this client. 355 LROClient **lroauto.OperationsClient 356 357 // The x-goog-* metadata to be sent with each request. 358 xGoogMetadata metadata.MD 359} 360 361// NewFlowsClient creates a new flows client based on gRPC. 362// The returned client must be Closed when it is done being used to clean up its underlying connections. 363// 364// Service for managing Flows. 365func NewFlowsClient(ctx context.Context, opts ...option.ClientOption) (*FlowsClient, error) { 366 clientOpts := defaultFlowsGRPCClientOptions() 367 if newFlowsClientHook != nil { 368 hookOpts, err := newFlowsClientHook(ctx, clientHookParams{}) 369 if err != nil { 370 return nil, err 371 } 372 clientOpts = append(clientOpts, hookOpts...) 373 } 374 375 disableDeadlines, err := checkDisableDeadlines() 376 if err != nil { 377 return nil, err 378 } 379 380 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 381 if err != nil { 382 return nil, err 383 } 384 client := FlowsClient{CallOptions: defaultFlowsCallOptions()} 385 386 c := &flowsGRPCClient{ 387 connPool: connPool, 388 disableDeadlines: disableDeadlines, 389 flowsClient: cxpb.NewFlowsClient(connPool), 390 CallOptions: &client.CallOptions, 391 } 392 c.setGoogleClientInfo() 393 394 client.internalClient = c 395 396 client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) 397 if err != nil { 398 // This error "should not happen", since we are just reusing old connection pool 399 // and never actually need to dial. 400 // If this does happen, we could leak connp. However, we cannot close conn: 401 // If the user invoked the constructor with option.WithGRPCConn, 402 // we would close a connection that's still in use. 403 // TODO: investigate error conditions. 404 return nil, err 405 } 406 c.LROClient = &client.LROClient 407 return &client, nil 408} 409 410// Connection returns a connection to the API service. 411// 412// Deprecated. 413func (c *flowsGRPCClient) Connection() *grpc.ClientConn { 414 return c.connPool.Conn() 415} 416 417// setGoogleClientInfo sets the name and version of the application in 418// the `x-goog-api-client` header passed on each request. Intended for 419// use by Google-written clients. 420func (c *flowsGRPCClient) setGoogleClientInfo(keyval ...string) { 421 kv := append([]string{"gl-go", versionGo()}, keyval...) 422 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 423 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 424} 425 426// Close closes the connection to the API service. The user should invoke this when 427// the client is no longer required. 428func (c *flowsGRPCClient) Close() error { 429 return c.connPool.Close() 430} 431 432func (c *flowsGRPCClient) CreateFlow(ctx context.Context, req *cxpb.CreateFlowRequest, opts ...gax.CallOption) (*cxpb.Flow, error) { 433 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 434 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 435 defer cancel() 436 ctx = cctx 437 } 438 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 439 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 440 opts = append((*c.CallOptions).CreateFlow[0:len((*c.CallOptions).CreateFlow):len((*c.CallOptions).CreateFlow)], opts...) 441 var resp *cxpb.Flow 442 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 443 var err error 444 resp, err = c.flowsClient.CreateFlow(ctx, req, settings.GRPC...) 445 return err 446 }, opts...) 447 if err != nil { 448 return nil, err 449 } 450 return resp, nil 451} 452 453func (c *flowsGRPCClient) DeleteFlow(ctx context.Context, req *cxpb.DeleteFlowRequest, opts ...gax.CallOption) error { 454 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 455 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 456 defer cancel() 457 ctx = cctx 458 } 459 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 460 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 461 opts = append((*c.CallOptions).DeleteFlow[0:len((*c.CallOptions).DeleteFlow):len((*c.CallOptions).DeleteFlow)], opts...) 462 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 463 var err error 464 _, err = c.flowsClient.DeleteFlow(ctx, req, settings.GRPC...) 465 return err 466 }, opts...) 467 return err 468} 469 470func (c *flowsGRPCClient) ListFlows(ctx context.Context, req *cxpb.ListFlowsRequest, opts ...gax.CallOption) *FlowIterator { 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).ListFlows[0:len((*c.CallOptions).ListFlows):len((*c.CallOptions).ListFlows)], opts...) 474 it := &FlowIterator{} 475 req = proto.Clone(req).(*cxpb.ListFlowsRequest) 476 it.InternalFetch = func(pageSize int, pageToken string) ([]*cxpb.Flow, string, error) { 477 resp := &cxpb.ListFlowsResponse{} 478 if pageToken != "" { 479 req.PageToken = pageToken 480 } 481 if pageSize > math.MaxInt32 { 482 req.PageSize = math.MaxInt32 483 } else if pageSize != 0 { 484 req.PageSize = int32(pageSize) 485 } 486 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 487 var err error 488 resp, err = c.flowsClient.ListFlows(ctx, req, settings.GRPC...) 489 return err 490 }, opts...) 491 if err != nil { 492 return nil, "", err 493 } 494 495 it.Response = resp 496 return resp.GetFlows(), resp.GetNextPageToken(), nil 497 } 498 fetch := func(pageSize int, pageToken string) (string, error) { 499 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 500 if err != nil { 501 return "", err 502 } 503 it.items = append(it.items, items...) 504 return nextPageToken, nil 505 } 506 507 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 508 it.pageInfo.MaxSize = int(req.GetPageSize()) 509 it.pageInfo.Token = req.GetPageToken() 510 511 return it 512} 513 514func (c *flowsGRPCClient) GetFlow(ctx context.Context, req *cxpb.GetFlowRequest, opts ...gax.CallOption) (*cxpb.Flow, error) { 515 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 516 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 517 defer cancel() 518 ctx = cctx 519 } 520 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 521 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 522 opts = append((*c.CallOptions).GetFlow[0:len((*c.CallOptions).GetFlow):len((*c.CallOptions).GetFlow)], opts...) 523 var resp *cxpb.Flow 524 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 525 var err error 526 resp, err = c.flowsClient.GetFlow(ctx, req, settings.GRPC...) 527 return err 528 }, opts...) 529 if err != nil { 530 return nil, err 531 } 532 return resp, nil 533} 534 535func (c *flowsGRPCClient) UpdateFlow(ctx context.Context, req *cxpb.UpdateFlowRequest, opts ...gax.CallOption) (*cxpb.Flow, error) { 536 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 537 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 538 defer cancel() 539 ctx = cctx 540 } 541 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "flow.name", url.QueryEscape(req.GetFlow().GetName()))) 542 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 543 opts = append((*c.CallOptions).UpdateFlow[0:len((*c.CallOptions).UpdateFlow):len((*c.CallOptions).UpdateFlow)], opts...) 544 var resp *cxpb.Flow 545 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 546 var err error 547 resp, err = c.flowsClient.UpdateFlow(ctx, req, settings.GRPC...) 548 return err 549 }, opts...) 550 if err != nil { 551 return nil, err 552 } 553 return resp, nil 554} 555 556func (c *flowsGRPCClient) TrainFlow(ctx context.Context, req *cxpb.TrainFlowRequest, opts ...gax.CallOption) (*TrainFlowOperation, error) { 557 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 558 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 559 defer cancel() 560 ctx = cctx 561 } 562 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 563 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 564 opts = append((*c.CallOptions).TrainFlow[0:len((*c.CallOptions).TrainFlow):len((*c.CallOptions).TrainFlow)], opts...) 565 var resp *longrunningpb.Operation 566 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 567 var err error 568 resp, err = c.flowsClient.TrainFlow(ctx, req, settings.GRPC...) 569 return err 570 }, opts...) 571 if err != nil { 572 return nil, err 573 } 574 return &TrainFlowOperation{ 575 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 576 }, nil 577} 578 579func (c *flowsGRPCClient) ValidateFlow(ctx context.Context, req *cxpb.ValidateFlowRequest, opts ...gax.CallOption) (*cxpb.FlowValidationResult, error) { 580 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 581 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 582 defer cancel() 583 ctx = cctx 584 } 585 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 586 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 587 opts = append((*c.CallOptions).ValidateFlow[0:len((*c.CallOptions).ValidateFlow):len((*c.CallOptions).ValidateFlow)], opts...) 588 var resp *cxpb.FlowValidationResult 589 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 590 var err error 591 resp, err = c.flowsClient.ValidateFlow(ctx, req, settings.GRPC...) 592 return err 593 }, opts...) 594 if err != nil { 595 return nil, err 596 } 597 return resp, nil 598} 599 600func (c *flowsGRPCClient) GetFlowValidationResult(ctx context.Context, req *cxpb.GetFlowValidationResultRequest, opts ...gax.CallOption) (*cxpb.FlowValidationResult, error) { 601 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 602 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 603 defer cancel() 604 ctx = cctx 605 } 606 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 607 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 608 opts = append((*c.CallOptions).GetFlowValidationResult[0:len((*c.CallOptions).GetFlowValidationResult):len((*c.CallOptions).GetFlowValidationResult)], opts...) 609 var resp *cxpb.FlowValidationResult 610 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 611 var err error 612 resp, err = c.flowsClient.GetFlowValidationResult(ctx, req, settings.GRPC...) 613 return err 614 }, opts...) 615 if err != nil { 616 return nil, err 617 } 618 return resp, nil 619} 620 621func (c *flowsGRPCClient) ImportFlow(ctx context.Context, req *cxpb.ImportFlowRequest, opts ...gax.CallOption) (*ImportFlowOperation, error) { 622 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 623 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 624 defer cancel() 625 ctx = cctx 626 } 627 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 628 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 629 opts = append((*c.CallOptions).ImportFlow[0:len((*c.CallOptions).ImportFlow):len((*c.CallOptions).ImportFlow)], opts...) 630 var resp *longrunningpb.Operation 631 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 632 var err error 633 resp, err = c.flowsClient.ImportFlow(ctx, req, settings.GRPC...) 634 return err 635 }, opts...) 636 if err != nil { 637 return nil, err 638 } 639 return &ImportFlowOperation{ 640 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 641 }, nil 642} 643 644func (c *flowsGRPCClient) ExportFlow(ctx context.Context, req *cxpb.ExportFlowRequest, opts ...gax.CallOption) (*ExportFlowOperation, error) { 645 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 646 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 647 defer cancel() 648 ctx = cctx 649 } 650 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 651 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 652 opts = append((*c.CallOptions).ExportFlow[0:len((*c.CallOptions).ExportFlow):len((*c.CallOptions).ExportFlow)], opts...) 653 var resp *longrunningpb.Operation 654 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 655 var err error 656 resp, err = c.flowsClient.ExportFlow(ctx, req, settings.GRPC...) 657 return err 658 }, opts...) 659 if err != nil { 660 return nil, err 661 } 662 return &ExportFlowOperation{ 663 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 664 }, nil 665} 666 667// ExportFlowOperation manages a long-running operation from ExportFlow. 668type ExportFlowOperation struct { 669 lro *longrunning.Operation 670} 671 672// ExportFlowOperation returns a new ExportFlowOperation from a given name. 673// The name must be that of a previously created ExportFlowOperation, possibly from a different process. 674func (c *flowsGRPCClient) ExportFlowOperation(name string) *ExportFlowOperation { 675 return &ExportFlowOperation{ 676 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 677 } 678} 679 680// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 681// 682// See documentation of Poll for error-handling information. 683func (op *ExportFlowOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*cxpb.ExportFlowResponse, error) { 684 var resp cxpb.ExportFlowResponse 685 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 686 return nil, err 687 } 688 return &resp, nil 689} 690 691// Poll fetches the latest state of the long-running operation. 692// 693// Poll also fetches the latest metadata, which can be retrieved by Metadata. 694// 695// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 696// the operation has completed with failure, the error is returned and op.Done will return true. 697// If Poll succeeds and the operation has completed successfully, 698// op.Done will return true, and the response of the operation is returned. 699// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 700func (op *ExportFlowOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*cxpb.ExportFlowResponse, error) { 701 var resp cxpb.ExportFlowResponse 702 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 703 return nil, err 704 } 705 if !op.Done() { 706 return nil, nil 707 } 708 return &resp, nil 709} 710 711// Metadata returns metadata associated with the long-running operation. 712// Metadata itself does not contact the server, but Poll does. 713// To get the latest metadata, call this method after a successful call to Poll. 714// If the metadata is not available, the returned metadata and error are both nil. 715func (op *ExportFlowOperation) Metadata() (*structpb.Struct, error) { 716 var meta structpb.Struct 717 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 718 return nil, nil 719 } else if err != nil { 720 return nil, err 721 } 722 return &meta, nil 723} 724 725// Done reports whether the long-running operation has completed. 726func (op *ExportFlowOperation) Done() bool { 727 return op.lro.Done() 728} 729 730// Name returns the name of the long-running operation. 731// The name is assigned by the server and is unique within the service from which the operation is created. 732func (op *ExportFlowOperation) Name() string { 733 return op.lro.Name() 734} 735 736// ImportFlowOperation manages a long-running operation from ImportFlow. 737type ImportFlowOperation struct { 738 lro *longrunning.Operation 739} 740 741// ImportFlowOperation returns a new ImportFlowOperation from a given name. 742// The name must be that of a previously created ImportFlowOperation, possibly from a different process. 743func (c *flowsGRPCClient) ImportFlowOperation(name string) *ImportFlowOperation { 744 return &ImportFlowOperation{ 745 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 746 } 747} 748 749// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 750// 751// See documentation of Poll for error-handling information. 752func (op *ImportFlowOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*cxpb.ImportFlowResponse, error) { 753 var resp cxpb.ImportFlowResponse 754 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 755 return nil, err 756 } 757 return &resp, nil 758} 759 760// Poll fetches the latest state of the long-running operation. 761// 762// Poll also fetches the latest metadata, which can be retrieved by Metadata. 763// 764// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 765// the operation has completed with failure, the error is returned and op.Done will return true. 766// If Poll succeeds and the operation has completed successfully, 767// op.Done will return true, and the response of the operation is returned. 768// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 769func (op *ImportFlowOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*cxpb.ImportFlowResponse, error) { 770 var resp cxpb.ImportFlowResponse 771 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 772 return nil, err 773 } 774 if !op.Done() { 775 return nil, nil 776 } 777 return &resp, nil 778} 779 780// Metadata returns metadata associated with the long-running operation. 781// Metadata itself does not contact the server, but Poll does. 782// To get the latest metadata, call this method after a successful call to Poll. 783// If the metadata is not available, the returned metadata and error are both nil. 784func (op *ImportFlowOperation) Metadata() (*structpb.Struct, error) { 785 var meta structpb.Struct 786 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 787 return nil, nil 788 } else if err != nil { 789 return nil, err 790 } 791 return &meta, nil 792} 793 794// Done reports whether the long-running operation has completed. 795func (op *ImportFlowOperation) Done() bool { 796 return op.lro.Done() 797} 798 799// Name returns the name of the long-running operation. 800// The name is assigned by the server and is unique within the service from which the operation is created. 801func (op *ImportFlowOperation) Name() string { 802 return op.lro.Name() 803} 804 805// TrainFlowOperation manages a long-running operation from TrainFlow. 806type TrainFlowOperation struct { 807 lro *longrunning.Operation 808} 809 810// TrainFlowOperation returns a new TrainFlowOperation from a given name. 811// The name must be that of a previously created TrainFlowOperation, possibly from a different process. 812func (c *flowsGRPCClient) TrainFlowOperation(name string) *TrainFlowOperation { 813 return &TrainFlowOperation{ 814 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 815 } 816} 817 818// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 819// 820// See documentation of Poll for error-handling information. 821func (op *TrainFlowOperation) Wait(ctx context.Context, opts ...gax.CallOption) error { 822 return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...) 823} 824 825// Poll fetches the latest state of the long-running operation. 826// 827// Poll also fetches the latest metadata, which can be retrieved by Metadata. 828// 829// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 830// the operation has completed with failure, the error is returned and op.Done will return true. 831// If Poll succeeds and the operation has completed successfully, 832// op.Done will return true, and the response of the operation is returned. 833// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 834func (op *TrainFlowOperation) Poll(ctx context.Context, opts ...gax.CallOption) error { 835 return op.lro.Poll(ctx, nil, opts...) 836} 837 838// Metadata returns metadata associated with the long-running operation. 839// Metadata itself does not contact the server, but Poll does. 840// To get the latest metadata, call this method after a successful call to Poll. 841// If the metadata is not available, the returned metadata and error are both nil. 842func (op *TrainFlowOperation) Metadata() (*structpb.Struct, error) { 843 var meta structpb.Struct 844 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 845 return nil, nil 846 } else if err != nil { 847 return nil, err 848 } 849 return &meta, nil 850} 851 852// Done reports whether the long-running operation has completed. 853func (op *TrainFlowOperation) Done() bool { 854 return op.lro.Done() 855} 856 857// Name returns the name of the long-running operation. 858// The name is assigned by the server and is unique within the service from which the operation is created. 859func (op *TrainFlowOperation) Name() string { 860 return op.lro.Name() 861} 862 863// FlowIterator manages a stream of *cxpb.Flow. 864type FlowIterator struct { 865 items []*cxpb.Flow 866 pageInfo *iterator.PageInfo 867 nextFunc func() error 868 869 // Response is the raw response for the current page. 870 // It must be cast to the RPC response type. 871 // Calling Next() or InternalFetch() updates this value. 872 Response interface{} 873 874 // InternalFetch is for use by the Google Cloud Libraries only. 875 // It is not part of the stable interface of this package. 876 // 877 // InternalFetch returns results from a single call to the underlying RPC. 878 // The number of results is no greater than pageSize. 879 // If there are no more results, nextPageToken is empty and err is nil. 880 InternalFetch func(pageSize int, pageToken string) (results []*cxpb.Flow, nextPageToken string, err error) 881} 882 883// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 884func (it *FlowIterator) PageInfo() *iterator.PageInfo { 885 return it.pageInfo 886} 887 888// Next returns the next result. Its second return value is iterator.Done if there are no more 889// results. Once Next returns Done, all subsequent calls will return Done. 890func (it *FlowIterator) Next() (*cxpb.Flow, error) { 891 var item *cxpb.Flow 892 if err := it.nextFunc(); err != nil { 893 return item, err 894 } 895 item = it.items[0] 896 it.items = it.items[1:] 897 return item, nil 898} 899 900func (it *FlowIterator) bufLen() int { 901 return len(it.items) 902} 903 904func (it *FlowIterator) takeBuf() interface{} { 905 b := it.items 906 it.items = nil 907 return b 908} 909