| 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 apikeys |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"fmt" |
| 23 |
"log/slog" |
| 24 |
"math" |
| 25 |
"net/http" |
| 26 |
"net/url" |
| 27 |
"time" |
| 28 |
|
| 29 |
apikeyspb "cloud.google.com/go/apikeys/apiv2/apikeyspb" |
| 30 |
"cloud.google.com/go/longrunning" |
| 31 |
lroauto "cloud.google.com/go/longrunning/autogen" |
| 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 |
"google.golang.org/grpc" |
| 40 |
"google.golang.org/protobuf/encoding/protojson" |
| 41 |
"google.golang.org/protobuf/proto" |
| 42 |
) |
| 43 |
|
| 44 |
var newClientHook clientHook |
| 45 |
|
| 46 |
// CallOptions contains the retry settings for each method of Client. |
| 47 |
type CallOptions struct { |
| 48 |
CreateKey []gax.CallOption |
| 49 |
ListKeys []gax.CallOption |
| 50 |
GetKey []gax.CallOption |
| 51 |
GetKeyString []gax.CallOption |
| 52 |
UpdateKey []gax.CallOption |
| 53 |
DeleteKey []gax.CallOption |
| 54 |
UndeleteKey []gax.CallOption |
| 55 |
LookupKey []gax.CallOption |
| 56 |
GetOperation []gax.CallOption |
| 57 |
} |
| 58 |
|
| 59 |
func defaultGRPCClientOptions() []option.ClientOption { |
| 60 |
return []option.ClientOption{ |
| 61 |
internaloption.WithDefaultEndpoint("apikeys.googleapis.com:443"), |
| 62 |
internaloption.WithDefaultEndpointTemplate("apikeys.UNIVERSE_DOMAIN:443"), |
| 63 |
internaloption.WithDefaultMTLSEndpoint("apikeys.mtls.googleapis.com:443"), |
| 64 |
internaloption.WithDefaultUniverseDomain("googleapis.com"), |
| 65 |
internaloption.WithDefaultAudience("https://apikeys.googleapis.com/"), |
| 66 |
internaloption.WithDefaultScopes(DefaultAuthScopes()...), |
| 67 |
internaloption.EnableJwtWithScope(), |
| 68 |
internaloption.EnableNewAuthLibrary(), |
| 69 |
option.WithGRPCDialOption(grpc.WithDefaultCallOptions( |
| 70 |
grpc.MaxCallRecvMsgSize(math.MaxInt32))), |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func defaultCallOptions() *CallOptions { |
| 75 |
return &CallOptions{ |
| 76 |
CreateKey: []gax.CallOption{ |
| 77 |
gax.WithTimeout(10000 * time.Millisecond), |
| 78 |
}, |
| 79 |
ListKeys: []gax.CallOption{ |
| 80 |
gax.WithTimeout(10000 * time.Millisecond), |
| 81 |
}, |
| 82 |
GetKey: []gax.CallOption{ |
| 83 |
gax.WithTimeout(10000 * time.Millisecond), |
| 84 |
}, |
| 85 |
GetKeyString: []gax.CallOption{ |
| 86 |
gax.WithTimeout(10000 * time.Millisecond), |
| 87 |
}, |
| 88 |
UpdateKey: []gax.CallOption{ |
| 89 |
gax.WithTimeout(10000 * time.Millisecond), |
| 90 |
}, |
| 91 |
DeleteKey: []gax.CallOption{ |
| 92 |
gax.WithTimeout(10000 * time.Millisecond), |
| 93 |
}, |
| 94 |
UndeleteKey: []gax.CallOption{ |
| 95 |
gax.WithTimeout(10000 * time.Millisecond), |
| 96 |
}, |
| 97 |
LookupKey: []gax.CallOption{ |
| 98 |
gax.WithTimeout(10000 * time.Millisecond), |
| 99 |
}, |
| 100 |
GetOperation: []gax.CallOption{}, |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func defaultRESTCallOptions() *CallOptions { |
| 105 |
return &CallOptions{ |
| 106 |
CreateKey: []gax.CallOption{ |
| 107 |
gax.WithTimeout(10000 * time.Millisecond), |
| 108 |
}, |
| 109 |
ListKeys: []gax.CallOption{ |
| 110 |
gax.WithTimeout(10000 * time.Millisecond), |
| 111 |
}, |
| 112 |
GetKey: []gax.CallOption{ |
| 113 |
gax.WithTimeout(10000 * time.Millisecond), |
| 114 |
}, |
| 115 |
GetKeyString: []gax.CallOption{ |
| 116 |
gax.WithTimeout(10000 * time.Millisecond), |
| 117 |
}, |
| 118 |
UpdateKey: []gax.CallOption{ |
| 119 |
gax.WithTimeout(10000 * time.Millisecond), |
| 120 |
}, |
| 121 |
DeleteKey: []gax.CallOption{ |
| 122 |
gax.WithTimeout(10000 * time.Millisecond), |
| 123 |
}, |
| 124 |
UndeleteKey: []gax.CallOption{ |
| 125 |
gax.WithTimeout(10000 * time.Millisecond), |
| 126 |
}, |
| 127 |
LookupKey: []gax.CallOption{ |
| 128 |
gax.WithTimeout(10000 * time.Millisecond), |
| 129 |
}, |
| 130 |
GetOperation: []gax.CallOption{}, |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// internalClient is an interface that defines the methods available from API Keys API. |
| 135 |
type internalClient interface { |
| 136 |
Close() error |
| 137 |
setGoogleClientInfo(...string) |
| 138 |
Connection() *grpc.ClientConn |
| 139 |
CreateKey(context.Context, *apikeyspb.CreateKeyRequest, ...gax.CallOption) (*CreateKeyOperation, error) |
| 140 |
CreateKeyOperation(name string) *CreateKeyOperation |
| 141 |
ListKeys(context.Context, *apikeyspb.ListKeysRequest, ...gax.CallOption) *KeyIterator |
| 142 |
GetKey(context.Context, *apikeyspb.GetKeyRequest, ...gax.CallOption) (*apikeyspb.Key, error) |
| 143 |
GetKeyString(context.Context, *apikeyspb.GetKeyStringRequest, ...gax.CallOption) (*apikeyspb.GetKeyStringResponse, error) |
| 144 |
UpdateKey(context.Context, *apikeyspb.UpdateKeyRequest, ...gax.CallOption) (*UpdateKeyOperation, error) |
| 145 |
UpdateKeyOperation(name string) *UpdateKeyOperation |
| 146 |
DeleteKey(context.Context, *apikeyspb.DeleteKeyRequest, ...gax.CallOption) (*DeleteKeyOperation, error) |
| 147 |
DeleteKeyOperation(name string) *DeleteKeyOperation |
| 148 |
UndeleteKey(context.Context, *apikeyspb.UndeleteKeyRequest, ...gax.CallOption) (*UndeleteKeyOperation, error) |
| 149 |
UndeleteKeyOperation(name string) *UndeleteKeyOperation |
| 150 |
LookupKey(context.Context, *apikeyspb.LookupKeyRequest, ...gax.CallOption) (*apikeyspb.LookupKeyResponse, error) |
| 151 |
GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) |
| 152 |
} |
| 153 |
|
| 154 |
// Client is a client for interacting with API Keys API. |
| 155 |
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 156 |
// |
| 157 |
// Manages the API keys associated with projects. |
| 158 |
type Client struct { |
| 159 |
// The internal transport-dependent client. |
| 160 |
internalClient internalClient |
| 161 |
|
| 162 |
// The call options for this service. |
| 163 |
CallOptions *CallOptions |
| 164 |
|
| 165 |
// LROClient is used internally to handle long-running operations. |
| 166 |
// It is exposed so that its CallOptions can be modified if required. |
| 167 |
// Users should not Close this client. |
| 168 |
LROClient *lroauto.OperationsClient |
| 169 |
} |
| 170 |
|
| 171 |
// Wrapper methods routed to the internal client. |
| 172 |
|
| 173 |
// Close closes the connection to the API service. The user should invoke this when |
| 174 |
// the client is no longer required. |
| 175 |
func (c *Client) Close() error { |
| 176 |
return c.internalClient.Close() |
| 177 |
} |
| 178 |
|
| 179 |
// setGoogleClientInfo sets the name and version of the application in |
| 180 |
// the `x-goog-api-client` header passed on each request. Intended for |
| 181 |
// use by Google-written clients. |
| 182 |
func (c *Client) setGoogleClientInfo(keyval ...string) { |
| 183 |
c.internalClient.setGoogleClientInfo(keyval...) |
| 184 |
} |
| 185 |
|
| 186 |
// Connection returns a connection to the API service. |
| 187 |
// |
| 188 |
// Deprecated: Connections are now pooled so this method does not always |
| 189 |
// return the same resource. |
| 190 |
func (c *Client) Connection() *grpc.ClientConn { |
| 191 |
return c.internalClient.Connection() |
| 192 |
} |
| 193 |
|
| 194 |
// CreateKey creates a new API key. |
| 195 |
// |
| 196 |
// NOTE: Key is a global resource; hence the only supported value for |
| 197 |
// location is global. |
| 198 |
func (c *Client) CreateKey(ctx context.Context, req *apikeyspb.CreateKeyRequest, opts ...gax.CallOption) (*CreateKeyOperation, error) { |
| 199 |
return c.internalClient.CreateKey(ctx, req, opts...) |
| 200 |
} |
| 201 |
|
| 202 |
// CreateKeyOperation returns a new CreateKeyOperation from a given name. |
| 203 |
// The name must be that of a previously created CreateKeyOperation, possibly from a different process. |
| 204 |
func (c *Client) CreateKeyOperation(name string) *CreateKeyOperation { |
| 205 |
return c.internalClient.CreateKeyOperation(name) |
| 206 |
} |
| 207 |
|
| 208 |
// ListKeys lists the API keys owned by a project. The key string of the API key |
| 209 |
// isn’t included in the response. |
| 210 |
// |
| 211 |
// NOTE: Key is a global resource; hence the only supported value for |
| 212 |
// location is global. |
| 213 |
func (c *Client) ListKeys(ctx context.Context, req *apikeyspb.ListKeysRequest, opts ...gax.CallOption) *KeyIterator { |
| 214 |
return c.internalClient.ListKeys(ctx, req, opts...) |
| 215 |
} |
| 216 |
|
| 217 |
// GetKey gets the metadata for an API key. The key string of the API key |
| 218 |
// isn’t included in the response. |
| 219 |
// |
| 220 |
// NOTE: Key is a global resource; hence the only supported value for |
| 221 |
// location is global. |
| 222 |
func (c *Client) GetKey(ctx context.Context, req *apikeyspb.GetKeyRequest, opts ...gax.CallOption) (*apikeyspb.Key, error) { |
| 223 |
return c.internalClient.GetKey(ctx, req, opts...) |
| 224 |
} |
| 225 |
|
| 226 |
// GetKeyString get the key string for an API key. |
| 227 |
// |
| 228 |
// NOTE: Key is a global resource; hence the only supported value for |
| 229 |
// location is global. |
| 230 |
func (c *Client) GetKeyString(ctx context.Context, req *apikeyspb.GetKeyStringRequest, opts ...gax.CallOption) (*apikeyspb.GetKeyStringResponse, error) { |
| 231 |
return c.internalClient.GetKeyString(ctx, req, opts...) |
| 232 |
} |
| 233 |
|
| 234 |
// UpdateKey patches the modifiable fields of an API key. |
| 235 |
// The key string of the API key isn’t included in the response. |
| 236 |
// |
| 237 |
// NOTE: Key is a global resource; hence the only supported value for |
| 238 |
// location is global. |
| 239 |
func (c *Client) UpdateKey(ctx context.Context, req *apikeyspb.UpdateKeyRequest, opts ...gax.CallOption) (*UpdateKeyOperation, error) { |
| 240 |
return c.internalClient.UpdateKey(ctx, req, opts...) |
| 241 |
} |
| 242 |
|
| 243 |
// UpdateKeyOperation returns a new UpdateKeyOperation from a given name. |
| 244 |
// The name must be that of a previously created UpdateKeyOperation, possibly from a different process. |
| 245 |
func (c *Client) UpdateKeyOperation(name string) *UpdateKeyOperation { |
| 246 |
return c.internalClient.UpdateKeyOperation(name) |
| 247 |
} |
| 248 |
|
| 249 |
// DeleteKey deletes an API key. Deleted key can be retrieved within 30 days of |
| 250 |
// deletion. Afterward, key will be purged from the project. |
| 251 |
// |
| 252 |
// NOTE: Key is a global resource; hence the only supported value for |
| 253 |
// location is global. |
| 254 |
func (c *Client) DeleteKey(ctx context.Context, req *apikeyspb.DeleteKeyRequest, opts ...gax.CallOption) (*DeleteKeyOperation, error) { |
| 255 |
return c.internalClient.DeleteKey(ctx, req, opts...) |
| 256 |
} |
| 257 |
|
| 258 |
// DeleteKeyOperation returns a new DeleteKeyOperation from a given name. |
| 259 |
// The name must be that of a previously created DeleteKeyOperation, possibly from a different process. |
| 260 |
func (c *Client) DeleteKeyOperation(name string) *DeleteKeyOperation { |
| 261 |
return c.internalClient.DeleteKeyOperation(name) |
| 262 |
} |
| 263 |
|
| 264 |
// UndeleteKey undeletes an API key which was deleted within 30 days. |
| 265 |
// |
| 266 |
// NOTE: Key is a global resource; hence the only supported value for |
| 267 |
// location is global. |
| 268 |
func (c *Client) UndeleteKey(ctx context.Context, req *apikeyspb.UndeleteKeyRequest, opts ...gax.CallOption) (*UndeleteKeyOperation, error) { |
| 269 |
return c.internalClient.UndeleteKey(ctx, req, opts...) |
| 270 |
} |
| 271 |
|
| 272 |
// UndeleteKeyOperation returns a new UndeleteKeyOperation from a given name. |
| 273 |
// The name must be that of a previously created UndeleteKeyOperation, possibly from a different process. |
| 274 |
func (c *Client) UndeleteKeyOperation(name string) *UndeleteKeyOperation { |
| 275 |
return c.internalClient.UndeleteKeyOperation(name) |
| 276 |
} |
| 277 |
|
| 278 |
// LookupKey find the parent project and resource name of the API |
| 279 |
// key that matches the key string in the request. If the API key has been |
| 280 |
// purged, resource name will not be set. |
| 281 |
// The service account must have the apikeys.keys.lookup permission |
| 282 |
// on the parent project. |
| 283 |
func (c *Client) LookupKey(ctx context.Context, req *apikeyspb.LookupKeyRequest, opts ...gax.CallOption) (*apikeyspb.LookupKeyResponse, error) { |
| 284 |
return c.internalClient.LookupKey(ctx, req, opts...) |
| 285 |
} |
| 286 |
|
| 287 |
// GetOperation is a utility method from google.longrunning.Operations. |
| 288 |
func (c *Client) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { |
| 289 |
return c.internalClient.GetOperation(ctx, req, opts...) |
| 290 |
} |
| 291 |
|
| 292 |
// gRPCClient is a client for interacting with API Keys API over gRPC transport. |
| 293 |
// |
| 294 |
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 295 |
type gRPCClient struct { |
| 296 |
// Connection pool of gRPC connections to the service. |
| 297 |
connPool gtransport.ConnPool |
| 298 |
|
| 299 |
// Points back to the CallOptions field of the containing Client |
| 300 |
CallOptions **CallOptions |
| 301 |
|
| 302 |
// The gRPC API client. |
| 303 |
client apikeyspb.ApiKeysClient |
| 304 |
|
| 305 |
// LROClient is used internally to handle long-running operations. |
| 306 |
// It is exposed so that its CallOptions can be modified if required. |
| 307 |
// Users should not Close this client. |
| 308 |
LROClient **lroauto.OperationsClient |
| 309 |
|
| 310 |
operationsClient longrunningpb.OperationsClient |
| 311 |
|
| 312 |
// The x-goog-* metadata to be sent with each request. |
| 313 |
xGoogHeaders []string |
| 314 |
|
| 315 |
logger *slog.Logger |
| 316 |
} |
| 317 |
|
| 318 |
// NewClient creates a new api keys client based on gRPC. |
| 319 |
// The returned client must be Closed when it is done being used to clean up its underlying connections. |
| 320 |
// |
| 321 |
// Manages the API keys associated with projects. |
| 322 |
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { |
| 323 |
clientOpts := defaultGRPCClientOptions() |
| 324 |
if newClientHook != nil { |
| 325 |
hookOpts, err := newClientHook(ctx, clientHookParams{}) |
| 326 |
if err != nil { |
| 327 |
return nil, err |
| 328 |
} |
| 329 |
clientOpts = append(clientOpts, hookOpts...) |
| 330 |
} |
| 331 |
|
| 332 |
connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) |
| 333 |
if err != nil { |
| 334 |
return nil, err |
| 335 |
} |
| 336 |
client := Client{CallOptions: defaultCallOptions()} |
| 337 |
|
| 338 |
c := &gRPCClient{ |
| 339 |
connPool: connPool, |
| 340 |
client: apikeyspb.NewApiKeysClient(connPool), |
| 341 |
CallOptions: &client.CallOptions, |
| 342 |
logger: internaloption.GetLogger(opts), |
| 343 |
operationsClient: longrunningpb.NewOperationsClient(connPool), |
| 344 |
} |
| 345 |
c.setGoogleClientInfo() |
| 346 |
|
| 347 |
client.internalClient = c |
| 348 |
|
| 349 |
client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) |
| 350 |
if err != nil { |
| 351 |
// This error "should not happen", since we are just reusing old connection pool |
| 352 |
// and never actually need to dial. |
| 353 |
// If this does happen, we could leak connp. However, we cannot close conn: |
| 354 |
// If the user invoked the constructor with option.WithGRPCConn, |
| 355 |
// we would close a connection that's still in use. |
| 356 |
// TODO: investigate error conditions. |
| 357 |
return nil, err |
| 358 |
} |
| 359 |
c.LROClient = &client.LROClient |
| 360 |
return &client, nil |
| 361 |
} |
| 362 |
|
| 363 |
// Connection returns a connection to the API service. |
| 364 |
// |
| 365 |
// Deprecated: Connections are now pooled so this method does not always |
| 366 |
// return the same resource. |
| 367 |
func (c *gRPCClient) Connection() *grpc.ClientConn { |
| 368 |
return c.connPool.Conn() |
| 369 |
} |
| 370 |
|
| 371 |
// setGoogleClientInfo sets the name and version of the application in |
| 372 |
// the `x-goog-api-client` header passed on each request. Intended for |
| 373 |
// use by Google-written clients. |
| 374 |
func (c *gRPCClient) setGoogleClientInfo(keyval ...string) { |
| 375 |
kv := append([]string{"gl-go", gax.GoVersion}, keyval...) |
| 376 |
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion) |
| 377 |
c.xGoogHeaders = []string{ |
| 378 |
"x-goog-api-client", gax.XGoogHeader(kv...), |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// Close closes the connection to the API service. The user should invoke this when |
| 383 |
// the client is no longer required. |
| 384 |
func (c *gRPCClient) Close() error { |
| 385 |
return c.connPool.Close() |
| 386 |
} |
| 387 |
|
| 388 |
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 389 |
type restClient struct { |
| 390 |
// The http endpoint to connect to. |
| 391 |
endpoint string |
| 392 |
|
| 393 |
// The http client. |
| 394 |
httpClient *http.Client |
| 395 |
|
| 396 |
// LROClient is used internally to handle long-running operations. |
| 397 |
// It is exposed so that its CallOptions can be modified if required. |
| 398 |
// Users should not Close this client. |
| 399 |
LROClient **lroauto.OperationsClient |
| 400 |
|
| 401 |
// The x-goog-* headers to be sent with each request. |
| 402 |
xGoogHeaders []string |
| 403 |
|
| 404 |
// Points back to the CallOptions field of the containing Client |
| 405 |
CallOptions **CallOptions |
| 406 |
|
| 407 |
logger *slog.Logger |
| 408 |
} |
| 409 |
|
| 410 |
// NewRESTClient creates a new api keys rest client. |
| 411 |
// |
| 412 |
// Manages the API keys associated with projects. |
| 413 |
func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { |
| 414 |
clientOpts := append(defaultRESTClientOptions(), opts...) |
| 415 |
httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...) |
| 416 |
if err != nil { |
| 417 |
return nil, err |
| 418 |
} |
| 419 |
|
| 420 |
callOpts := defaultRESTCallOptions() |
| 421 |
c := &restClient{ |
| 422 |
endpoint: endpoint, |
| 423 |
httpClient: httpClient, |
| 424 |
CallOptions: &callOpts, |
| 425 |
logger: internaloption.GetLogger(opts), |
| 426 |
} |
| 427 |
c.setGoogleClientInfo() |
| 428 |
|
| 429 |
lroOpts := []option.ClientOption{ |
| 430 |
option.WithHTTPClient(httpClient), |
| 431 |
option.WithEndpoint(endpoint), |
| 432 |
} |
| 433 |
opClient, err := lroauto.NewOperationsRESTClient(ctx, lroOpts...) |
| 434 |
if err != nil { |
| 435 |
return nil, err |
| 436 |
} |
| 437 |
c.LROClient = &opClient |
| 438 |
|
| 439 |
return &Client{internalClient: c, CallOptions: callOpts}, nil |
| 440 |
} |
| 441 |
|
| 442 |
func defaultRESTClientOptions() []option.ClientOption { |
| 443 |
return []option.ClientOption{ |
| 444 |
internaloption.WithDefaultEndpoint("https://apikeys.googleapis.com"), |
| 445 |
internaloption.WithDefaultEndpointTemplate("https://apikeys.UNIVERSE_DOMAIN"), |
| 446 |
internaloption.WithDefaultMTLSEndpoint("https://apikeys.mtls.googleapis.com"), |
| 447 |
internaloption.WithDefaultUniverseDomain("googleapis.com"), |
| 448 |
internaloption.WithDefaultAudience("https://apikeys.googleapis.com/"), |
| 449 |
internaloption.WithDefaultScopes(DefaultAuthScopes()...), |
| 450 |
internaloption.EnableNewAuthLibrary(), |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
// setGoogleClientInfo sets the name and version of the application in |
| 455 |
// the `x-goog-api-client` header passed on each request. Intended for |
| 456 |
// use by Google-written clients. |
| 457 |
func (c *restClient) setGoogleClientInfo(keyval ...string) { |
| 458 |
kv := append([]string{"gl-go", gax.GoVersion}, keyval...) |
| 459 |
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion) |
| 460 |
c.xGoogHeaders = []string{ |
| 461 |
"x-goog-api-client", gax.XGoogHeader(kv...), |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// Close closes the connection to the API service. The user should invoke this when |
| 466 |
// the client is no longer required. |
| 467 |
func (c *restClient) Close() error { |
| 468 |
// Replace httpClient with nil to force cleanup. |
| 469 |
c.httpClient = nil |
| 470 |
return nil |
| 471 |
} |
| 472 |
|
| 473 |
// Connection returns a connection to the API service. |
| 474 |
// |
| 475 |
// Deprecated: This method always returns nil. |
| 476 |
func (c *restClient) Connection() *grpc.ClientConn { |
| 477 |
return nil |
| 478 |
} |
| 479 |
func (c *gRPCClient) CreateKey(ctx context.Context, req *apikeyspb.CreateKeyRequest, opts ...gax.CallOption) (*CreateKeyOperation, error) { |
| 480 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))} |
| 481 |
|
| 482 |
hds = append(c.xGoogHeaders, hds...) |
| 483 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 484 |
opts = append((*c.CallOptions).CreateKey[0:len((*c.CallOptions).CreateKey):len((*c.CallOptions).CreateKey)], opts...) |
| 485 |
var resp *longrunningpb.Operation |
| 486 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 487 |
var err error |
| 488 |
resp, err = executeRPC(ctx, c.client.CreateKey, req, settings.GRPC, c.logger, "CreateKey") |
| 489 |
return err |
| 490 |
}, opts...) |
| 491 |
if err != nil { |
| 492 |
return nil, err |
| 493 |
} |
| 494 |
return &CreateKeyOperation{ |
| 495 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 496 |
}, nil |
| 497 |
} |
| 498 |
|
| 499 |
func (c *gRPCClient) ListKeys(ctx context.Context, req *apikeyspb.ListKeysRequest, opts ...gax.CallOption) *KeyIterator { |
| 500 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))} |
| 501 |
|
| 502 |
hds = append(c.xGoogHeaders, hds...) |
| 503 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 504 |
opts = append((*c.CallOptions).ListKeys[0:len((*c.CallOptions).ListKeys):len((*c.CallOptions).ListKeys)], opts...) |
| 505 |
it := &KeyIterator{} |
| 506 |
req = proto.Clone(req).(*apikeyspb.ListKeysRequest) |
| 507 |
it.InternalFetch = func(pageSize int, pageToken string) ([]*apikeyspb.Key, string, error) { |
| 508 |
resp := &apikeyspb.ListKeysResponse{} |
| 509 |
if pageToken != "" { |
| 510 |
req.PageToken = pageToken |
| 511 |
} |
| 512 |
if pageSize > math.MaxInt32 { |
| 513 |
req.PageSize = math.MaxInt32 |
| 514 |
} else if pageSize != 0 { |
| 515 |
req.PageSize = int32(pageSize) |
| 516 |
} |
| 517 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 518 |
var err error |
| 519 |
resp, err = executeRPC(ctx, c.client.ListKeys, req, settings.GRPC, c.logger, "ListKeys") |
| 520 |
return err |
| 521 |
}, opts...) |
| 522 |
if err != nil { |
| 523 |
return nil, "", err |
| 524 |
} |
| 525 |
|
| 526 |
it.Response = resp |
| 527 |
return resp.GetKeys(), resp.GetNextPageToken(), nil |
| 528 |
} |
| 529 |
fetch := func(pageSize int, pageToken string) (string, error) { |
| 530 |
items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) |
| 531 |
if err != nil { |
| 532 |
return "", err |
| 533 |
} |
| 534 |
it.items = append(it.items, items...) |
| 535 |
return nextPageToken, nil |
| 536 |
} |
| 537 |
|
| 538 |
it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) |
| 539 |
it.pageInfo.MaxSize = int(req.GetPageSize()) |
| 540 |
it.pageInfo.Token = req.GetPageToken() |
| 541 |
|
| 542 |
return it |
| 543 |
} |
| 544 |
|
| 545 |
func (c *gRPCClient) GetKey(ctx context.Context, req *apikeyspb.GetKeyRequest, opts ...gax.CallOption) (*apikeyspb.Key, error) { |
| 546 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 547 |
|
| 548 |
hds = append(c.xGoogHeaders, hds...) |
| 549 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 550 |
opts = append((*c.CallOptions).GetKey[0:len((*c.CallOptions).GetKey):len((*c.CallOptions).GetKey)], opts...) |
| 551 |
var resp *apikeyspb.Key |
| 552 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 553 |
var err error |
| 554 |
resp, err = executeRPC(ctx, c.client.GetKey, req, settings.GRPC, c.logger, "GetKey") |
| 555 |
return err |
| 556 |
}, opts...) |
| 557 |
if err != nil { |
| 558 |
return nil, err |
| 559 |
} |
| 560 |
return resp, nil |
| 561 |
} |
| 562 |
|
| 563 |
func (c *gRPCClient) GetKeyString(ctx context.Context, req *apikeyspb.GetKeyStringRequest, opts ...gax.CallOption) (*apikeyspb.GetKeyStringResponse, error) { |
| 564 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 565 |
|
| 566 |
hds = append(c.xGoogHeaders, hds...) |
| 567 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 568 |
opts = append((*c.CallOptions).GetKeyString[0:len((*c.CallOptions).GetKeyString):len((*c.CallOptions).GetKeyString)], opts...) |
| 569 |
var resp *apikeyspb.GetKeyStringResponse |
| 570 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 571 |
var err error |
| 572 |
resp, err = executeRPC(ctx, c.client.GetKeyString, req, settings.GRPC, c.logger, "GetKeyString") |
| 573 |
return err |
| 574 |
}, opts...) |
| 575 |
if err != nil { |
| 576 |
return nil, err |
| 577 |
} |
| 578 |
return resp, nil |
| 579 |
} |
| 580 |
|
| 581 |
func (c *gRPCClient) UpdateKey(ctx context.Context, req *apikeyspb.UpdateKeyRequest, opts ...gax.CallOption) (*UpdateKeyOperation, error) { |
| 582 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "key.name", url.QueryEscape(req.GetKey().GetName()))} |
| 583 |
|
| 584 |
hds = append(c.xGoogHeaders, hds...) |
| 585 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 586 |
opts = append((*c.CallOptions).UpdateKey[0:len((*c.CallOptions).UpdateKey):len((*c.CallOptions).UpdateKey)], opts...) |
| 587 |
var resp *longrunningpb.Operation |
| 588 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 589 |
var err error |
| 590 |
resp, err = executeRPC(ctx, c.client.UpdateKey, req, settings.GRPC, c.logger, "UpdateKey") |
| 591 |
return err |
| 592 |
}, opts...) |
| 593 |
if err != nil { |
| 594 |
return nil, err |
| 595 |
} |
| 596 |
return &UpdateKeyOperation{ |
| 597 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 598 |
}, nil |
| 599 |
} |
| 600 |
|
| 601 |
func (c *gRPCClient) DeleteKey(ctx context.Context, req *apikeyspb.DeleteKeyRequest, opts ...gax.CallOption) (*DeleteKeyOperation, error) { |
| 602 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 603 |
|
| 604 |
hds = append(c.xGoogHeaders, hds...) |
| 605 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 606 |
opts = append((*c.CallOptions).DeleteKey[0:len((*c.CallOptions).DeleteKey):len((*c.CallOptions).DeleteKey)], opts...) |
| 607 |
var resp *longrunningpb.Operation |
| 608 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 609 |
var err error |
| 610 |
resp, err = executeRPC(ctx, c.client.DeleteKey, req, settings.GRPC, c.logger, "DeleteKey") |
| 611 |
return err |
| 612 |
}, opts...) |
| 613 |
if err != nil { |
| 614 |
return nil, err |
| 615 |
} |
| 616 |
return &DeleteKeyOperation{ |
| 617 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 618 |
}, nil |
| 619 |
} |
| 620 |
|
| 621 |
func (c *gRPCClient) UndeleteKey(ctx context.Context, req *apikeyspb.UndeleteKeyRequest, opts ...gax.CallOption) (*UndeleteKeyOperation, error) { |
| 622 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 623 |
|
| 624 |
hds = append(c.xGoogHeaders, hds...) |
| 625 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 626 |
opts = append((*c.CallOptions).UndeleteKey[0:len((*c.CallOptions).UndeleteKey):len((*c.CallOptions).UndeleteKey)], opts...) |
| 627 |
var resp *longrunningpb.Operation |
| 628 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 629 |
var err error |
| 630 |
resp, err = executeRPC(ctx, c.client.UndeleteKey, req, settings.GRPC, c.logger, "UndeleteKey") |
| 631 |
return err |
| 632 |
}, opts...) |
| 633 |
if err != nil { |
| 634 |
return nil, err |
| 635 |
} |
| 636 |
return &UndeleteKeyOperation{ |
| 637 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 638 |
}, nil |
| 639 |
} |
| 640 |
|
| 641 |
func (c *gRPCClient) LookupKey(ctx context.Context, req *apikeyspb.LookupKeyRequest, opts ...gax.CallOption) (*apikeyspb.LookupKeyResponse, error) { |
| 642 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...) |
| 643 |
opts = append((*c.CallOptions).LookupKey[0:len((*c.CallOptions).LookupKey):len((*c.CallOptions).LookupKey)], opts...) |
| 644 |
var resp *apikeyspb.LookupKeyResponse |
| 645 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 646 |
var err error |
| 647 |
resp, err = executeRPC(ctx, c.client.LookupKey, req, settings.GRPC, c.logger, "LookupKey") |
| 648 |
return err |
| 649 |
}, opts...) |
| 650 |
if err != nil { |
| 651 |
return nil, err |
| 652 |
} |
| 653 |
return resp, nil |
| 654 |
} |
| 655 |
|
| 656 |
func (c *gRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { |
| 657 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 658 |
|
| 659 |
hds = append(c.xGoogHeaders, hds...) |
| 660 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 661 |
opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...) |
| 662 |
var resp *longrunningpb.Operation |
| 663 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 664 |
var err error |
| 665 |
resp, err = executeRPC(ctx, c.operationsClient.GetOperation, req, settings.GRPC, c.logger, "GetOperation") |
| 666 |
return err |
| 667 |
}, opts...) |
| 668 |
if err != nil { |
| 669 |
return nil, err |
| 670 |
} |
| 671 |
return resp, nil |
| 672 |
} |
| 673 |
|
| 674 |
// CreateKey creates a new API key. |
| 675 |
// |
| 676 |
// NOTE: Key is a global resource; hence the only supported value for |
| 677 |
// location is global. |
| 678 |
func (c *restClient) CreateKey(ctx context.Context, req *apikeyspb.CreateKeyRequest, opts ...gax.CallOption) (*CreateKeyOperation, error) { |
| 679 |
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} |
| 680 |
body := req.GetKey() |
| 681 |
jsonReq, err := m.Marshal(body) |
| 682 |
if err != nil { |
| 683 |
return nil, err |
| 684 |
} |
| 685 |
|
| 686 |
baseUrl, err := url.Parse(c.endpoint) |
| 687 |
if err != nil { |
| 688 |
return nil, err |
| 689 |
} |
| 690 |
baseUrl.Path += fmt.Sprintf("/v2/%v/keys", req.GetParent()) |
| 691 |
|
| 692 |
params := url.Values{} |
| 693 |
params.Add("$alt", "json;enum-encoding=int") |
| 694 |
if req.GetKeyId() != "" { |
| 695 |
params.Add("keyId", fmt.Sprintf("%v", req.GetKeyId())) |
| 696 |
} |
| 697 |
|
| 698 |
baseUrl.RawQuery = params.Encode() |
| 699 |
|
| 700 |
// Build HTTP headers from client and context metadata. |
| 701 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))} |
| 702 |
|
| 703 |
hds = append(c.xGoogHeaders, hds...) |
| 704 |
hds = append(hds, "Content-Type", "application/json") |
| 705 |
headers := gax.BuildHeaders(ctx, hds...) |
| 706 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 707 |
resp := &longrunningpb.Operation{} |
| 708 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 709 |
if settings.Path != "" { |
| 710 |
baseUrl.Path = settings.Path |
| 711 |
} |
| 712 |
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) |
| 713 |
if err != nil { |
| 714 |
return err |
| 715 |
} |
| 716 |
httpReq = httpReq.WithContext(ctx) |
| 717 |
httpReq.Header = headers |
| 718 |
|
| 719 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "CreateKey") |
| 720 |
if err != nil { |
| 721 |
return err |
| 722 |
} |
| 723 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 724 |
return err |
| 725 |
} |
| 726 |
|
| 727 |
return nil |
| 728 |
}, opts...) |
| 729 |
if e != nil { |
| 730 |
return nil, e |
| 731 |
} |
| 732 |
|
| 733 |
override := fmt.Sprintf("/v2/%s", resp.GetName()) |
| 734 |
return &CreateKeyOperation{ |
| 735 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 736 |
pollPath: override, |
| 737 |
}, nil |
| 738 |
} |
| 739 |
|
| 740 |
// ListKeys lists the API keys owned by a project. The key string of the API key |
| 741 |
// isn’t included in the response. |
| 742 |
// |
| 743 |
// NOTE: Key is a global resource; hence the only supported value for |
| 744 |
// location is global. |
| 745 |
func (c *restClient) ListKeys(ctx context.Context, req *apikeyspb.ListKeysRequest, opts ...gax.CallOption) *KeyIterator { |
| 746 |
it := &KeyIterator{} |
| 747 |
req = proto.Clone(req).(*apikeyspb.ListKeysRequest) |
| 748 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 749 |
it.InternalFetch = func(pageSize int, pageToken string) ([]*apikeyspb.Key, string, error) { |
| 750 |
resp := &apikeyspb.ListKeysResponse{} |
| 751 |
if pageToken != "" { |
| 752 |
req.PageToken = pageToken |
| 753 |
} |
| 754 |
if pageSize > math.MaxInt32 { |
| 755 |
req.PageSize = math.MaxInt32 |
| 756 |
} else if pageSize != 0 { |
| 757 |
req.PageSize = int32(pageSize) |
| 758 |
} |
| 759 |
baseUrl, err := url.Parse(c.endpoint) |
| 760 |
if err != nil { |
| 761 |
return nil, "", err |
| 762 |
} |
| 763 |
baseUrl.Path += fmt.Sprintf("/v2/%v/keys", req.GetParent()) |
| 764 |
|
| 765 |
params := url.Values{} |
| 766 |
params.Add("$alt", "json;enum-encoding=int") |
| 767 |
if req.GetPageSize() != 0 { |
| 768 |
params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize())) |
| 769 |
} |
| 770 |
if req.GetPageToken() != "" { |
| 771 |
params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken())) |
| 772 |
} |
| 773 |
if req.GetShowDeleted() { |
| 774 |
params.Add("showDeleted", fmt.Sprintf("%v", req.GetShowDeleted())) |
| 775 |
} |
| 776 |
|
| 777 |
baseUrl.RawQuery = params.Encode() |
| 778 |
|
| 779 |
// Build HTTP headers from client and context metadata. |
| 780 |
hds := append(c.xGoogHeaders, "Content-Type", "application/json") |
| 781 |
headers := gax.BuildHeaders(ctx, hds...) |
| 782 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 783 |
if settings.Path != "" { |
| 784 |
baseUrl.Path = settings.Path |
| 785 |
} |
| 786 |
httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) |
| 787 |
if err != nil { |
| 788 |
return err |
| 789 |
} |
| 790 |
httpReq.Header = headers |
| 791 |
|
| 792 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListKeys") |
| 793 |
if err != nil { |
| 794 |
return err |
| 795 |
} |
| 796 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 797 |
return err |
| 798 |
} |
| 799 |
|
| 800 |
return nil |
| 801 |
}, opts...) |
| 802 |
if e != nil { |
| 803 |
return nil, "", e |
| 804 |
} |
| 805 |
it.Response = resp |
| 806 |
return resp.GetKeys(), resp.GetNextPageToken(), nil |
| 807 |
} |
| 808 |
|
| 809 |
fetch := func(pageSize int, pageToken string) (string, error) { |
| 810 |
items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) |
| 811 |
if err != nil { |
| 812 |
return "", err |
| 813 |
} |
| 814 |
it.items = append(it.items, items...) |
| 815 |
return nextPageToken, nil |
| 816 |
} |
| 817 |
|
| 818 |
it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) |
| 819 |
it.pageInfo.MaxSize = int(req.GetPageSize()) |
| 820 |
it.pageInfo.Token = req.GetPageToken() |
| 821 |
|
| 822 |
return it |
| 823 |
} |
| 824 |
|
| 825 |
// GetKey gets the metadata for an API key. The key string of the API key |
| 826 |
// isn’t included in the response. |
| 827 |
// |
| 828 |
// NOTE: Key is a global resource; hence the only supported value for |
| 829 |
// location is global. |
| 830 |
func (c *restClient) GetKey(ctx context.Context, req *apikeyspb.GetKeyRequest, opts ...gax.CallOption) (*apikeyspb.Key, error) { |
| 831 |
baseUrl, err := url.Parse(c.endpoint) |
| 832 |
if err != nil { |
| 833 |
return nil, err |
| 834 |
} |
| 835 |
baseUrl.Path += fmt.Sprintf("/v2/%v", req.GetName()) |
| 836 |
|
| 837 |
params := url.Values{} |
| 838 |
params.Add("$alt", "json;enum-encoding=int") |
| 839 |
|
| 840 |
baseUrl.RawQuery = params.Encode() |
| 841 |
|
| 842 |
// Build HTTP headers from client and context metadata. |
| 843 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 844 |
|
| 845 |
hds = append(c.xGoogHeaders, hds...) |
| 846 |
hds = append(hds, "Content-Type", "application/json") |
| 847 |
headers := gax.BuildHeaders(ctx, hds...) |
| 848 |
opts = append((*c.CallOptions).GetKey[0:len((*c.CallOptions).GetKey):len((*c.CallOptions).GetKey)], opts...) |
| 849 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 850 |
resp := &apikeyspb.Key{} |
| 851 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 852 |
if settings.Path != "" { |
| 853 |
baseUrl.Path = settings.Path |
| 854 |
} |
| 855 |
httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) |
| 856 |
if err != nil { |
| 857 |
return err |
| 858 |
} |
| 859 |
httpReq = httpReq.WithContext(ctx) |
| 860 |
httpReq.Header = headers |
| 861 |
|
| 862 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetKey") |
| 863 |
if err != nil { |
| 864 |
return err |
| 865 |
} |
| 866 |
|
| 867 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 868 |
return err |
| 869 |
} |
| 870 |
|
| 871 |
return nil |
| 872 |
}, opts...) |
| 873 |
if e != nil { |
| 874 |
return nil, e |
| 875 |
} |
| 876 |
return resp, nil |
| 877 |
} |
| 878 |
|
| 879 |
// GetKeyString get the key string for an API key. |
| 880 |
// |
| 881 |
// NOTE: Key is a global resource; hence the only supported value for |
| 882 |
// location is global. |
| 883 |
func (c *restClient) GetKeyString(ctx context.Context, req *apikeyspb.GetKeyStringRequest, opts ...gax.CallOption) (*apikeyspb.GetKeyStringResponse, error) { |
| 884 |
baseUrl, err := url.Parse(c.endpoint) |
| 885 |
if err != nil { |
| 886 |
return nil, err |
| 887 |
} |
| 888 |
baseUrl.Path += fmt.Sprintf("/v2/%v/keyString", req.GetName()) |
| 889 |
|
| 890 |
params := url.Values{} |
| 891 |
params.Add("$alt", "json;enum-encoding=int") |
| 892 |
|
| 893 |
baseUrl.RawQuery = params.Encode() |
| 894 |
|
| 895 |
// Build HTTP headers from client and context metadata. |
| 896 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 897 |
|
| 898 |
hds = append(c.xGoogHeaders, hds...) |
| 899 |
hds = append(hds, "Content-Type", "application/json") |
| 900 |
headers := gax.BuildHeaders(ctx, hds...) |
| 901 |
opts = append((*c.CallOptions).GetKeyString[0:len((*c.CallOptions).GetKeyString):len((*c.CallOptions).GetKeyString)], opts...) |
| 902 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 903 |
resp := &apikeyspb.GetKeyStringResponse{} |
| 904 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 905 |
if settings.Path != "" { |
| 906 |
baseUrl.Path = settings.Path |
| 907 |
} |
| 908 |
httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) |
| 909 |
if err != nil { |
| 910 |
return err |
| 911 |
} |
| 912 |
httpReq = httpReq.WithContext(ctx) |
| 913 |
httpReq.Header = headers |
| 914 |
|
| 915 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetKeyString") |
| 916 |
if err != nil { |
| 917 |
return err |
| 918 |
} |
| 919 |
|
| 920 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 921 |
return err |
| 922 |
} |
| 923 |
|
| 924 |
return nil |
| 925 |
}, opts...) |
| 926 |
if e != nil { |
| 927 |
return nil, e |
| 928 |
} |
| 929 |
return resp, nil |
| 930 |
} |
| 931 |
|
| 932 |
// UpdateKey patches the modifiable fields of an API key. |
| 933 |
// The key string of the API key isn’t included in the response. |
| 934 |
// |
| 935 |
// NOTE: Key is a global resource; hence the only supported value for |
| 936 |
// location is global. |
| 937 |
func (c *restClient) UpdateKey(ctx context.Context, req *apikeyspb.UpdateKeyRequest, opts ...gax.CallOption) (*UpdateKeyOperation, error) { |
| 938 |
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} |
| 939 |
body := req.GetKey() |
| 940 |
jsonReq, err := m.Marshal(body) |
| 941 |
if err != nil { |
| 942 |
return nil, err |
| 943 |
} |
| 944 |
|
| 945 |
baseUrl, err := url.Parse(c.endpoint) |
| 946 |
if err != nil { |
| 947 |
return nil, err |
| 948 |
} |
| 949 |
baseUrl.Path += fmt.Sprintf("/v2/%v", req.GetKey().GetName()) |
| 950 |
|
| 951 |
params := url.Values{} |
| 952 |
params.Add("$alt", "json;enum-encoding=int") |
| 953 |
if req.GetUpdateMask() != nil { |
| 954 |
field, err := protojson.Marshal(req.GetUpdateMask()) |
| 955 |
if err != nil { |
| 956 |
return nil, err |
| 957 |
} |
| 958 |
params.Add("updateMask", string(field[1:len(field)-1])) |
| 959 |
} |
| 960 |
|
| 961 |
baseUrl.RawQuery = params.Encode() |
| 962 |
|
| 963 |
// Build HTTP headers from client and context metadata. |
| 964 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "key.name", url.QueryEscape(req.GetKey().GetName()))} |
| 965 |
|
| 966 |
hds = append(c.xGoogHeaders, hds...) |
| 967 |
hds = append(hds, "Content-Type", "application/json") |
| 968 |
headers := gax.BuildHeaders(ctx, hds...) |
| 969 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 970 |
resp := &longrunningpb.Operation{} |
| 971 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 972 |
if settings.Path != "" { |
| 973 |
baseUrl.Path = settings.Path |
| 974 |
} |
| 975 |
httpReq, err := http.NewRequest("PATCH", baseUrl.String(), bytes.NewReader(jsonReq)) |
| 976 |
if err != nil { |
| 977 |
return err |
| 978 |
} |
| 979 |
httpReq = httpReq.WithContext(ctx) |
| 980 |
httpReq.Header = headers |
| 981 |
|
| 982 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "UpdateKey") |
| 983 |
if err != nil { |
| 984 |
return err |
| 985 |
} |
| 986 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 987 |
return err |
| 988 |
} |
| 989 |
|
| 990 |
return nil |
| 991 |
}, opts...) |
| 992 |
if e != nil { |
| 993 |
return nil, e |
| 994 |
} |
| 995 |
|
| 996 |
override := fmt.Sprintf("/v2/%s", resp.GetName()) |
| 997 |
return &UpdateKeyOperation{ |
| 998 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 999 |
pollPath: override, |
| 1000 |
}, nil |
| 1001 |
} |
| 1002 |
|
| 1003 |
// DeleteKey deletes an API key. Deleted key can be retrieved within 30 days of |
| 1004 |
// deletion. Afterward, key will be purged from the project. |
| 1005 |
// |
| 1006 |
// NOTE: Key is a global resource; hence the only supported value for |
| 1007 |
// location is global. |
| 1008 |
func (c *restClient) DeleteKey(ctx context.Context, req *apikeyspb.DeleteKeyRequest, opts ...gax.CallOption) (*DeleteKeyOperation, error) { |
| 1009 |
baseUrl, err := url.Parse(c.endpoint) |
| 1010 |
if err != nil { |
| 1011 |
return nil, err |
| 1012 |
} |
| 1013 |
baseUrl.Path += fmt.Sprintf("/v2/%v", req.GetName()) |
| 1014 |
|
| 1015 |
params := url.Values{} |
| 1016 |
params.Add("$alt", "json;enum-encoding=int") |
| 1017 |
if req.GetEtag() != "" { |
| 1018 |
params.Add("etag", fmt.Sprintf("%v", req.GetEtag())) |
| 1019 |
} |
| 1020 |
|
| 1021 |
baseUrl.RawQuery = params.Encode() |
| 1022 |
|
| 1023 |
// Build HTTP headers from client and context metadata. |
| 1024 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 1025 |
|
| 1026 |
hds = append(c.xGoogHeaders, hds...) |
| 1027 |
hds = append(hds, "Content-Type", "application/json") |
| 1028 |
headers := gax.BuildHeaders(ctx, hds...) |
| 1029 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 1030 |
resp := &longrunningpb.Operation{} |
| 1031 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 1032 |
if settings.Path != "" { |
| 1033 |
baseUrl.Path = settings.Path |
| 1034 |
} |
| 1035 |
httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil) |
| 1036 |
if err != nil { |
| 1037 |
return err |
| 1038 |
} |
| 1039 |
httpReq = httpReq.WithContext(ctx) |
| 1040 |
httpReq.Header = headers |
| 1041 |
|
| 1042 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "DeleteKey") |
| 1043 |
if err != nil { |
| 1044 |
return err |
| 1045 |
} |
| 1046 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 1047 |
return err |
| 1048 |
} |
| 1049 |
|
| 1050 |
return nil |
| 1051 |
}, opts...) |
| 1052 |
if e != nil { |
| 1053 |
return nil, e |
| 1054 |
} |
| 1055 |
|
| 1056 |
override := fmt.Sprintf("/v2/%s", resp.GetName()) |
| 1057 |
return &DeleteKeyOperation{ |
| 1058 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 1059 |
pollPath: override, |
| 1060 |
}, nil |
| 1061 |
} |
| 1062 |
|
| 1063 |
// UndeleteKey undeletes an API key which was deleted within 30 days. |
| 1064 |
// |
| 1065 |
// NOTE: Key is a global resource; hence the only supported value for |
| 1066 |
// location is global. |
| 1067 |
func (c *restClient) UndeleteKey(ctx context.Context, req *apikeyspb.UndeleteKeyRequest, opts ...gax.CallOption) (*UndeleteKeyOperation, error) { |
| 1068 |
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} |
| 1069 |
jsonReq, err := m.Marshal(req) |
| 1070 |
if err != nil { |
| 1071 |
return nil, err |
| 1072 |
} |
| 1073 |
|
| 1074 |
baseUrl, err := url.Parse(c.endpoint) |
| 1075 |
if err != nil { |
| 1076 |
return nil, err |
| 1077 |
} |
| 1078 |
baseUrl.Path += fmt.Sprintf("/v2/%v:undelete", req.GetName()) |
| 1079 |
|
| 1080 |
params := url.Values{} |
| 1081 |
params.Add("$alt", "json;enum-encoding=int") |
| 1082 |
|
| 1083 |
baseUrl.RawQuery = params.Encode() |
| 1084 |
|
| 1085 |
// Build HTTP headers from client and context metadata. |
| 1086 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 1087 |
|
| 1088 |
hds = append(c.xGoogHeaders, hds...) |
| 1089 |
hds = append(hds, "Content-Type", "application/json") |
| 1090 |
headers := gax.BuildHeaders(ctx, hds...) |
| 1091 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 1092 |
resp := &longrunningpb.Operation{} |
| 1093 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 1094 |
if settings.Path != "" { |
| 1095 |
baseUrl.Path = settings.Path |
| 1096 |
} |
| 1097 |
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq)) |
| 1098 |
if err != nil { |
| 1099 |
return err |
| 1100 |
} |
| 1101 |
httpReq = httpReq.WithContext(ctx) |
| 1102 |
httpReq.Header = headers |
| 1103 |
|
| 1104 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "UndeleteKey") |
| 1105 |
if err != nil { |
| 1106 |
return err |
| 1107 |
} |
| 1108 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 1109 |
return err |
| 1110 |
} |
| 1111 |
|
| 1112 |
return nil |
| 1113 |
}, opts...) |
| 1114 |
if e != nil { |
| 1115 |
return nil, e |
| 1116 |
} |
| 1117 |
|
| 1118 |
override := fmt.Sprintf("/v2/%s", resp.GetName()) |
| 1119 |
return &UndeleteKeyOperation{ |
| 1120 |
lro: longrunning.InternalNewOperation(*c.LROClient, resp), |
| 1121 |
pollPath: override, |
| 1122 |
}, nil |
| 1123 |
} |
| 1124 |
|
| 1125 |
// LookupKey find the parent project and resource name of the API |
| 1126 |
// key that matches the key string in the request. If the API key has been |
| 1127 |
// purged, resource name will not be set. |
| 1128 |
// The service account must have the apikeys.keys.lookup permission |
| 1129 |
// on the parent project. |
| 1130 |
func (c *restClient) LookupKey(ctx context.Context, req *apikeyspb.LookupKeyRequest, opts ...gax.CallOption) (*apikeyspb.LookupKeyResponse, error) { |
| 1131 |
baseUrl, err := url.Parse(c.endpoint) |
| 1132 |
if err != nil { |
| 1133 |
return nil, err |
| 1134 |
} |
| 1135 |
baseUrl.Path += fmt.Sprintf("/v2/keys:lookupKey") |
| 1136 |
|
| 1137 |
params := url.Values{} |
| 1138 |
params.Add("$alt", "json;enum-encoding=int") |
| 1139 |
params.Add("keyString", fmt.Sprintf("%v", req.GetKeyString())) |
| 1140 |
|
| 1141 |
baseUrl.RawQuery = params.Encode() |
| 1142 |
|
| 1143 |
// Build HTTP headers from client and context metadata. |
| 1144 |
hds := append(c.xGoogHeaders, "Content-Type", "application/json") |
| 1145 |
headers := gax.BuildHeaders(ctx, hds...) |
| 1146 |
opts = append((*c.CallOptions).LookupKey[0:len((*c.CallOptions).LookupKey):len((*c.CallOptions).LookupKey)], opts...) |
| 1147 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 1148 |
resp := &apikeyspb.LookupKeyResponse{} |
| 1149 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 1150 |
if settings.Path != "" { |
| 1151 |
baseUrl.Path = settings.Path |
| 1152 |
} |
| 1153 |
httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) |
| 1154 |
if err != nil { |
| 1155 |
return err |
| 1156 |
} |
| 1157 |
httpReq = httpReq.WithContext(ctx) |
| 1158 |
httpReq.Header = headers |
| 1159 |
|
| 1160 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "LookupKey") |
| 1161 |
if err != nil { |
| 1162 |
return err |
| 1163 |
} |
| 1164 |
|
| 1165 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 1166 |
return err |
| 1167 |
} |
| 1168 |
|
| 1169 |
return nil |
| 1170 |
}, opts...) |
| 1171 |
if e != nil { |
| 1172 |
return nil, e |
| 1173 |
} |
| 1174 |
return resp, nil |
| 1175 |
} |
| 1176 |
|
| 1177 |
// GetOperation is a utility method from google.longrunning.Operations. |
| 1178 |
func (c *restClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { |
| 1179 |
baseUrl, err := url.Parse(c.endpoint) |
| 1180 |
if err != nil { |
| 1181 |
return nil, err |
| 1182 |
} |
| 1183 |
baseUrl.Path += fmt.Sprintf("/v2/%v", req.GetName()) |
| 1184 |
|
| 1185 |
params := url.Values{} |
| 1186 |
params.Add("$alt", "json;enum-encoding=int") |
| 1187 |
|
| 1188 |
baseUrl.RawQuery = params.Encode() |
| 1189 |
|
| 1190 |
// Build HTTP headers from client and context metadata. |
| 1191 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))} |
| 1192 |
|
| 1193 |
hds = append(c.xGoogHeaders, hds...) |
| 1194 |
hds = append(hds, "Content-Type", "application/json") |
| 1195 |
headers := gax.BuildHeaders(ctx, hds...) |
| 1196 |
opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...) |
| 1197 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 1198 |
resp := &longrunningpb.Operation{} |
| 1199 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 1200 |
if settings.Path != "" { |
| 1201 |
baseUrl.Path = settings.Path |
| 1202 |
} |
| 1203 |
httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) |
| 1204 |
if err != nil { |
| 1205 |
return err |
| 1206 |
} |
| 1207 |
httpReq = httpReq.WithContext(ctx) |
| 1208 |
httpReq.Header = headers |
| 1209 |
|
| 1210 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetOperation") |
| 1211 |
if err != nil { |
| 1212 |
return err |
| 1213 |
} |
| 1214 |
|
| 1215 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 1216 |
return err |
| 1217 |
} |
| 1218 |
|
| 1219 |
return nil |
| 1220 |
}, opts...) |
| 1221 |
if e != nil { |
| 1222 |
return nil, e |
| 1223 |
} |
| 1224 |
return resp, nil |
| 1225 |
} |
| 1226 |
|
| 1227 |
// CreateKeyOperation returns a new CreateKeyOperation from a given name. |
| 1228 |
// The name must be that of a previously created CreateKeyOperation, possibly from a different process. |
| 1229 |
func (c *gRPCClient) CreateKeyOperation(name string) *CreateKeyOperation { |
| 1230 |
return &CreateKeyOperation{ |
| 1231 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
// CreateKeyOperation returns a new CreateKeyOperation from a given name. |
| 1236 |
// The name must be that of a previously created CreateKeyOperation, possibly from a different process. |
| 1237 |
func (c *restClient) CreateKeyOperation(name string) *CreateKeyOperation { |
| 1238 |
override := fmt.Sprintf("/v2/%s", name) |
| 1239 |
return &CreateKeyOperation{ |
| 1240 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1241 |
pollPath: override, |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
// DeleteKeyOperation returns a new DeleteKeyOperation from a given name. |
| 1246 |
// The name must be that of a previously created DeleteKeyOperation, possibly from a different process. |
| 1247 |
func (c *gRPCClient) DeleteKeyOperation(name string) *DeleteKeyOperation { |
| 1248 |
return &DeleteKeyOperation{ |
| 1249 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
// DeleteKeyOperation returns a new DeleteKeyOperation from a given name. |
| 1254 |
// The name must be that of a previously created DeleteKeyOperation, possibly from a different process. |
| 1255 |
func (c *restClient) DeleteKeyOperation(name string) *DeleteKeyOperation { |
| 1256 |
override := fmt.Sprintf("/v2/%s", name) |
| 1257 |
return &DeleteKeyOperation{ |
| 1258 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1259 |
pollPath: override, |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
// UndeleteKeyOperation returns a new UndeleteKeyOperation from a given name. |
| 1264 |
// The name must be that of a previously created UndeleteKeyOperation, possibly from a different process. |
| 1265 |
func (c *gRPCClient) UndeleteKeyOperation(name string) *UndeleteKeyOperation { |
| 1266 |
return &UndeleteKeyOperation{ |
| 1267 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
// UndeleteKeyOperation returns a new UndeleteKeyOperation from a given name. |
| 1272 |
// The name must be that of a previously created UndeleteKeyOperation, possibly from a different process. |
| 1273 |
func (c *restClient) UndeleteKeyOperation(name string) *UndeleteKeyOperation { |
| 1274 |
override := fmt.Sprintf("/v2/%s", name) |
| 1275 |
return &UndeleteKeyOperation{ |
| 1276 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1277 |
pollPath: override, |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
// UpdateKeyOperation returns a new UpdateKeyOperation from a given name. |
| 1282 |
// The name must be that of a previously created UpdateKeyOperation, possibly from a different process. |
| 1283 |
func (c *gRPCClient) UpdateKeyOperation(name string) *UpdateKeyOperation { |
| 1284 |
return &UpdateKeyOperation{ |
| 1285 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
// UpdateKeyOperation returns a new UpdateKeyOperation from a given name. |
| 1290 |
// The name must be that of a previously created UpdateKeyOperation, possibly from a different process. |
| 1291 |
func (c *restClient) UpdateKeyOperation(name string) *UpdateKeyOperation { |
| 1292 |
override := fmt.Sprintf("/v2/%s", name) |
| 1293 |
return &UpdateKeyOperation{ |
| 1294 |
lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), |
| 1295 |
pollPath: override, |
| 1296 |
} |
| 1297 |
} |