cloud.google.com/go/aiplatform@v1.106.0/apiv1beta1/prediction_client.go (about) 1 // Copyright 2025 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 17 package aiplatform 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "fmt" 24 "log/slog" 25 "math" 26 "net/http" 27 "net/url" 28 "time" 29 30 aiplatformpb "cloud.google.com/go/aiplatform/apiv1beta1/aiplatformpb" 31 iampb "cloud.google.com/go/iam/apiv1/iampb" 32 longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb" 33 gax "github.com/googleapis/gax-go/v2" 34 "google.golang.org/api/iterator" 35 "google.golang.org/api/option" 36 "google.golang.org/api/option/internaloption" 37 gtransport "google.golang.org/api/transport/grpc" 38 httptransport "google.golang.org/api/transport/http" 39 httpbodypb "google.golang.org/genproto/googleapis/api/httpbody" 40 locationpb "google.golang.org/genproto/googleapis/cloud/location" 41 "google.golang.org/grpc" 42 "google.golang.org/grpc/metadata" 43 "google.golang.org/protobuf/encoding/protojson" 44 "google.golang.org/protobuf/proto" 45 ) 46 47 var newPredictionClientHook clientHook 48 49 // PredictionCallOptions contains the retry settings for each method of PredictionClient. 50 type PredictionCallOptions struct { 51 Predict []gax.CallOption 52 RawPredict []gax.CallOption 53 StreamRawPredict []gax.CallOption 54 DirectPredict []gax.CallOption 55 DirectRawPredict []gax.CallOption 56 StreamDirectPredict []gax.CallOption 57 StreamDirectRawPredict []gax.CallOption 58 StreamingPredict []gax.CallOption 59 ServerStreamingPredict []gax.CallOption 60 StreamingRawPredict []gax.CallOption 61 Explain []gax.CallOption 62 CountTokens []gax.CallOption 63 GenerateContent []gax.CallOption 64 StreamGenerateContent []gax.CallOption 65 ChatCompletions []gax.CallOption 66 GetLocation []gax.CallOption 67 ListLocations []gax.CallOption 68 GetIamPolicy []gax.CallOption 69 SetIamPolicy []gax.CallOption 70 TestIamPermissions []gax.CallOption 71 CancelOperation []gax.CallOption 72 DeleteOperation []gax.CallOption 73 GetOperation []gax.CallOption 74 ListOperations []gax.CallOption 75 WaitOperation []gax.CallOption 76 } 77 78 func defaultPredictionGRPCClientOptions() []option.ClientOption { 79 return []option.ClientOption{ 80 internaloption.WithDefaultEndpoint("aiplatform.googleapis.com:443"), 81 internaloption.WithDefaultEndpointTemplate("aiplatform.UNIVERSE_DOMAIN:443"), 82 internaloption.WithDefaultMTLSEndpoint("aiplatform.mtls.googleapis.com:443"), 83 internaloption.WithDefaultUniverseDomain("googleapis.com"), 84 internaloption.WithDefaultAudience("https://aiplatform.googleapis.com/"), 85 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 86 internaloption.EnableJwtWithScope(), 87 internaloption.EnableNewAuthLibrary(), 88 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 89 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 90 } 91 } 92 93 func defaultPredictionCallOptions() *PredictionCallOptions { 94 return &PredictionCallOptions{ 95 Predict: []gax.CallOption{ 96 gax.WithTimeout(5000 * time.Millisecond), 97 }, 98 RawPredict: []gax.CallOption{}, 99 StreamRawPredict: []gax.CallOption{}, 100 DirectPredict: []gax.CallOption{}, 101 DirectRawPredict: []gax.CallOption{}, 102 StreamDirectPredict: []gax.CallOption{}, 103 StreamDirectRawPredict: []gax.CallOption{}, 104 StreamingPredict: []gax.CallOption{}, 105 ServerStreamingPredict: []gax.CallOption{}, 106 StreamingRawPredict: []gax.CallOption{}, 107 Explain: []gax.CallOption{ 108 gax.WithTimeout(5000 * time.Millisecond), 109 }, 110 CountTokens: []gax.CallOption{}, 111 GenerateContent: []gax.CallOption{}, 112 StreamGenerateContent: []gax.CallOption{}, 113 ChatCompletions: []gax.CallOption{}, 114 GetLocation: []gax.CallOption{}, 115 ListLocations: []gax.CallOption{}, 116 GetIamPolicy: []gax.CallOption{}, 117 SetIamPolicy: []gax.CallOption{}, 118 TestIamPermissions: []gax.CallOption{}, 119 CancelOperation: []gax.CallOption{}, 120 DeleteOperation: []gax.CallOption{}, 121 GetOperation: []gax.CallOption{}, 122 ListOperations: []gax.CallOption{}, 123 WaitOperation: []gax.CallOption{}, 124 } 125 } 126 127 func defaultPredictionRESTCallOptions() *PredictionCallOptions { 128 return &PredictionCallOptions{ 129 Predict: []gax.CallOption{ 130 gax.WithTimeout(5000 * time.Millisecond), 131 }, 132 RawPredict: []gax.CallOption{}, 133 StreamRawPredict: []gax.CallOption{}, 134 DirectPredict: []gax.CallOption{}, 135 DirectRawPredict: []gax.CallOption{}, 136 StreamDirectPredict: []gax.CallOption{}, 137 StreamDirectRawPredict: []gax.CallOption{}, 138 StreamingPredict: []gax.CallOption{}, 139 ServerStreamingPredict: []gax.CallOption{}, 140 StreamingRawPredict: []gax.CallOption{}, 141 Explain: []gax.CallOption{ 142 gax.WithTimeout(5000 * time.Millisecond), 143 }, 144 CountTokens: []gax.CallOption{}, 145 GenerateContent: []gax.CallOption{}, 146 StreamGenerateContent: []gax.CallOption{}, 147 ChatCompletions: []gax.CallOption{}, 148 GetLocation: []gax.CallOption{}, 149 ListLocations: []gax.CallOption{}, 150 GetIamPolicy: []gax.CallOption{}, 151 SetIamPolicy: []gax.CallOption{}, 152 TestIamPermissions: []gax.CallOption{}, 153 CancelOperation: []gax.CallOption{}, 154 DeleteOperation: []gax.CallOption{}, 155 GetOperation: []gax.CallOption{}, 156 ListOperations: []gax.CallOption{}, 157 WaitOperation: []gax.CallOption{}, 158 } 159 } 160 161 // internalPredictionClient is an interface that defines the methods available from Vertex AI API. 162 type internalPredictionClient interface { 163 Close() error 164 setGoogleClientInfo(...string) 165 Connection() *grpc.ClientConn 166 Predict(context.Context, *aiplatformpb.PredictRequest, ...gax.CallOption) (*aiplatformpb.PredictResponse, error) 167 RawPredict(context.Context, *aiplatformpb.RawPredictRequest, ...gax.CallOption) (*httpbodypb.HttpBody, error) 168 StreamRawPredict(context.Context, *aiplatformpb.StreamRawPredictRequest, ...gax.CallOption) (aiplatformpb.PredictionService_StreamRawPredictClient, error) 169 DirectPredict(context.Context, *aiplatformpb.DirectPredictRequest, ...gax.CallOption) (*aiplatformpb.DirectPredictResponse, error) 170 DirectRawPredict(context.Context, *aiplatformpb.DirectRawPredictRequest, ...gax.CallOption) (*aiplatformpb.DirectRawPredictResponse, error) 171 StreamDirectPredict(context.Context, ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectPredictClient, error) 172 StreamDirectRawPredict(context.Context, ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectRawPredictClient, error) 173 StreamingPredict(context.Context, ...gax.CallOption) (aiplatformpb.PredictionService_StreamingPredictClient, error) 174 ServerStreamingPredict(context.Context, *aiplatformpb.StreamingPredictRequest, ...gax.CallOption) (aiplatformpb.PredictionService_ServerStreamingPredictClient, error) 175 StreamingRawPredict(context.Context, ...gax.CallOption) (aiplatformpb.PredictionService_StreamingRawPredictClient, error) 176 Explain(context.Context, *aiplatformpb.ExplainRequest, ...gax.CallOption) (*aiplatformpb.ExplainResponse, error) 177 CountTokens(context.Context, *aiplatformpb.CountTokensRequest, ...gax.CallOption) (*aiplatformpb.CountTokensResponse, error) 178 GenerateContent(context.Context, *aiplatformpb.GenerateContentRequest, ...gax.CallOption) (*aiplatformpb.GenerateContentResponse, error) 179 StreamGenerateContent(context.Context, *aiplatformpb.GenerateContentRequest, ...gax.CallOption) (aiplatformpb.PredictionService_StreamGenerateContentClient, error) 180 ChatCompletions(context.Context, *aiplatformpb.ChatCompletionsRequest, ...gax.CallOption) (aiplatformpb.PredictionService_ChatCompletionsClient, error) 181 GetLocation(context.Context, *locationpb.GetLocationRequest, ...gax.CallOption) (*locationpb.Location, error) 182 ListLocations(context.Context, *locationpb.ListLocationsRequest, ...gax.CallOption) *LocationIterator 183 GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error) 184 SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error) 185 TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) 186 CancelOperation(context.Context, *longrunningpb.CancelOperationRequest, ...gax.CallOption) error 187 DeleteOperation(context.Context, *longrunningpb.DeleteOperationRequest, ...gax.CallOption) error 188 GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) 189 ListOperations(context.Context, *longrunningpb.ListOperationsRequest, ...gax.CallOption) *OperationIterator 190 WaitOperation(context.Context, *longrunningpb.WaitOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) 191 } 192 193 // PredictionClient is a client for interacting with Vertex AI API. 194 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 195 // 196 // A service for online predictions and explanations. 197 type PredictionClient struct { 198 // The internal transport-dependent client. 199 internalClient internalPredictionClient 200 201 // The call options for this service. 202 CallOptions *PredictionCallOptions 203 } 204 205 // Wrapper methods routed to the internal client. 206 207 // Close closes the connection to the API service. The user should invoke this when 208 // the client is no longer required. 209 func (c *PredictionClient) Close() error { 210 return c.internalClient.Close() 211 } 212 213 // setGoogleClientInfo sets the name and version of the application in 214 // the `x-goog-api-client` header passed on each request. Intended for 215 // use by Google-written clients. 216 func (c *PredictionClient) setGoogleClientInfo(keyval ...string) { 217 c.internalClient.setGoogleClientInfo(keyval...) 218 } 219 220 // Connection returns a connection to the API service. 221 // 222 // Deprecated: Connections are now pooled so this method does not always 223 // return the same resource. 224 func (c *PredictionClient) Connection() *grpc.ClientConn { 225 return c.internalClient.Connection() 226 } 227 228 // Predict perform an online prediction. 229 func (c *PredictionClient) Predict(ctx context.Context, req *aiplatformpb.PredictRequest, opts ...gax.CallOption) (*aiplatformpb.PredictResponse, error) { 230 return c.internalClient.Predict(ctx, req, opts...) 231 } 232 233 // RawPredict perform an online prediction with an arbitrary HTTP payload. 234 // 235 // The response includes the following HTTP headers: 236 // 237 // X-Vertex-AI-Endpoint-Id: ID of the 238 // Endpoint that served this 239 // prediction. 240 // 241 // X-Vertex-AI-Deployed-Model-Id: ID of the Endpoint’s 242 // DeployedModel that served 243 // this prediction. 244 func (c *PredictionClient) RawPredict(ctx context.Context, req *aiplatformpb.RawPredictRequest, opts ...gax.CallOption) (*httpbodypb.HttpBody, error) { 245 return c.internalClient.RawPredict(ctx, req, opts...) 246 } 247 248 // StreamRawPredict perform a streaming online prediction with an arbitrary HTTP payload. 249 func (c *PredictionClient) StreamRawPredict(ctx context.Context, req *aiplatformpb.StreamRawPredictRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamRawPredictClient, error) { 250 return c.internalClient.StreamRawPredict(ctx, req, opts...) 251 } 252 253 // DirectPredict perform an unary online prediction request to a gRPC model server for 254 // Vertex first-party products and frameworks. 255 func (c *PredictionClient) DirectPredict(ctx context.Context, req *aiplatformpb.DirectPredictRequest, opts ...gax.CallOption) (*aiplatformpb.DirectPredictResponse, error) { 256 return c.internalClient.DirectPredict(ctx, req, opts...) 257 } 258 259 // DirectRawPredict perform an unary online prediction request to a gRPC model server for 260 // custom containers. 261 func (c *PredictionClient) DirectRawPredict(ctx context.Context, req *aiplatformpb.DirectRawPredictRequest, opts ...gax.CallOption) (*aiplatformpb.DirectRawPredictResponse, error) { 262 return c.internalClient.DirectRawPredict(ctx, req, opts...) 263 } 264 265 // StreamDirectPredict perform a streaming online prediction request to a gRPC model server for 266 // Vertex first-party products and frameworks. 267 // 268 // This method is not supported for the REST transport. 269 func (c *PredictionClient) StreamDirectPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectPredictClient, error) { 270 return c.internalClient.StreamDirectPredict(ctx, opts...) 271 } 272 273 // StreamDirectRawPredict perform a streaming online prediction request to a gRPC model server for 274 // custom containers. 275 // 276 // This method is not supported for the REST transport. 277 func (c *PredictionClient) StreamDirectRawPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectRawPredictClient, error) { 278 return c.internalClient.StreamDirectRawPredict(ctx, opts...) 279 } 280 281 // StreamingPredict perform a streaming online prediction request for Vertex first-party 282 // products and frameworks. 283 // 284 // This method is not supported for the REST transport. 285 func (c *PredictionClient) StreamingPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamingPredictClient, error) { 286 return c.internalClient.StreamingPredict(ctx, opts...) 287 } 288 289 // ServerStreamingPredict perform a server-side streaming online prediction request for Vertex 290 // LLM streaming. 291 func (c *PredictionClient) ServerStreamingPredict(ctx context.Context, req *aiplatformpb.StreamingPredictRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_ServerStreamingPredictClient, error) { 292 return c.internalClient.ServerStreamingPredict(ctx, req, opts...) 293 } 294 295 // StreamingRawPredict perform a streaming online prediction request through gRPC. 296 // 297 // This method is not supported for the REST transport. 298 func (c *PredictionClient) StreamingRawPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamingRawPredictClient, error) { 299 return c.internalClient.StreamingRawPredict(ctx, opts...) 300 } 301 302 // Explain perform an online explanation. 303 // 304 // If 305 // deployed_model_id 306 // is specified, the corresponding DeployModel must have 307 // explanation_spec 308 // populated. If 309 // deployed_model_id 310 // is not specified, all DeployedModels must have 311 // explanation_spec 312 // populated. 313 func (c *PredictionClient) Explain(ctx context.Context, req *aiplatformpb.ExplainRequest, opts ...gax.CallOption) (*aiplatformpb.ExplainResponse, error) { 314 return c.internalClient.Explain(ctx, req, opts...) 315 } 316 317 // CountTokens perform a token counting. 318 func (c *PredictionClient) CountTokens(ctx context.Context, req *aiplatformpb.CountTokensRequest, opts ...gax.CallOption) (*aiplatformpb.CountTokensResponse, error) { 319 return c.internalClient.CountTokens(ctx, req, opts...) 320 } 321 322 // GenerateContent generate content with multimodal inputs. 323 func (c *PredictionClient) GenerateContent(ctx context.Context, req *aiplatformpb.GenerateContentRequest, opts ...gax.CallOption) (*aiplatformpb.GenerateContentResponse, error) { 324 return c.internalClient.GenerateContent(ctx, req, opts...) 325 } 326 327 // StreamGenerateContent generate content with multimodal inputs with streaming support. 328 func (c *PredictionClient) StreamGenerateContent(ctx context.Context, req *aiplatformpb.GenerateContentRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamGenerateContentClient, error) { 329 return c.internalClient.StreamGenerateContent(ctx, req, opts...) 330 } 331 332 // ChatCompletions exposes an OpenAI-compatible endpoint for chat completions. 333 func (c *PredictionClient) ChatCompletions(ctx context.Context, req *aiplatformpb.ChatCompletionsRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_ChatCompletionsClient, error) { 334 return c.internalClient.ChatCompletions(ctx, req, opts...) 335 } 336 337 // GetLocation gets information about a location. 338 func (c *PredictionClient) GetLocation(ctx context.Context, req *locationpb.GetLocationRequest, opts ...gax.CallOption) (*locationpb.Location, error) { 339 return c.internalClient.GetLocation(ctx, req, opts...) 340 } 341 342 // ListLocations lists information about the supported locations for this service. 343 func (c *PredictionClient) ListLocations(ctx context.Context, req *locationpb.ListLocationsRequest, opts ...gax.CallOption) *LocationIterator { 344 return c.internalClient.ListLocations(ctx, req, opts...) 345 } 346 347 // GetIamPolicy gets the access control policy for a resource. Returns an empty policy 348 // if the resource exists and does not have a policy set. 349 func (c *PredictionClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 350 return c.internalClient.GetIamPolicy(ctx, req, opts...) 351 } 352 353 // SetIamPolicy sets the access control policy on the specified resource. Replaces 354 // any existing policy. 355 // 356 // Can return NOT_FOUND, INVALID_ARGUMENT, and PERMISSION_DENIED 357 // errors. 358 func (c *PredictionClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 359 return c.internalClient.SetIamPolicy(ctx, req, opts...) 360 } 361 362 // TestIamPermissions returns permissions that a caller has on the specified resource. If the 363 // resource does not exist, this will return an empty set of 364 // permissions, not a NOT_FOUND error. 365 // 366 // Note: This operation is designed to be used for building 367 // permission-aware UIs and command-line tools, not for authorization 368 // checking. This operation may “fail open” without warning. 369 func (c *PredictionClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) { 370 return c.internalClient.TestIamPermissions(ctx, req, opts...) 371 } 372 373 // CancelOperation is a utility method from google.longrunning.Operations. 374 func (c *PredictionClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error { 375 return c.internalClient.CancelOperation(ctx, req, opts...) 376 } 377 378 // DeleteOperation is a utility method from google.longrunning.Operations. 379 func (c *PredictionClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error { 380 return c.internalClient.DeleteOperation(ctx, req, opts...) 381 } 382 383 // GetOperation is a utility method from google.longrunning.Operations. 384 func (c *PredictionClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { 385 return c.internalClient.GetOperation(ctx, req, opts...) 386 } 387 388 // ListOperations is a utility method from google.longrunning.Operations. 389 func (c *PredictionClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator { 390 return c.internalClient.ListOperations(ctx, req, opts...) 391 } 392 393 // WaitOperation is a utility method from google.longrunning.Operations. 394 func (c *PredictionClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { 395 return c.internalClient.WaitOperation(ctx, req, opts...) 396 } 397 398 // predictionGRPCClient is a client for interacting with Vertex AI API over gRPC transport. 399 // 400 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 401 type predictionGRPCClient struct { 402 // Connection pool of gRPC connections to the service. 403 connPool gtransport.ConnPool 404 405 // Points back to the CallOptions field of the containing PredictionClient 406 CallOptions **PredictionCallOptions 407 408 // The gRPC API client. 409 predictionClient aiplatformpb.PredictionServiceClient 410 411 operationsClient longrunningpb.OperationsClient 412 413 iamPolicyClient iampb.IAMPolicyClient 414 415 locationsClient locationpb.LocationsClient 416 417 // The x-goog-* metadata to be sent with each request. 418 xGoogHeaders []string 419 420 logger *slog.Logger 421 } 422 423 // NewPredictionClient creates a new prediction service client based on gRPC. 424 // The returned client must be Closed when it is done being used to clean up its underlying connections. 425 // 426 // A service for online predictions and explanations. 427 func NewPredictionClient(ctx context.Context, opts ...option.ClientOption) (*PredictionClient, error) { 428 clientOpts := defaultPredictionGRPCClientOptions() 429 if newPredictionClientHook != nil { 430 hookOpts, err := newPredictionClientHook(ctx, clientHookParams{}) 431 if err != nil { 432 return nil, err 433 } 434 clientOpts = append(clientOpts, hookOpts...) 435 } 436 437 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 438 if err != nil { 439 return nil, err 440 } 441 client := PredictionClient{CallOptions: defaultPredictionCallOptions()} 442 443 c := &predictionGRPCClient{ 444 connPool: connPool, 445 predictionClient: aiplatformpb.NewPredictionServiceClient(connPool), 446 CallOptions: &client.CallOptions, 447 logger: internaloption.GetLogger(opts), 448 operationsClient: longrunningpb.NewOperationsClient(connPool), 449 iamPolicyClient: iampb.NewIAMPolicyClient(connPool), 450 locationsClient: locationpb.NewLocationsClient(connPool), 451 } 452 c.setGoogleClientInfo() 453 454 client.internalClient = c 455 456 return &client, nil 457 } 458 459 // Connection returns a connection to the API service. 460 // 461 // Deprecated: Connections are now pooled so this method does not always 462 // return the same resource. 463 func (c *predictionGRPCClient) Connection() *grpc.ClientConn { 464 return c.connPool.Conn() 465 } 466 467 // setGoogleClientInfo sets the name and version of the application in 468 // the `x-goog-api-client` header passed on each request. Intended for 469 // use by Google-written clients. 470 func (c *predictionGRPCClient) setGoogleClientInfo(keyval ...string) { 471 kv := append([]string{"gl-go", gax.GoVersion}, keyval...) 472 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion) 473 c.xGoogHeaders = []string{ 474 "x-goog-api-client", gax.XGoogHeader(kv...), 475 } 476 } 477 478 // Close closes the connection to the API service. The user should invoke this when 479 // the client is no longer required. 480 func (c *predictionGRPCClient) Close() error { 481 return c.connPool.Close() 482 } 483 484 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 485 type predictionRESTClient struct { 486 // The http endpoint to connect to. 487 endpoint string 488 489 // The http client. 490 httpClient *http.Client 491 492 // The x-goog-* headers to be sent with each request. 493 xGoogHeaders []string 494 495 // Points back to the CallOptions field of the containing PredictionClient 496 CallOptions **PredictionCallOptions 497 498 logger *slog.Logger 499 } 500 501 // NewPredictionRESTClient creates a new prediction service rest client. 502 // 503 // A service for online predictions and explanations. 504 func NewPredictionRESTClient(ctx context.Context, opts ...option.ClientOption) (*PredictionClient, error) { 505 clientOpts := append(defaultPredictionRESTClientOptions(), opts...) 506 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...) 507 if err != nil { 508 return nil, err 509 } 510 511 callOpts := defaultPredictionRESTCallOptions() 512 c := &predictionRESTClient{ 513 endpoint: endpoint, 514 httpClient: httpClient, 515 CallOptions: &callOpts, 516 logger: internaloption.GetLogger(opts), 517 } 518 c.setGoogleClientInfo() 519 520 return &PredictionClient{internalClient: c, CallOptions: callOpts}, nil 521 } 522 523 func defaultPredictionRESTClientOptions() []option.ClientOption { 524 return []option.ClientOption{ 525 internaloption.WithDefaultEndpoint("https://aiplatform.googleapis.com"), 526 internaloption.WithDefaultEndpointTemplate("https://aiplatform.UNIVERSE_DOMAIN"), 527 internaloption.WithDefaultMTLSEndpoint("https://aiplatform.mtls.googleapis.com"), 528 internaloption.WithDefaultUniverseDomain("googleapis.com"), 529 internaloption.WithDefaultAudience("https://aiplatform.googleapis.com/"), 530 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 531 internaloption.EnableNewAuthLibrary(), 532 } 533 } 534 535 // setGoogleClientInfo sets the name and version of the application in 536 // the `x-goog-api-client` header passed on each request. Intended for 537 // use by Google-written clients. 538 func (c *predictionRESTClient) setGoogleClientInfo(keyval ...string) { 539 kv := append([]string{"gl-go", gax.GoVersion}, keyval...) 540 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion) 541 c.xGoogHeaders = []string{ 542 "x-goog-api-client", gax.XGoogHeader(kv...), 543 } 544 } 545 546 // Close closes the connection to the API service. The user should invoke this when 547 // the client is no longer required. 548 func (c *predictionRESTClient) Close() error { 549 // Replace httpClient with nil to force cleanup. 550 c.httpClient = nil 551 return nil 552 } 553 554 // Connection returns a connection to the API service. 555 // 556 // Deprecated: This method always returns nil. 557 func (c *predictionRESTClient) Connection() *grpc.ClientConn { 558 return nil 559 } 560 func (c *predictionGRPCClient) Predict(ctx context.Context, req *aiplatformpb.PredictRequest, opts ...gax.CallOption) (*aiplatformpb.PredictResponse, error) { 561 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 562 563 hds = append(c.xGoogHeaders, hds...) 564 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 565 opts = append((*c.CallOptions).Predict[0:len((*c.CallOptions).Predict):len((*c.CallOptions).Predict)], opts...) 566 var resp *aiplatformpb.PredictResponse 567 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 568 var err error 569 resp, err = executeRPC(ctx, c.predictionClient.Predict, req, settings.GRPC, c.logger, "Predict") 570 return err 571 }, opts...) 572 if err != nil { 573 return nil, err 574 } 575 return resp, nil 576 } 577 578 func (c *predictionGRPCClient) RawPredict(ctx context.Context, req *aiplatformpb.RawPredictRequest, opts ...gax.CallOption) (*httpbodypb.HttpBody, error) { 579 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 580 581 hds = append(c.xGoogHeaders, hds...) 582 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 583 opts = append((*c.CallOptions).RawPredict[0:len((*c.CallOptions).RawPredict):len((*c.CallOptions).RawPredict)], opts...) 584 var resp *httpbodypb.HttpBody 585 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 586 var err error 587 resp, err = executeRPC(ctx, c.predictionClient.RawPredict, req, settings.GRPC, c.logger, "RawPredict") 588 return err 589 }, opts...) 590 if err != nil { 591 return nil, err 592 } 593 return resp, nil 594 } 595 596 func (c *predictionGRPCClient) StreamRawPredict(ctx context.Context, req *aiplatformpb.StreamRawPredictRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamRawPredictClient, error) { 597 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 598 599 hds = append(c.xGoogHeaders, hds...) 600 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 601 opts = append((*c.CallOptions).StreamRawPredict[0:len((*c.CallOptions).StreamRawPredict):len((*c.CallOptions).StreamRawPredict)], opts...) 602 var resp aiplatformpb.PredictionService_StreamRawPredictClient 603 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 604 var err error 605 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamRawPredict") 606 resp, err = c.predictionClient.StreamRawPredict(ctx, req, settings.GRPC...) 607 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamRawPredict") 608 return err 609 }, opts...) 610 if err != nil { 611 return nil, err 612 } 613 return resp, nil 614 } 615 616 func (c *predictionGRPCClient) DirectPredict(ctx context.Context, req *aiplatformpb.DirectPredictRequest, opts ...gax.CallOption) (*aiplatformpb.DirectPredictResponse, error) { 617 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 618 619 hds = append(c.xGoogHeaders, hds...) 620 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 621 opts = append((*c.CallOptions).DirectPredict[0:len((*c.CallOptions).DirectPredict):len((*c.CallOptions).DirectPredict)], opts...) 622 var resp *aiplatformpb.DirectPredictResponse 623 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 624 var err error 625 resp, err = executeRPC(ctx, c.predictionClient.DirectPredict, req, settings.GRPC, c.logger, "DirectPredict") 626 return err 627 }, opts...) 628 if err != nil { 629 return nil, err 630 } 631 return resp, nil 632 } 633 634 func (c *predictionGRPCClient) DirectRawPredict(ctx context.Context, req *aiplatformpb.DirectRawPredictRequest, opts ...gax.CallOption) (*aiplatformpb.DirectRawPredictResponse, error) { 635 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 636 637 hds = append(c.xGoogHeaders, hds...) 638 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 639 opts = append((*c.CallOptions).DirectRawPredict[0:len((*c.CallOptions).DirectRawPredict):len((*c.CallOptions).DirectRawPredict)], opts...) 640 var resp *aiplatformpb.DirectRawPredictResponse 641 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 642 var err error 643 resp, err = executeRPC(ctx, c.predictionClient.DirectRawPredict, req, settings.GRPC, c.logger, "DirectRawPredict") 644 return err 645 }, opts...) 646 if err != nil { 647 return nil, err 648 } 649 return resp, nil 650 } 651 652 func (c *predictionGRPCClient) StreamDirectPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectPredictClient, error) { 653 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...) 654 var resp aiplatformpb.PredictionService_StreamDirectPredictClient 655 opts = append((*c.CallOptions).StreamDirectPredict[0:len((*c.CallOptions).StreamDirectPredict):len((*c.CallOptions).StreamDirectPredict)], opts...) 656 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 657 var err error 658 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamDirectPredict") 659 resp, err = c.predictionClient.StreamDirectPredict(ctx, settings.GRPC...) 660 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamDirectPredict") 661 return err 662 }, opts...) 663 if err != nil { 664 return nil, err 665 } 666 return resp, nil 667 } 668 669 func (c *predictionGRPCClient) StreamDirectRawPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectRawPredictClient, error) { 670 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...) 671 var resp aiplatformpb.PredictionService_StreamDirectRawPredictClient 672 opts = append((*c.CallOptions).StreamDirectRawPredict[0:len((*c.CallOptions).StreamDirectRawPredict):len((*c.CallOptions).StreamDirectRawPredict)], opts...) 673 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 674 var err error 675 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamDirectRawPredict") 676 resp, err = c.predictionClient.StreamDirectRawPredict(ctx, settings.GRPC...) 677 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamDirectRawPredict") 678 return err 679 }, opts...) 680 if err != nil { 681 return nil, err 682 } 683 return resp, nil 684 } 685 686 func (c *predictionGRPCClient) StreamingPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamingPredictClient, error) { 687 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...) 688 var resp aiplatformpb.PredictionService_StreamingPredictClient 689 opts = append((*c.CallOptions).StreamingPredict[0:len((*c.CallOptions).StreamingPredict):len((*c.CallOptions).StreamingPredict)], opts...) 690 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 691 var err error 692 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamingPredict") 693 resp, err = c.predictionClient.StreamingPredict(ctx, settings.GRPC...) 694 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamingPredict") 695 return err 696 }, opts...) 697 if err != nil { 698 return nil, err 699 } 700 return resp, nil 701 } 702 703 func (c *predictionGRPCClient) ServerStreamingPredict(ctx context.Context, req *aiplatformpb.StreamingPredictRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_ServerStreamingPredictClient, error) { 704 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 705 706 hds = append(c.xGoogHeaders, hds...) 707 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 708 opts = append((*c.CallOptions).ServerStreamingPredict[0:len((*c.CallOptions).ServerStreamingPredict):len((*c.CallOptions).ServerStreamingPredict)], opts...) 709 var resp aiplatformpb.PredictionService_ServerStreamingPredictClient 710 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 711 var err error 712 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "ServerStreamingPredict") 713 resp, err = c.predictionClient.ServerStreamingPredict(ctx, req, settings.GRPC...) 714 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "ServerStreamingPredict") 715 return err 716 }, opts...) 717 if err != nil { 718 return nil, err 719 } 720 return resp, nil 721 } 722 723 func (c *predictionGRPCClient) StreamingRawPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamingRawPredictClient, error) { 724 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...) 725 var resp aiplatformpb.PredictionService_StreamingRawPredictClient 726 opts = append((*c.CallOptions).StreamingRawPredict[0:len((*c.CallOptions).StreamingRawPredict):len((*c.CallOptions).StreamingRawPredict)], opts...) 727 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 728 var err error 729 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamingRawPredict") 730 resp, err = c.predictionClient.StreamingRawPredict(ctx, settings.GRPC...) 731 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamingRawPredict") 732 return err 733 }, opts...) 734 if err != nil { 735 return nil, err 736 } 737 return resp, nil 738 } 739 740 func (c *predictionGRPCClient) Explain(ctx context.Context, req *aiplatformpb.ExplainRequest, opts ...gax.CallOption) (*aiplatformpb.ExplainResponse, error) { 741 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 742 743 hds = append(c.xGoogHeaders, hds...) 744 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 745 opts = append((*c.CallOptions).Explain[0:len((*c.CallOptions).Explain):len((*c.CallOptions).Explain)], opts...) 746 var resp *aiplatformpb.ExplainResponse 747 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 748 var err error 749 resp, err = executeRPC(ctx, c.predictionClient.Explain, req, settings.GRPC, c.logger, "Explain") 750 return err 751 }, opts...) 752 if err != nil { 753 return nil, err 754 } 755 return resp, nil 756 } 757 758 func (c *predictionGRPCClient) CountTokens(ctx context.Context, req *aiplatformpb.CountTokensRequest, opts ...gax.CallOption) (*aiplatformpb.CountTokensResponse, error) { 759 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 760 761 hds = append(c.xGoogHeaders, hds...) 762 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 763 opts = append((*c.CallOptions).CountTokens[0:len((*c.CallOptions).CountTokens):len((*c.CallOptions).CountTokens)], opts...) 764 var resp *aiplatformpb.CountTokensResponse 765 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 766 var err error 767 resp, err = executeRPC(ctx, c.predictionClient.CountTokens, req, settings.GRPC, c.logger, "CountTokens") 768 return err 769 }, opts...) 770 if err != nil { 771 return nil, err 772 } 773 return resp, nil 774 } 775 776 func (c *predictionGRPCClient) GenerateContent(ctx context.Context, req *aiplatformpb.GenerateContentRequest, opts ...gax.CallOption) (*aiplatformpb.GenerateContentResponse, error) { 777 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))} 778 779 hds = append(c.xGoogHeaders, hds...) 780 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 781 opts = append((*c.CallOptions).GenerateContent[0:len((*c.CallOptions).GenerateContent):len((*c.CallOptions).GenerateContent)], opts...) 782 var resp *aiplatformpb.GenerateContentResponse 783 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 784 var err error 785 resp, err = executeRPC(ctx, c.predictionClient.GenerateContent, req, settings.GRPC, c.logger, "GenerateContent") 786 return err 787 }, opts...) 788 if err != nil { 789 return nil, err 790 } 791 return resp, nil 792 } 793 794 func (c *predictionGRPCClient) StreamGenerateContent(ctx context.Context, req *aiplatformpb.GenerateContentRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamGenerateContentClient, error) { 795 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))} 796 797 hds = append(c.xGoogHeaders, hds...) 798 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 799 opts = append((*c.CallOptions).StreamGenerateContent[0:len((*c.CallOptions).StreamGenerateContent):len((*c.CallOptions).StreamGenerateContent)], opts...) 800 var resp aiplatformpb.PredictionService_StreamGenerateContentClient 801 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 802 var err error 803 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "StreamGenerateContent") 804 resp, err = c.predictionClient.StreamGenerateContent(ctx, req, settings.GRPC...) 805 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "StreamGenerateContent") 806 return err 807 }, opts...) 808 if err != nil { 809 return nil, err 810 } 811 return resp, nil 812 } 813 814 func (c *predictionGRPCClient) ChatCompletions(ctx context.Context, req *aiplatformpb.ChatCompletionsRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_ChatCompletionsClient, error) { 815 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 816 817 hds = append(c.xGoogHeaders, hds...) 818 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 819 opts = append((*c.CallOptions).ChatCompletions[0:len((*c.CallOptions).ChatCompletions):len((*c.CallOptions).ChatCompletions)], opts...) 820 var resp aiplatformpb.PredictionService_ChatCompletionsClient 821 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 822 var err error 823 c.logger.DebugContext(ctx, "api streaming client request", "serviceName", serviceName, "rpcName", "ChatCompletions") 824 resp, err = c.predictionClient.ChatCompletions(ctx, req, settings.GRPC...) 825 c.logger.DebugContext(ctx, "api streaming client response", "serviceName", serviceName, "rpcName", "ChatCompletions") 826 return err 827 }, opts...) 828 if err != nil { 829 return nil, err 830 } 831 return resp, nil 832 } 833 834 func (c *predictionGRPCClient) GetLocation(ctx context.Context, req *locationpb.GetLocationRequest, opts ...gax.CallOption) (*locationpb.Location, error) { 835 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 836 837 hds = append(c.xGoogHeaders, hds...) 838 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 839 opts = append((*c.CallOptions).GetLocation[0:len((*c.CallOptions).GetLocation):len((*c.CallOptions).GetLocation)], opts...) 840 var resp *locationpb.Location 841 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 842 var err error 843 resp, err = executeRPC(ctx, c.locationsClient.GetLocation, req, settings.GRPC, c.logger, "GetLocation") 844 return err 845 }, opts...) 846 if err != nil { 847 return nil, err 848 } 849 return resp, nil 850 } 851 852 func (c *predictionGRPCClient) ListLocations(ctx context.Context, req *locationpb.ListLocationsRequest, opts ...gax.CallOption) *LocationIterator { 853 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 854 855 hds = append(c.xGoogHeaders, hds...) 856 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 857 opts = append((*c.CallOptions).ListLocations[0:len((*c.CallOptions).ListLocations):len((*c.CallOptions).ListLocations)], opts...) 858 it := &LocationIterator{} 859 req = proto.Clone(req).(*locationpb.ListLocationsRequest) 860 it.InternalFetch = func(pageSize int, pageToken string) ([]*locationpb.Location, string, error) { 861 resp := &locationpb.ListLocationsResponse{} 862 if pageToken != "" { 863 req.PageToken = pageToken 864 } 865 if pageSize > math.MaxInt32 { 866 req.PageSize = math.MaxInt32 867 } else if pageSize != 0 { 868 req.PageSize = int32(pageSize) 869 } 870 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 871 var err error 872 resp, err = executeRPC(ctx, c.locationsClient.ListLocations, req, settings.GRPC, c.logger, "ListLocations") 873 return err 874 }, opts...) 875 if err != nil { 876 return nil, "", err 877 } 878 879 it.Response = resp 880 return resp.GetLocations(), resp.GetNextPageToken(), nil 881 } 882 fetch := func(pageSize int, pageToken string) (string, error) { 883 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 884 if err != nil { 885 return "", err 886 } 887 it.items = append(it.items, items...) 888 return nextPageToken, nil 889 } 890 891 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 892 it.pageInfo.MaxSize = int(req.GetPageSize()) 893 it.pageInfo.Token = req.GetPageToken() 894 895 return it 896 } 897 898 func (c *predictionGRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 899 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))} 900 901 hds = append(c.xGoogHeaders, hds...) 902 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 903 opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...) 904 var resp *iampb.Policy 905 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 906 var err error 907 resp, err = executeRPC(ctx, c.iamPolicyClient.GetIamPolicy, req, settings.GRPC, c.logger, "GetIamPolicy") 908 return err 909 }, opts...) 910 if err != nil { 911 return nil, err 912 } 913 return resp, nil 914 } 915 916 func (c *predictionGRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 917 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))} 918 919 hds = append(c.xGoogHeaders, hds...) 920 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 921 opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...) 922 var resp *iampb.Policy 923 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 924 var err error 925 resp, err = executeRPC(ctx, c.iamPolicyClient.SetIamPolicy, req, settings.GRPC, c.logger, "SetIamPolicy") 926 return err 927 }, opts...) 928 if err != nil { 929 return nil, err 930 } 931 return resp, nil 932 } 933 934 func (c *predictionGRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) { 935 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))} 936 937 hds = append(c.xGoogHeaders, hds...) 938 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 939 opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...) 940 var resp *iampb.TestIamPermissionsResponse 941 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 942 var err error 943 resp, err = executeRPC(ctx, c.iamPolicyClient.TestIamPermissions, req, settings.GRPC, c.logger, "TestIamPermissions") 944 return err 945 }, opts...) 946 if err != nil { 947 return nil, err 948 } 949 return resp, nil 950 } 951 952 func (c *predictionGRPCClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error { 953 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 954 955 hds = append(c.xGoogHeaders, hds...) 956 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 957 opts = append((*c.CallOptions).CancelOperation[0:len((*c.CallOptions).CancelOperation):len((*c.CallOptions).CancelOperation)], opts...) 958 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 959 var err error 960 _, err = executeRPC(ctx, c.operationsClient.CancelOperation, req, settings.GRPC, c.logger, "CancelOperation") 961 return err 962 }, opts...) 963 return err 964 } 965 966 func (c *predictionGRPCClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error { 967 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 968 969 hds = append(c.xGoogHeaders, hds...) 970 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 971 opts = append((*c.CallOptions).DeleteOperation[0:len((*c.CallOptions).DeleteOperation):len((*c.CallOptions).DeleteOperation)], opts...) 972 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 973 var err error 974 _, err = executeRPC(ctx, c.operationsClient.DeleteOperation, req, settings.GRPC, c.logger, "DeleteOperation") 975 return err 976 }, opts...) 977 return err 978 } 979 980 func (c *predictionGRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { 981 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 982 983 hds = append(c.xGoogHeaders, hds...) 984 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 985 opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...) 986 var resp *longrunningpb.Operation 987 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 988 var err error 989 resp, err = executeRPC(ctx, c.operationsClient.GetOperation, req, settings.GRPC, c.logger, "GetOperation") 990 return err 991 }, opts...) 992 if err != nil { 993 return nil, err 994 } 995 return resp, nil 996 } 997 998 func (c *predictionGRPCClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator { 999 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 1000 1001 hds = append(c.xGoogHeaders, hds...) 1002 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 1003 opts = append((*c.CallOptions).ListOperations[0:len((*c.CallOptions).ListOperations):len((*c.CallOptions).ListOperations)], opts...) 1004 it := &OperationIterator{} 1005 req = proto.Clone(req).(*longrunningpb.ListOperationsRequest) 1006 it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) { 1007 resp := &longrunningpb.ListOperationsResponse{} 1008 if pageToken != "" { 1009 req.PageToken = pageToken 1010 } 1011 if pageSize > math.MaxInt32 { 1012 req.PageSize = math.MaxInt32 1013 } else if pageSize != 0 { 1014 req.PageSize = int32(pageSize) 1015 } 1016 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1017 var err error 1018 resp, err = executeRPC(ctx, c.operationsClient.ListOperations, req, settings.GRPC, c.logger, "ListOperations") 1019 return err 1020 }, opts...) 1021 if err != nil { 1022 return nil, "", err 1023 } 1024 1025 it.Response = resp 1026 return resp.GetOperations(), resp.GetNextPageToken(), nil 1027 } 1028 fetch := func(pageSize int, pageToken string) (string, error) { 1029 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 1030 if err != nil { 1031 return "", err 1032 } 1033 it.items = append(it.items, items...) 1034 return nextPageToken, nil 1035 } 1036 1037 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 1038 it.pageInfo.MaxSize = int(req.GetPageSize()) 1039 it.pageInfo.Token = req.GetPageToken() 1040 1041 return it 1042 } 1043 1044 func (c *predictionGRPCClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { 1045 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 1046 1047 hds = append(c.xGoogHeaders, hds...) 1048 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) 1049 opts = append((*c.CallOptions).WaitOperation[0:len((*c.CallOptions).WaitOperation):len((*c.CallOptions).WaitOperation)], opts...) 1050 var resp *longrunningpb.Operation 1051 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1052 var err error 1053 resp, err = executeRPC(ctx, c.operationsClient.WaitOperation, req, settings.GRPC, c.logger, "WaitOperation") 1054 return err 1055 }, opts...) 1056 if err != nil { 1057 return nil, err 1058 } 1059 return resp, nil 1060 } 1061 1062 // Predict perform an online prediction. 1063 func (c *predictionRESTClient) Predict(ctx context.Context, req *aiplatformpb.PredictRequest, opts ...gax.CallOption) (*aiplatformpb.PredictResponse, error) { 1064 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1065 jsonReq, err := m.Marshal(req) 1066 if err != nil { 1067 return nil, err 1068 } 1069 1070 baseUrl, err := url.Parse(c.endpoint) 1071 if err != nil { 1072 return nil, err 1073 } 1074 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:predict", req.GetEndpoint()) 1075 1076 params := url.Values{} 1077 params.Add("$alt", "json;enum-encoding=int") 1078 1079 baseUrl.RawQuery = params.Encode() 1080 1081 // Build HTTP headers from client and context metadata. 1082 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1083 1084 hds = append(c.xGoogHeaders, hds...) 1085 hds = append(hds, "Content-Type", "application/json") 1086 headers := gax.BuildHeaders(ctx, hds...) 1087 opts = append((*c.CallOptions).Predict[0:len((*c.CallOptions).Predict):len((*c.CallOptions).Predict)], opts...) 1088 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1089 resp := &aiplatformpb.PredictResponse{} 1090 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1091 if settings.Path != "" { 1092 baseUrl.Path = settings.Path 1093 } 1094 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1095 if err != nil { 1096 return err 1097 } 1098 httpReq = httpReq.WithContext(ctx) 1099 httpReq.Header = headers 1100 1101 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "Predict") 1102 if err != nil { 1103 return err 1104 } 1105 1106 if err := unm.Unmarshal(buf, resp); err != nil { 1107 return err 1108 } 1109 1110 return nil 1111 }, opts...) 1112 if e != nil { 1113 return nil, e 1114 } 1115 return resp, nil 1116 } 1117 1118 // RawPredict perform an online prediction with an arbitrary HTTP payload. 1119 // 1120 // The response includes the following HTTP headers: 1121 // 1122 // X-Vertex-AI-Endpoint-Id: ID of the 1123 // Endpoint that served this 1124 // prediction. 1125 // 1126 // X-Vertex-AI-Deployed-Model-Id: ID of the Endpoint’s 1127 // DeployedModel that served 1128 // this prediction. 1129 func (c *predictionRESTClient) RawPredict(ctx context.Context, req *aiplatformpb.RawPredictRequest, opts ...gax.CallOption) (*httpbodypb.HttpBody, error) { 1130 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1131 jsonReq, err := m.Marshal(req) 1132 if err != nil { 1133 return nil, err 1134 } 1135 1136 baseUrl, err := url.Parse(c.endpoint) 1137 if err != nil { 1138 return nil, err 1139 } 1140 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:rawPredict", req.GetEndpoint()) 1141 1142 params := url.Values{} 1143 params.Add("$alt", "json;enum-encoding=int") 1144 1145 baseUrl.RawQuery = params.Encode() 1146 1147 // Build HTTP headers from client and context metadata. 1148 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1149 1150 hds = append(c.xGoogHeaders, hds...) 1151 hds = append(hds, "Content-Type", "application/json") 1152 headers := gax.BuildHeaders(ctx, hds...) 1153 opts = append((*c.CallOptions).RawPredict[0:len((*c.CallOptions).RawPredict):len((*c.CallOptions).RawPredict)], opts...) 1154 resp := &httpbodypb.HttpBody{} 1155 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1156 if settings.Path != "" { 1157 baseUrl.Path = settings.Path 1158 } 1159 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1160 if err != nil { 1161 return err 1162 } 1163 httpReq = httpReq.WithContext(ctx) 1164 httpReq.Header = headers 1165 1166 buf, httpRsp, err := executeHTTPRequestWithResponse(ctx, c.httpClient, httpReq, c.logger, jsonReq, "RawPredict") 1167 if err != nil { 1168 return err 1169 } 1170 1171 resp.Data = buf 1172 if headers := httpRsp.Header; len(headers["Content-Type"]) > 0 { 1173 resp.ContentType = headers["Content-Type"][0] 1174 } 1175 1176 return nil 1177 }, opts...) 1178 if e != nil { 1179 return nil, e 1180 } 1181 return resp, nil 1182 } 1183 1184 // StreamRawPredict perform a streaming online prediction with an arbitrary HTTP payload. 1185 func (c *predictionRESTClient) StreamRawPredict(ctx context.Context, req *aiplatformpb.StreamRawPredictRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamRawPredictClient, error) { 1186 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1187 jsonReq, err := m.Marshal(req) 1188 if err != nil { 1189 return nil, err 1190 } 1191 1192 baseUrl, err := url.Parse(c.endpoint) 1193 if err != nil { 1194 return nil, err 1195 } 1196 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:streamRawPredict", req.GetEndpoint()) 1197 1198 params := url.Values{} 1199 params.Add("$alt", "json;enum-encoding=int") 1200 1201 baseUrl.RawQuery = params.Encode() 1202 1203 // Build HTTP headers from client and context metadata. 1204 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1205 1206 hds = append(c.xGoogHeaders, hds...) 1207 hds = append(hds, "Content-Type", "application/json") 1208 headers := gax.BuildHeaders(ctx, hds...) 1209 var streamClient *streamRawPredictRESTStreamClient 1210 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1211 if settings.Path != "" { 1212 baseUrl.Path = settings.Path 1213 } 1214 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1215 if err != nil { 1216 return err 1217 } 1218 httpReq = httpReq.WithContext(ctx) 1219 httpReq.Header = headers 1220 1221 httpRsp, err := executeStreamingHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "StreamRawPredict") 1222 if err != nil { 1223 return err 1224 } 1225 1226 streamClient = &streamRawPredictRESTStreamClient{ 1227 ctx: ctx, 1228 md: metadata.MD(httpRsp.Header), 1229 stream: gax.NewProtoJSONStreamReader(httpRsp.Body, (&httpbodypb.HttpBody{}).ProtoReflect().Type()), 1230 } 1231 return nil 1232 }, opts...) 1233 1234 return streamClient, e 1235 } 1236 1237 // streamRawPredictRESTStreamClient is the stream client used to consume the server stream created by 1238 // the REST implementation of StreamRawPredict. 1239 type streamRawPredictRESTStreamClient struct { 1240 ctx context.Context 1241 md metadata.MD 1242 stream *gax.ProtoJSONStream 1243 } 1244 1245 func (c *streamRawPredictRESTStreamClient) Recv() (*httpbodypb.HttpBody, error) { 1246 if err := c.ctx.Err(); err != nil { 1247 defer c.stream.Close() 1248 return nil, err 1249 } 1250 msg, err := c.stream.Recv() 1251 if err != nil { 1252 defer c.stream.Close() 1253 return nil, err 1254 } 1255 res := msg.(*httpbodypb.HttpBody) 1256 return res, nil 1257 } 1258 1259 func (c *streamRawPredictRESTStreamClient) Header() (metadata.MD, error) { 1260 return c.md, nil 1261 } 1262 1263 func (c *streamRawPredictRESTStreamClient) Trailer() metadata.MD { 1264 return c.md 1265 } 1266 1267 func (c *streamRawPredictRESTStreamClient) CloseSend() error { 1268 // This is a no-op to fulfill the interface. 1269 return errors.New("this method is not implemented for a server-stream") 1270 } 1271 1272 func (c *streamRawPredictRESTStreamClient) Context() context.Context { 1273 return c.ctx 1274 } 1275 1276 func (c *streamRawPredictRESTStreamClient) SendMsg(m interface{}) error { 1277 // This is a no-op to fulfill the interface. 1278 return errors.New("this method is not implemented for a server-stream") 1279 } 1280 1281 func (c *streamRawPredictRESTStreamClient) RecvMsg(m interface{}) error { 1282 // This is a no-op to fulfill the interface. 1283 return errors.New("this method is not implemented, use Recv") 1284 } 1285 1286 // DirectPredict perform an unary online prediction request to a gRPC model server for 1287 // Vertex first-party products and frameworks. 1288 func (c *predictionRESTClient) DirectPredict(ctx context.Context, req *aiplatformpb.DirectPredictRequest, opts ...gax.CallOption) (*aiplatformpb.DirectPredictResponse, error) { 1289 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1290 jsonReq, err := m.Marshal(req) 1291 if err != nil { 1292 return nil, err 1293 } 1294 1295 baseUrl, err := url.Parse(c.endpoint) 1296 if err != nil { 1297 return nil, err 1298 } 1299 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:directPredict", req.GetEndpoint()) 1300 1301 params := url.Values{} 1302 params.Add("$alt", "json;enum-encoding=int") 1303 1304 baseUrl.RawQuery = params.Encode() 1305 1306 // Build HTTP headers from client and context metadata. 1307 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1308 1309 hds = append(c.xGoogHeaders, hds...) 1310 hds = append(hds, "Content-Type", "application/json") 1311 headers := gax.BuildHeaders(ctx, hds...) 1312 opts = append((*c.CallOptions).DirectPredict[0:len((*c.CallOptions).DirectPredict):len((*c.CallOptions).DirectPredict)], opts...) 1313 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1314 resp := &aiplatformpb.DirectPredictResponse{} 1315 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1316 if settings.Path != "" { 1317 baseUrl.Path = settings.Path 1318 } 1319 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1320 if err != nil { 1321 return err 1322 } 1323 httpReq = httpReq.WithContext(ctx) 1324 httpReq.Header = headers 1325 1326 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "DirectPredict") 1327 if err != nil { 1328 return err 1329 } 1330 1331 if err := unm.Unmarshal(buf, resp); err != nil { 1332 return err 1333 } 1334 1335 return nil 1336 }, opts...) 1337 if e != nil { 1338 return nil, e 1339 } 1340 return resp, nil 1341 } 1342 1343 // DirectRawPredict perform an unary online prediction request to a gRPC model server for 1344 // custom containers. 1345 func (c *predictionRESTClient) DirectRawPredict(ctx context.Context, req *aiplatformpb.DirectRawPredictRequest, opts ...gax.CallOption) (*aiplatformpb.DirectRawPredictResponse, error) { 1346 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1347 jsonReq, err := m.Marshal(req) 1348 if err != nil { 1349 return nil, err 1350 } 1351 1352 baseUrl, err := url.Parse(c.endpoint) 1353 if err != nil { 1354 return nil, err 1355 } 1356 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:directRawPredict", req.GetEndpoint()) 1357 1358 params := url.Values{} 1359 params.Add("$alt", "json;enum-encoding=int") 1360 1361 baseUrl.RawQuery = params.Encode() 1362 1363 // Build HTTP headers from client and context metadata. 1364 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1365 1366 hds = append(c.xGoogHeaders, hds...) 1367 hds = append(hds, "Content-Type", "application/json") 1368 headers := gax.BuildHeaders(ctx, hds...) 1369 opts = append((*c.CallOptions).DirectRawPredict[0:len((*c.CallOptions).DirectRawPredict):len((*c.CallOptions).DirectRawPredict)], opts...) 1370 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1371 resp := &aiplatformpb.DirectRawPredictResponse{} 1372 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1373 if settings.Path != "" { 1374 baseUrl.Path = settings.Path 1375 } 1376 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1377 if err != nil { 1378 return err 1379 } 1380 httpReq = httpReq.WithContext(ctx) 1381 httpReq.Header = headers 1382 1383 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "DirectRawPredict") 1384 if err != nil { 1385 return err 1386 } 1387 1388 if err := unm.Unmarshal(buf, resp); err != nil { 1389 return err 1390 } 1391 1392 return nil 1393 }, opts...) 1394 if e != nil { 1395 return nil, e 1396 } 1397 return resp, nil 1398 } 1399 1400 // StreamDirectPredict perform a streaming online prediction request to a gRPC model server for 1401 // Vertex first-party products and frameworks. 1402 // 1403 // This method is not supported for the REST transport. 1404 func (c *predictionRESTClient) StreamDirectPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectPredictClient, error) { 1405 return nil, errors.New("StreamDirectPredict not yet supported for REST clients") 1406 } 1407 1408 // StreamDirectRawPredict perform a streaming online prediction request to a gRPC model server for 1409 // custom containers. 1410 // 1411 // This method is not supported for the REST transport. 1412 func (c *predictionRESTClient) StreamDirectRawPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamDirectRawPredictClient, error) { 1413 return nil, errors.New("StreamDirectRawPredict not yet supported for REST clients") 1414 } 1415 1416 // StreamingPredict perform a streaming online prediction request for Vertex first-party 1417 // products and frameworks. 1418 // 1419 // This method is not supported for the REST transport. 1420 func (c *predictionRESTClient) StreamingPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamingPredictClient, error) { 1421 return nil, errors.New("StreamingPredict not yet supported for REST clients") 1422 } 1423 1424 // ServerStreamingPredict perform a server-side streaming online prediction request for Vertex 1425 // LLM streaming. 1426 func (c *predictionRESTClient) ServerStreamingPredict(ctx context.Context, req *aiplatformpb.StreamingPredictRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_ServerStreamingPredictClient, error) { 1427 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1428 jsonReq, err := m.Marshal(req) 1429 if err != nil { 1430 return nil, err 1431 } 1432 1433 baseUrl, err := url.Parse(c.endpoint) 1434 if err != nil { 1435 return nil, err 1436 } 1437 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:serverStreamingPredict", req.GetEndpoint()) 1438 1439 params := url.Values{} 1440 params.Add("$alt", "json;enum-encoding=int") 1441 1442 baseUrl.RawQuery = params.Encode() 1443 1444 // Build HTTP headers from client and context metadata. 1445 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1446 1447 hds = append(c.xGoogHeaders, hds...) 1448 hds = append(hds, "Content-Type", "application/json") 1449 headers := gax.BuildHeaders(ctx, hds...) 1450 var streamClient *serverStreamingPredictRESTStreamClient 1451 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1452 if settings.Path != "" { 1453 baseUrl.Path = settings.Path 1454 } 1455 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1456 if err != nil { 1457 return err 1458 } 1459 httpReq = httpReq.WithContext(ctx) 1460 httpReq.Header = headers 1461 1462 httpRsp, err := executeStreamingHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "ServerStreamingPredict") 1463 if err != nil { 1464 return err 1465 } 1466 1467 streamClient = &serverStreamingPredictRESTStreamClient{ 1468 ctx: ctx, 1469 md: metadata.MD(httpRsp.Header), 1470 stream: gax.NewProtoJSONStreamReader(httpRsp.Body, (&aiplatformpb.StreamingPredictResponse{}).ProtoReflect().Type()), 1471 } 1472 return nil 1473 }, opts...) 1474 1475 return streamClient, e 1476 } 1477 1478 // serverStreamingPredictRESTStreamClient is the stream client used to consume the server stream created by 1479 // the REST implementation of ServerStreamingPredict. 1480 type serverStreamingPredictRESTStreamClient struct { 1481 ctx context.Context 1482 md metadata.MD 1483 stream *gax.ProtoJSONStream 1484 } 1485 1486 func (c *serverStreamingPredictRESTStreamClient) Recv() (*aiplatformpb.StreamingPredictResponse, error) { 1487 if err := c.ctx.Err(); err != nil { 1488 defer c.stream.Close() 1489 return nil, err 1490 } 1491 msg, err := c.stream.Recv() 1492 if err != nil { 1493 defer c.stream.Close() 1494 return nil, err 1495 } 1496 res := msg.(*aiplatformpb.StreamingPredictResponse) 1497 return res, nil 1498 } 1499 1500 func (c *serverStreamingPredictRESTStreamClient) Header() (metadata.MD, error) { 1501 return c.md, nil 1502 } 1503 1504 func (c *serverStreamingPredictRESTStreamClient) Trailer() metadata.MD { 1505 return c.md 1506 } 1507 1508 func (c *serverStreamingPredictRESTStreamClient) CloseSend() error { 1509 // This is a no-op to fulfill the interface. 1510 return errors.New("this method is not implemented for a server-stream") 1511 } 1512 1513 func (c *serverStreamingPredictRESTStreamClient) Context() context.Context { 1514 return c.ctx 1515 } 1516 1517 func (c *serverStreamingPredictRESTStreamClient) SendMsg(m interface{}) error { 1518 // This is a no-op to fulfill the interface. 1519 return errors.New("this method is not implemented for a server-stream") 1520 } 1521 1522 func (c *serverStreamingPredictRESTStreamClient) RecvMsg(m interface{}) error { 1523 // This is a no-op to fulfill the interface. 1524 return errors.New("this method is not implemented, use Recv") 1525 } 1526 1527 // StreamingRawPredict perform a streaming online prediction request through gRPC. 1528 // 1529 // This method is not supported for the REST transport. 1530 func (c *predictionRESTClient) StreamingRawPredict(ctx context.Context, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamingRawPredictClient, error) { 1531 return nil, errors.New("StreamingRawPredict not yet supported for REST clients") 1532 } 1533 1534 // Explain perform an online explanation. 1535 // 1536 // If 1537 // deployed_model_id 1538 // is specified, the corresponding DeployModel must have 1539 // explanation_spec 1540 // populated. If 1541 // deployed_model_id 1542 // is not specified, all DeployedModels must have 1543 // explanation_spec 1544 // populated. 1545 func (c *predictionRESTClient) Explain(ctx context.Context, req *aiplatformpb.ExplainRequest, opts ...gax.CallOption) (*aiplatformpb.ExplainResponse, error) { 1546 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1547 jsonReq, err := m.Marshal(req) 1548 if err != nil { 1549 return nil, err 1550 } 1551 1552 baseUrl, err := url.Parse(c.endpoint) 1553 if err != nil { 1554 return nil, err 1555 } 1556 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:explain", req.GetEndpoint()) 1557 1558 params := url.Values{} 1559 params.Add("$alt", "json;enum-encoding=int") 1560 1561 baseUrl.RawQuery = params.Encode() 1562 1563 // Build HTTP headers from client and context metadata. 1564 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1565 1566 hds = append(c.xGoogHeaders, hds...) 1567 hds = append(hds, "Content-Type", "application/json") 1568 headers := gax.BuildHeaders(ctx, hds...) 1569 opts = append((*c.CallOptions).Explain[0:len((*c.CallOptions).Explain):len((*c.CallOptions).Explain)], opts...) 1570 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1571 resp := &aiplatformpb.ExplainResponse{} 1572 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1573 if settings.Path != "" { 1574 baseUrl.Path = settings.Path 1575 } 1576 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1577 if err != nil { 1578 return err 1579 } 1580 httpReq = httpReq.WithContext(ctx) 1581 httpReq.Header = headers 1582 1583 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "Explain") 1584 if err != nil { 1585 return err 1586 } 1587 1588 if err := unm.Unmarshal(buf, resp); err != nil { 1589 return err 1590 } 1591 1592 return nil 1593 }, opts...) 1594 if e != nil { 1595 return nil, e 1596 } 1597 return resp, nil 1598 } 1599 1600 // CountTokens perform a token counting. 1601 func (c *predictionRESTClient) CountTokens(ctx context.Context, req *aiplatformpb.CountTokensRequest, opts ...gax.CallOption) (*aiplatformpb.CountTokensResponse, error) { 1602 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1603 jsonReq, err := m.Marshal(req) 1604 if err != nil { 1605 return nil, err 1606 } 1607 1608 baseUrl, err := url.Parse(c.endpoint) 1609 if err != nil { 1610 return nil, err 1611 } 1612 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:countTokens", req.GetEndpoint()) 1613 1614 params := url.Values{} 1615 params.Add("$alt", "json;enum-encoding=int") 1616 1617 baseUrl.RawQuery = params.Encode() 1618 1619 // Build HTTP headers from client and context metadata. 1620 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1621 1622 hds = append(c.xGoogHeaders, hds...) 1623 hds = append(hds, "Content-Type", "application/json") 1624 headers := gax.BuildHeaders(ctx, hds...) 1625 opts = append((*c.CallOptions).CountTokens[0:len((*c.CallOptions).CountTokens):len((*c.CallOptions).CountTokens)], opts...) 1626 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1627 resp := &aiplatformpb.CountTokensResponse{} 1628 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1629 if settings.Path != "" { 1630 baseUrl.Path = settings.Path 1631 } 1632 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1633 if err != nil { 1634 return err 1635 } 1636 httpReq = httpReq.WithContext(ctx) 1637 httpReq.Header = headers 1638 1639 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "CountTokens") 1640 if err != nil { 1641 return err 1642 } 1643 1644 if err := unm.Unmarshal(buf, resp); err != nil { 1645 return err 1646 } 1647 1648 return nil 1649 }, opts...) 1650 if e != nil { 1651 return nil, e 1652 } 1653 return resp, nil 1654 } 1655 1656 // GenerateContent generate content with multimodal inputs. 1657 func (c *predictionRESTClient) GenerateContent(ctx context.Context, req *aiplatformpb.GenerateContentRequest, opts ...gax.CallOption) (*aiplatformpb.GenerateContentResponse, error) { 1658 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1659 jsonReq, err := m.Marshal(req) 1660 if err != nil { 1661 return nil, err 1662 } 1663 1664 baseUrl, err := url.Parse(c.endpoint) 1665 if err != nil { 1666 return nil, err 1667 } 1668 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:generateContent", req.GetModel()) 1669 1670 params := url.Values{} 1671 params.Add("$alt", "json;enum-encoding=int") 1672 1673 baseUrl.RawQuery = params.Encode() 1674 1675 // Build HTTP headers from client and context metadata. 1676 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))} 1677 1678 hds = append(c.xGoogHeaders, hds...) 1679 hds = append(hds, "Content-Type", "application/json") 1680 headers := gax.BuildHeaders(ctx, hds...) 1681 opts = append((*c.CallOptions).GenerateContent[0:len((*c.CallOptions).GenerateContent):len((*c.CallOptions).GenerateContent)], opts...) 1682 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1683 resp := &aiplatformpb.GenerateContentResponse{} 1684 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1685 if settings.Path != "" { 1686 baseUrl.Path = settings.Path 1687 } 1688 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1689 if err != nil { 1690 return err 1691 } 1692 httpReq = httpReq.WithContext(ctx) 1693 httpReq.Header = headers 1694 1695 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "GenerateContent") 1696 if err != nil { 1697 return err 1698 } 1699 1700 if err := unm.Unmarshal(buf, resp); err != nil { 1701 return err 1702 } 1703 1704 return nil 1705 }, opts...) 1706 if e != nil { 1707 return nil, e 1708 } 1709 return resp, nil 1710 } 1711 1712 // StreamGenerateContent generate content with multimodal inputs with streaming support. 1713 func (c *predictionRESTClient) StreamGenerateContent(ctx context.Context, req *aiplatformpb.GenerateContentRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_StreamGenerateContentClient, error) { 1714 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1715 jsonReq, err := m.Marshal(req) 1716 if err != nil { 1717 return nil, err 1718 } 1719 1720 baseUrl, err := url.Parse(c.endpoint) 1721 if err != nil { 1722 return nil, err 1723 } 1724 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:streamGenerateContent", req.GetModel()) 1725 1726 params := url.Values{} 1727 params.Add("$alt", "json;enum-encoding=int") 1728 1729 baseUrl.RawQuery = params.Encode() 1730 1731 // Build HTTP headers from client and context metadata. 1732 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))} 1733 1734 hds = append(c.xGoogHeaders, hds...) 1735 hds = append(hds, "Content-Type", "application/json") 1736 headers := gax.BuildHeaders(ctx, hds...) 1737 var streamClient *streamGenerateContentRESTStreamClient 1738 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1739 if settings.Path != "" { 1740 baseUrl.Path = settings.Path 1741 } 1742 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1743 if err != nil { 1744 return err 1745 } 1746 httpReq = httpReq.WithContext(ctx) 1747 httpReq.Header = headers 1748 1749 httpRsp, err := executeStreamingHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "StreamGenerateContent") 1750 if err != nil { 1751 return err 1752 } 1753 1754 streamClient = &streamGenerateContentRESTStreamClient{ 1755 ctx: ctx, 1756 md: metadata.MD(httpRsp.Header), 1757 stream: gax.NewProtoJSONStreamReader(httpRsp.Body, (&aiplatformpb.GenerateContentResponse{}).ProtoReflect().Type()), 1758 } 1759 return nil 1760 }, opts...) 1761 1762 return streamClient, e 1763 } 1764 1765 // streamGenerateContentRESTStreamClient is the stream client used to consume the server stream created by 1766 // the REST implementation of StreamGenerateContent. 1767 type streamGenerateContentRESTStreamClient struct { 1768 ctx context.Context 1769 md metadata.MD 1770 stream *gax.ProtoJSONStream 1771 } 1772 1773 func (c *streamGenerateContentRESTStreamClient) Recv() (*aiplatformpb.GenerateContentResponse, error) { 1774 if err := c.ctx.Err(); err != nil { 1775 defer c.stream.Close() 1776 return nil, err 1777 } 1778 msg, err := c.stream.Recv() 1779 if err != nil { 1780 defer c.stream.Close() 1781 return nil, err 1782 } 1783 res := msg.(*aiplatformpb.GenerateContentResponse) 1784 return res, nil 1785 } 1786 1787 func (c *streamGenerateContentRESTStreamClient) Header() (metadata.MD, error) { 1788 return c.md, nil 1789 } 1790 1791 func (c *streamGenerateContentRESTStreamClient) Trailer() metadata.MD { 1792 return c.md 1793 } 1794 1795 func (c *streamGenerateContentRESTStreamClient) CloseSend() error { 1796 // This is a no-op to fulfill the interface. 1797 return errors.New("this method is not implemented for a server-stream") 1798 } 1799 1800 func (c *streamGenerateContentRESTStreamClient) Context() context.Context { 1801 return c.ctx 1802 } 1803 1804 func (c *streamGenerateContentRESTStreamClient) SendMsg(m interface{}) error { 1805 // This is a no-op to fulfill the interface. 1806 return errors.New("this method is not implemented for a server-stream") 1807 } 1808 1809 func (c *streamGenerateContentRESTStreamClient) RecvMsg(m interface{}) error { 1810 // This is a no-op to fulfill the interface. 1811 return errors.New("this method is not implemented, use Recv") 1812 } 1813 1814 // ChatCompletions exposes an OpenAI-compatible endpoint for chat completions. 1815 func (c *predictionRESTClient) ChatCompletions(ctx context.Context, req *aiplatformpb.ChatCompletionsRequest, opts ...gax.CallOption) (aiplatformpb.PredictionService_ChatCompletionsClient, error) { 1816 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 1817 body := req.GetHttpBody() 1818 jsonReq, err := m.Marshal(body) 1819 if err != nil { 1820 return nil, err 1821 } 1822 1823 baseUrl, err := url.Parse(c.endpoint) 1824 if err != nil { 1825 return nil, err 1826 } 1827 baseUrl.Path += fmt.Sprintf("/v1beta1/%v/chat/completions", req.GetEndpoint()) 1828 1829 params := url.Values{} 1830 params.Add("$alt", "json;enum-encoding=int") 1831 1832 baseUrl.RawQuery = params.Encode() 1833 1834 // Build HTTP headers from client and context metadata. 1835 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "endpoint", url.QueryEscape(req.GetEndpoint()))} 1836 1837 hds = append(c.xGoogHeaders, hds...) 1838 hds = append(hds, "Content-Type", "application/json") 1839 headers := gax.BuildHeaders(ctx, hds...) 1840 var streamClient *chatCompletionsRESTStreamClient 1841 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1842 if settings.Path != "" { 1843 baseUrl.Path = settings.Path 1844 } 1845 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 1846 if err != nil { 1847 return err 1848 } 1849 httpReq = httpReq.WithContext(ctx) 1850 httpReq.Header = headers 1851 1852 httpRsp, err := executeStreamingHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "ChatCompletions") 1853 if err != nil { 1854 return err 1855 } 1856 1857 streamClient = &chatCompletionsRESTStreamClient{ 1858 ctx: ctx, 1859 md: metadata.MD(httpRsp.Header), 1860 stream: gax.NewProtoJSONStreamReader(httpRsp.Body, (&httpbodypb.HttpBody{}).ProtoReflect().Type()), 1861 } 1862 return nil 1863 }, opts...) 1864 1865 return streamClient, e 1866 } 1867 1868 // chatCompletionsRESTStreamClient is the stream client used to consume the server stream created by 1869 // the REST implementation of ChatCompletions. 1870 type chatCompletionsRESTStreamClient struct { 1871 ctx context.Context 1872 md metadata.MD 1873 stream *gax.ProtoJSONStream 1874 } 1875 1876 func (c *chatCompletionsRESTStreamClient) Recv() (*httpbodypb.HttpBody, error) { 1877 if err := c.ctx.Err(); err != nil { 1878 defer c.stream.Close() 1879 return nil, err 1880 } 1881 msg, err := c.stream.Recv() 1882 if err != nil { 1883 defer c.stream.Close() 1884 return nil, err 1885 } 1886 res := msg.(*httpbodypb.HttpBody) 1887 return res, nil 1888 } 1889 1890 func (c *chatCompletionsRESTStreamClient) Header() (metadata.MD, error) { 1891 return c.md, nil 1892 } 1893 1894 func (c *chatCompletionsRESTStreamClient) Trailer() metadata.MD { 1895 return c.md 1896 } 1897 1898 func (c *chatCompletionsRESTStreamClient) CloseSend() error { 1899 // This is a no-op to fulfill the interface. 1900 return errors.New("this method is not implemented for a server-stream") 1901 } 1902 1903 func (c *chatCompletionsRESTStreamClient) Context() context.Context { 1904 return c.ctx 1905 } 1906 1907 func (c *chatCompletionsRESTStreamClient) SendMsg(m interface{}) error { 1908 // This is a no-op to fulfill the interface. 1909 return errors.New("this method is not implemented for a server-stream") 1910 } 1911 1912 func (c *chatCompletionsRESTStreamClient) RecvMsg(m interface{}) error { 1913 // This is a no-op to fulfill the interface. 1914 return errors.New("this method is not implemented, use Recv") 1915 } 1916 1917 // GetLocation gets information about a location. 1918 func (c *predictionRESTClient) GetLocation(ctx context.Context, req *locationpb.GetLocationRequest, opts ...gax.CallOption) (*locationpb.Location, error) { 1919 baseUrl, err := url.Parse(c.endpoint) 1920 if err != nil { 1921 return nil, err 1922 } 1923 baseUrl.Path += fmt.Sprintf("/ui/%v", req.GetName()) 1924 1925 params := url.Values{} 1926 params.Add("$alt", "json;enum-encoding=int") 1927 1928 baseUrl.RawQuery = params.Encode() 1929 1930 // Build HTTP headers from client and context metadata. 1931 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 1932 1933 hds = append(c.xGoogHeaders, hds...) 1934 hds = append(hds, "Content-Type", "application/json") 1935 headers := gax.BuildHeaders(ctx, hds...) 1936 opts = append((*c.CallOptions).GetLocation[0:len((*c.CallOptions).GetLocation):len((*c.CallOptions).GetLocation)], opts...) 1937 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1938 resp := &locationpb.Location{} 1939 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 1940 if settings.Path != "" { 1941 baseUrl.Path = settings.Path 1942 } 1943 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) 1944 if err != nil { 1945 return err 1946 } 1947 httpReq = httpReq.WithContext(ctx) 1948 httpReq.Header = headers 1949 1950 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetLocation") 1951 if err != nil { 1952 return err 1953 } 1954 1955 if err := unm.Unmarshal(buf, resp); err != nil { 1956 return err 1957 } 1958 1959 return nil 1960 }, opts...) 1961 if e != nil { 1962 return nil, e 1963 } 1964 return resp, nil 1965 } 1966 1967 // ListLocations lists information about the supported locations for this service. 1968 func (c *predictionRESTClient) ListLocations(ctx context.Context, req *locationpb.ListLocationsRequest, opts ...gax.CallOption) *LocationIterator { 1969 it := &LocationIterator{} 1970 req = proto.Clone(req).(*locationpb.ListLocationsRequest) 1971 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 1972 it.InternalFetch = func(pageSize int, pageToken string) ([]*locationpb.Location, string, error) { 1973 resp := &locationpb.ListLocationsResponse{} 1974 if pageToken != "" { 1975 req.PageToken = pageToken 1976 } 1977 if pageSize > math.MaxInt32 { 1978 req.PageSize = math.MaxInt32 1979 } else if pageSize != 0 { 1980 req.PageSize = int32(pageSize) 1981 } 1982 baseUrl, err := url.Parse(c.endpoint) 1983 if err != nil { 1984 return nil, "", err 1985 } 1986 baseUrl.Path += fmt.Sprintf("/ui/%v/locations", req.GetName()) 1987 1988 params := url.Values{} 1989 params.Add("$alt", "json;enum-encoding=int") 1990 if req.GetFilter() != "" { 1991 params.Add("filter", fmt.Sprintf("%v", req.GetFilter())) 1992 } 1993 if req.GetPageSize() != 0 { 1994 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize())) 1995 } 1996 if req.GetPageToken() != "" { 1997 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken())) 1998 } 1999 2000 baseUrl.RawQuery = params.Encode() 2001 2002 // Build HTTP headers from client and context metadata. 2003 hds := append(c.xGoogHeaders, "Content-Type", "application/json") 2004 headers := gax.BuildHeaders(ctx, hds...) 2005 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2006 if settings.Path != "" { 2007 baseUrl.Path = settings.Path 2008 } 2009 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) 2010 if err != nil { 2011 return err 2012 } 2013 httpReq.Header = headers 2014 2015 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListLocations") 2016 if err != nil { 2017 return err 2018 } 2019 if err := unm.Unmarshal(buf, resp); err != nil { 2020 return err 2021 } 2022 2023 return nil 2024 }, opts...) 2025 if e != nil { 2026 return nil, "", e 2027 } 2028 it.Response = resp 2029 return resp.GetLocations(), resp.GetNextPageToken(), nil 2030 } 2031 2032 fetch := func(pageSize int, pageToken string) (string, error) { 2033 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 2034 if err != nil { 2035 return "", err 2036 } 2037 it.items = append(it.items, items...) 2038 return nextPageToken, nil 2039 } 2040 2041 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 2042 it.pageInfo.MaxSize = int(req.GetPageSize()) 2043 it.pageInfo.Token = req.GetPageToken() 2044 2045 return it 2046 } 2047 2048 // GetIamPolicy gets the access control policy for a resource. Returns an empty policy 2049 // if the resource exists and does not have a policy set. 2050 func (c *predictionRESTClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 2051 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 2052 jsonReq, err := m.Marshal(req) 2053 if err != nil { 2054 return nil, err 2055 } 2056 2057 baseUrl, err := url.Parse(c.endpoint) 2058 if err != nil { 2059 return nil, err 2060 } 2061 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:getIamPolicy", req.GetResource()) 2062 2063 params := url.Values{} 2064 params.Add("$alt", "json;enum-encoding=int") 2065 2066 baseUrl.RawQuery = params.Encode() 2067 2068 // Build HTTP headers from client and context metadata. 2069 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))} 2070 2071 hds = append(c.xGoogHeaders, hds...) 2072 hds = append(hds, "Content-Type", "application/json") 2073 headers := gax.BuildHeaders(ctx, hds...) 2074 opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...) 2075 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 2076 resp := &iampb.Policy{} 2077 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2078 if settings.Path != "" { 2079 baseUrl.Path = settings.Path 2080 } 2081 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 2082 if err != nil { 2083 return err 2084 } 2085 httpReq = httpReq.WithContext(ctx) 2086 httpReq.Header = headers 2087 2088 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "GetIamPolicy") 2089 if err != nil { 2090 return err 2091 } 2092 2093 if err := unm.Unmarshal(buf, resp); err != nil { 2094 return err 2095 } 2096 2097 return nil 2098 }, opts...) 2099 if e != nil { 2100 return nil, e 2101 } 2102 return resp, nil 2103 } 2104 2105 // SetIamPolicy sets the access control policy on the specified resource. Replaces 2106 // any existing policy. 2107 // 2108 // Can return NOT_FOUND, INVALID_ARGUMENT, and PERMISSION_DENIED 2109 // errors. 2110 func (c *predictionRESTClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 2111 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 2112 jsonReq, err := m.Marshal(req) 2113 if err != nil { 2114 return nil, err 2115 } 2116 2117 baseUrl, err := url.Parse(c.endpoint) 2118 if err != nil { 2119 return nil, err 2120 } 2121 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:setIamPolicy", req.GetResource()) 2122 2123 params := url.Values{} 2124 params.Add("$alt", "json;enum-encoding=int") 2125 2126 baseUrl.RawQuery = params.Encode() 2127 2128 // Build HTTP headers from client and context metadata. 2129 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))} 2130 2131 hds = append(c.xGoogHeaders, hds...) 2132 hds = append(hds, "Content-Type", "application/json") 2133 headers := gax.BuildHeaders(ctx, hds...) 2134 opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...) 2135 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 2136 resp := &iampb.Policy{} 2137 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2138 if settings.Path != "" { 2139 baseUrl.Path = settings.Path 2140 } 2141 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 2142 if err != nil { 2143 return err 2144 } 2145 httpReq = httpReq.WithContext(ctx) 2146 httpReq.Header = headers 2147 2148 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "SetIamPolicy") 2149 if err != nil { 2150 return err 2151 } 2152 2153 if err := unm.Unmarshal(buf, resp); err != nil { 2154 return err 2155 } 2156 2157 return nil 2158 }, opts...) 2159 if e != nil { 2160 return nil, e 2161 } 2162 return resp, nil 2163 } 2164 2165 // TestIamPermissions returns permissions that a caller has on the specified resource. If the 2166 // resource does not exist, this will return an empty set of 2167 // permissions, not a NOT_FOUND error. 2168 // 2169 // Note: This operation is designed to be used for building 2170 // permission-aware UIs and command-line tools, not for authorization 2171 // checking. This operation may “fail open” without warning. 2172 func (c *predictionRESTClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) { 2173 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} 2174 jsonReq, err := m.Marshal(req) 2175 if err != nil { 2176 return nil, err 2177 } 2178 2179 baseUrl, err := url.Parse(c.endpoint) 2180 if err != nil { 2181 return nil, err 2182 } 2183 baseUrl.Path += fmt.Sprintf("/v1beta1/%v:testIamPermissions", req.GetResource()) 2184 2185 params := url.Values{} 2186 params.Add("$alt", "json;enum-encoding=int") 2187 2188 baseUrl.RawQuery = params.Encode() 2189 2190 // Build HTTP headers from client and context metadata. 2191 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))} 2192 2193 hds = append(c.xGoogHeaders, hds...) 2194 hds = append(hds, "Content-Type", "application/json") 2195 headers := gax.BuildHeaders(ctx, hds...) 2196 opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...) 2197 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 2198 resp := &iampb.TestIamPermissionsResponse{} 2199 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2200 if settings.Path != "" { 2201 baseUrl.Path = settings.Path 2202 } 2203 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) 2204 if err != nil { 2205 return err 2206 } 2207 httpReq = httpReq.WithContext(ctx) 2208 httpReq.Header = headers 2209 2210 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "TestIamPermissions") 2211 if err != nil { 2212 return err 2213 } 2214 2215 if err := unm.Unmarshal(buf, resp); err != nil { 2216 return err 2217 } 2218 2219 return nil 2220 }, opts...) 2221 if e != nil { 2222 return nil, e 2223 } 2224 return resp, nil 2225 } 2226 2227 // CancelOperation is a utility method from google.longrunning.Operations. 2228 func (c *predictionRESTClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error { 2229 baseUrl, err := url.Parse(c.endpoint) 2230 if err != nil { 2231 return err 2232 } 2233 baseUrl.Path += fmt.Sprintf("/ui/%v:cancel", req.GetName()) 2234 2235 params := url.Values{} 2236 params.Add("$alt", "json;enum-encoding=int") 2237 2238 baseUrl.RawQuery = params.Encode() 2239 2240 // Build HTTP headers from client and context metadata. 2241 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 2242 2243 hds = append(c.xGoogHeaders, hds...) 2244 hds = append(hds, "Content-Type", "application/json") 2245 headers := gax.BuildHeaders(ctx, hds...) 2246 return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2247 if settings.Path != "" { 2248 baseUrl.Path = settings.Path 2249 } 2250 httpReq, err := http.NewRequest("POST", baseUrl.String(), nil) 2251 if err != nil { 2252 return err 2253 } 2254 httpReq = httpReq.WithContext(ctx) 2255 httpReq.Header = headers 2256 2257 _, err = executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "CancelOperation") 2258 return err 2259 }, opts...) 2260 } 2261 2262 // DeleteOperation is a utility method from google.longrunning.Operations. 2263 func (c *predictionRESTClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error { 2264 baseUrl, err := url.Parse(c.endpoint) 2265 if err != nil { 2266 return err 2267 } 2268 baseUrl.Path += fmt.Sprintf("/ui/%v", req.GetName()) 2269 2270 params := url.Values{} 2271 params.Add("$alt", "json;enum-encoding=int") 2272 2273 baseUrl.RawQuery = params.Encode() 2274 2275 // Build HTTP headers from client and context metadata. 2276 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 2277 2278 hds = append(c.xGoogHeaders, hds...) 2279 hds = append(hds, "Content-Type", "application/json") 2280 headers := gax.BuildHeaders(ctx, hds...) 2281 return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2282 if settings.Path != "" { 2283 baseUrl.Path = settings.Path 2284 } 2285 httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil) 2286 if err != nil { 2287 return err 2288 } 2289 httpReq = httpReq.WithContext(ctx) 2290 httpReq.Header = headers 2291 2292 _, err = executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "DeleteOperation") 2293 return err 2294 }, opts...) 2295 } 2296 2297 // GetOperation is a utility method from google.longrunning.Operations. 2298 func (c *predictionRESTClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { 2299 baseUrl, err := url.Parse(c.endpoint) 2300 if err != nil { 2301 return nil, err 2302 } 2303 baseUrl.Path += fmt.Sprintf("/ui/%v", req.GetName()) 2304 2305 params := url.Values{} 2306 params.Add("$alt", "json;enum-encoding=int") 2307 2308 baseUrl.RawQuery = params.Encode() 2309 2310 // Build HTTP headers from client and context metadata. 2311 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 2312 2313 hds = append(c.xGoogHeaders, hds...) 2314 hds = append(hds, "Content-Type", "application/json") 2315 headers := gax.BuildHeaders(ctx, hds...) 2316 opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...) 2317 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 2318 resp := &longrunningpb.Operation{} 2319 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2320 if settings.Path != "" { 2321 baseUrl.Path = settings.Path 2322 } 2323 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) 2324 if err != nil { 2325 return err 2326 } 2327 httpReq = httpReq.WithContext(ctx) 2328 httpReq.Header = headers 2329 2330 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetOperation") 2331 if err != nil { 2332 return err 2333 } 2334 2335 if err := unm.Unmarshal(buf, resp); err != nil { 2336 return err 2337 } 2338 2339 return nil 2340 }, opts...) 2341 if e != nil { 2342 return nil, e 2343 } 2344 return resp, nil 2345 } 2346 2347 // ListOperations is a utility method from google.longrunning.Operations. 2348 func (c *predictionRESTClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator { 2349 it := &OperationIterator{} 2350 req = proto.Clone(req).(*longrunningpb.ListOperationsRequest) 2351 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 2352 it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) { 2353 resp := &longrunningpb.ListOperationsResponse{} 2354 if pageToken != "" { 2355 req.PageToken = pageToken 2356 } 2357 if pageSize > math.MaxInt32 { 2358 req.PageSize = math.MaxInt32 2359 } else if pageSize != 0 { 2360 req.PageSize = int32(pageSize) 2361 } 2362 baseUrl, err := url.Parse(c.endpoint) 2363 if err != nil { 2364 return nil, "", err 2365 } 2366 baseUrl.Path += fmt.Sprintf("/ui/%v/operations", req.GetName()) 2367 2368 params := url.Values{} 2369 params.Add("$alt", "json;enum-encoding=int") 2370 if req.GetFilter() != "" { 2371 params.Add("filter", fmt.Sprintf("%v", req.GetFilter())) 2372 } 2373 if req.GetPageSize() != 0 { 2374 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize())) 2375 } 2376 if req.GetPageToken() != "" { 2377 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken())) 2378 } 2379 2380 baseUrl.RawQuery = params.Encode() 2381 2382 // Build HTTP headers from client and context metadata. 2383 hds := append(c.xGoogHeaders, "Content-Type", "application/json") 2384 headers := gax.BuildHeaders(ctx, hds...) 2385 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2386 if settings.Path != "" { 2387 baseUrl.Path = settings.Path 2388 } 2389 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) 2390 if err != nil { 2391 return err 2392 } 2393 httpReq.Header = headers 2394 2395 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListOperations") 2396 if err != nil { 2397 return err 2398 } 2399 if err := unm.Unmarshal(buf, resp); err != nil { 2400 return err 2401 } 2402 2403 return nil 2404 }, opts...) 2405 if e != nil { 2406 return nil, "", e 2407 } 2408 it.Response = resp 2409 return resp.GetOperations(), resp.GetNextPageToken(), nil 2410 } 2411 2412 fetch := func(pageSize int, pageToken string) (string, error) { 2413 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 2414 if err != nil { 2415 return "", err 2416 } 2417 it.items = append(it.items, items...) 2418 return nextPageToken, nil 2419 } 2420 2421 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 2422 it.pageInfo.MaxSize = int(req.GetPageSize()) 2423 it.pageInfo.Token = req.GetPageToken() 2424 2425 return it 2426 } 2427 2428 // WaitOperation is a utility method from google.longrunning.Operations. 2429 func (c *predictionRESTClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { 2430 baseUrl, err := url.Parse(c.endpoint) 2431 if err != nil { 2432 return nil, err 2433 } 2434 baseUrl.Path += fmt.Sprintf("/ui/%v:wait", req.GetName()) 2435 2436 params := url.Values{} 2437 params.Add("$alt", "json;enum-encoding=int") 2438 if req.GetTimeout() != nil { 2439 field, err := protojson.Marshal(req.GetTimeout()) 2440 if err != nil { 2441 return nil, err 2442 } 2443 params.Add("timeout", string(field[1:len(field)-1])) 2444 } 2445 2446 baseUrl.RawQuery = params.Encode() 2447 2448 // Build HTTP headers from client and context metadata. 2449 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} 2450 2451 hds = append(c.xGoogHeaders, hds...) 2452 hds = append(hds, "Content-Type", "application/json") 2453 headers := gax.BuildHeaders(ctx, hds...) 2454 opts = append((*c.CallOptions).WaitOperation[0:len((*c.CallOptions).WaitOperation):len((*c.CallOptions).WaitOperation)], opts...) 2455 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} 2456 resp := &longrunningpb.Operation{} 2457 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 2458 if settings.Path != "" { 2459 baseUrl.Path = settings.Path 2460 } 2461 httpReq, err := http.NewRequest("POST", baseUrl.String(), nil) 2462 if err != nil { 2463 return err 2464 } 2465 httpReq = httpReq.WithContext(ctx) 2466 httpReq.Header = headers 2467 2468 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "WaitOperation") 2469 if err != nil { 2470 return err 2471 } 2472 2473 if err := unm.Unmarshal(buf, resp); err != nil { 2474 return err 2475 } 2476 2477 return nil 2478 }, opts...) 2479 if e != nil { 2480 return nil, e 2481 } 2482 return resp, nil 2483 }