@@ -74,7 +74,7 @@ def _set_project_as_env_var_or_google_auth_default(self): |
74 | 74 | the project and credentials have already been set. |
75 | 75 | """ |
76 | 76 |
|
77 | | - if not self._project: |
| 77 | + if not self._project and not self._api_key: |
78 | 78 | # Project is not set. Trying to get it from the environment. |
79 | 79 | # See https://github.com/googleapis/python-aiplatform/issues/852 |
80 | 80 | # See https://github.com/googleapis/google-auth-library-python/issues/924 |
@@ -104,7 +104,7 @@ def _set_project_as_env_var_or_google_auth_default(self): |
104 | 104 | self._credentials = self._credentials or credentials |
105 | 105 | self._project = project |
106 | 106 |
|
107 | | - if not self._credentials: |
| 107 | + if not self._credentials and not self._api_key: |
108 | 108 | credentials, _ = google.auth.default() |
109 | 109 | self._credentials = credentials |
110 | 110 |
|
@@ -117,6 +117,7 @@ def __init__(self): |
117 | 117 | self._network = None |
118 | 118 | self._service_account = None |
119 | 119 | self._api_endpoint = None |
| 120 | + self._api_key = None |
120 | 121 | self._api_transport = None |
121 | 122 | self._request_metadata = None |
122 | 123 | self._resource_type = None |
@@ -137,6 +138,7 @@ def init( |
137 | 138 | network: Optional[str] = None, |
138 | 139 | service_account: Optional[str] = None, |
139 | 140 | api_endpoint: Optional[str] = None, |
| 141 | + api_key: Optional[str] = None, |
140 | 142 | api_transport: Optional[str] = None, |
141 | 143 | request_metadata: Optional[Sequence[Tuple[str, str]]] = None, |
142 | 144 | ): |
@@ -197,6 +199,9 @@ def init( |
197 | 199 | api_endpoint (str): |
198 | 200 | Optional. The desired API endpoint, |
199 | 201 | e.g., us-central1-aiplatform.googleapis.com |
| 202 | + api_key (str): |
| 203 | + Optional. The API key to use for service calls. |
| 204 | + NOTE: Not all services support API keys. |
200 | 205 | api_transport (str): |
201 | 206 | Optional. The transport method which is either 'grpc' or 'rest'. |
202 | 207 | NOTE: "rest" transport functionality is currently in a |
@@ -252,6 +257,8 @@ def init( |
252 | 257 | self._service_account = service_account |
253 | 258 | if request_metadata is not None: |
254 | 259 | self._request_metadata = request_metadata |
| 260 | + if api_key is not None: |
| 261 | + self._api_key = api_key |
255 | 262 | self._resource_type = None |
256 | 263 |
|
257 | 264 | # Finally, perform secondary state updates |
@@ -304,6 +311,11 @@ def api_endpoint(self) -> Optional[str]: |
304 | 311 | """Default API endpoint, if provided.""" |
305 | 312 | return self._api_endpoint |
306 | 313 |
|
| 314 | + @property |
| 315 | + def api_key(self) -> Optional[str]: |
| 316 | + """API Key, if provided.""" |
| 317 | + return self._api_key |
| 318 | + |
307 | 319 | @property |
308 | 320 | def project(self) -> str: |
309 | 321 | """Default project.""" |
@@ -325,7 +337,7 @@ def project(self) -> str: |
325 | 337 | except GoogleAuthError as exc: |
326 | 338 | raise GoogleAuthError(project_not_found_exception_str) from exc |
327 | 339 |
|
328 | | - if not project_id: |
| 340 | + if not project_id and not self.api_key: |
329 | 341 | raise ValueError(project_not_found_exception_str) |
330 | 342 |
|
331 | 343 | return project_id |
@@ -403,6 +415,7 @@ def get_client_options( |
403 | 415 | location_override: Optional[str] = None, |
404 | 416 | prediction_client: bool = False, |
405 | 417 | api_base_path_override: Optional[str] = None, |
| 418 | + api_key: Optional[str] = None, |
406 | 419 | api_path_override: Optional[str] = None, |
407 | 420 | ) -> client_options.ClientOptions: |
408 | 421 | """Creates GAPIC client_options using location and type. |
@@ -414,6 +427,7 @@ def get_client_options( |
414 | 427 | Vertex AI. |
415 | 428 | prediction_client (str): Optional. flag to use a prediction endpoint. |
416 | 429 | api_base_path_override (str): Optional. Override default API base path. |
| 430 | + api_key (str): Optional. API key to use for the client. |
417 | 431 | api_path_override (str): Optional. Override default api path. |
418 | 432 | Returns: |
419 | 433 | clients_options (google.api_core.client_options.ClientOptions): |
@@ -447,6 +461,11 @@ def get_client_options( |
447 | 461 | else api_path_override |
448 | 462 | ) |
449 | 463 |
|
| 464 | + # Project/location take precedence over api_key |
| 465 | + if api_key and not self._project: |
| 466 | + return client_options.ClientOptions( |
| 467 | + api_endpoint=api_endpoint, api_key=api_key |
| 468 | + ) |
450 | 469 | return client_options.ClientOptions(api_endpoint=api_endpoint) |
451 | 470 |
|
452 | 471 | def common_location_path( |
@@ -479,6 +498,7 @@ def create_client( |
479 | 498 | location_override: Optional[str] = None, |
480 | 499 | prediction_client: bool = False, |
481 | 500 | api_base_path_override: Optional[str] = None, |
| 501 | + api_key: Optional[str] = None, |
482 | 502 | api_path_override: Optional[str] = None, |
483 | 503 | appended_user_agent: Optional[List[str]] = None, |
484 | 504 | appended_gapic_version: Optional[str] = None, |
@@ -493,6 +513,7 @@ def create_client( |
493 | 513 | Optional. Custom auth credentials. If not provided will use the current config. |
494 | 514 | location_override (str): Optional. location override. |
495 | 515 | prediction_client (str): Optional. flag to use a prediction endpoint. |
| 516 | + api_key (str): Optional. API key to use for the client. |
496 | 517 | api_base_path_override (str): Optional. Override default api base path. |
497 | 518 | api_path_override (str): Optional. Override default api path. |
498 | 519 | appended_user_agent (List[str]): |
@@ -539,6 +560,7 @@ def create_client( |
539 | 560 | "client_options": self.get_client_options( |
540 | 561 | location_override=location_override, |
541 | 562 | prediction_client=prediction_client, |
| 563 | + api_key=api_key, |
542 | 564 | api_base_path_override=api_base_path_override, |
543 | 565 | api_path_override=api_path_override, |
544 | 566 | ), |
|