|
16 | 16 |
|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import threading |
19 | 20 | from typing import Literal, Optional, Sequence, Tuple |
20 | 21 | import warnings |
21 | 22 |
|
22 | 23 | import google.auth.credentials |
| 24 | +import google.auth.transport.requests |
23 | 25 | import requests.adapters |
24 | 26 |
|
| 27 | +import bigframes._config.auth |
25 | 28 | import bigframes._importing |
26 | 29 | import bigframes.enums |
27 | 30 | import bigframes.exceptions as bfe |
|
37 | 40 |
|
38 | 41 | def _get_validated_location(value: Optional[str]) -> Optional[str]: |
39 | 42 | import bigframes._tools.strings |
| 43 | + import bigframes.constants |
40 | 44 |
|
41 | 45 | if value is None or value in bigframes.constants.ALL_BIGQUERY_LOCATIONS: |
42 | 46 | return value |
@@ -97,6 +101,7 @@ def __init__( |
97 | 101 | ] = (), |
98 | 102 | enable_polars_execution: bool = False, |
99 | 103 | ): |
| 104 | + self._credentials_and_project_lock = threading.Lock() |
100 | 105 | self._credentials = credentials |
101 | 106 | self._project = project |
102 | 107 | self._location = _get_validated_location(location) |
@@ -141,15 +146,46 @@ def application_name(self, value: Optional[str]): |
141 | 146 | ) |
142 | 147 | self._application_name = value |
143 | 148 |
|
| 149 | + def _try_set_default_credentials_and_project( |
| 150 | + self, |
| 151 | + ) -> tuple[google.auth.credentials.Credentials, Optional[str]]: |
| 152 | + with self._credentials_and_project_lock: |
| 153 | + # Don't fetch credentials or project if credentials is already set. |
| 154 | + # If it's set, we've already authenticated, so if the user wants to |
| 155 | + # re-auth, they should explicitly reset the credentials. |
| 156 | + if self._credentials is not None: |
| 157 | + return self._credentials, self._project |
| 158 | + |
| 159 | + ( |
| 160 | + credentials, |
| 161 | + credentials_project, |
| 162 | + ) = bigframes._config.auth.get_default_credentials_with_project() |
| 163 | + |
| 164 | + # Ensure an access token is available. |
| 165 | + credentials.refresh(google.auth.transport.requests.Request()) |
| 166 | + self._credentials = credentials |
| 167 | + |
| 168 | + # Avoid overriding an explicitly set project with a default value. |
| 169 | + if self._project is None: |
| 170 | + self._project = credentials_project |
| 171 | + |
| 172 | + return credentials, credentials_project |
| 173 | + |
144 | 174 | @property |
145 | | - def credentials(self) -> Optional[google.auth.credentials.Credentials]: |
| 175 | + def credentials(self) -> google.auth.credentials.Credentials: |
146 | 176 | """The OAuth2 credentials to use for this client. |
147 | 177 |
|
| 178 | + Set to None to force re-authentication. |
| 179 | +
|
148 | 180 | Returns: |
149 | 181 | None or google.auth.credentials.Credentials: |
150 | 182 | google.auth.credentials.Credentials if exists; otherwise None. |
151 | 183 | """ |
152 | | - return self._credentials |
| 184 | + if self._credentials: |
| 185 | + return self._credentials |
| 186 | + |
| 187 | + credentials, _ = self._try_set_default_credentials_and_project() |
| 188 | + return credentials |
153 | 189 |
|
154 | 190 | @credentials.setter |
155 | 191 | def credentials(self, value: Optional[google.auth.credentials.Credentials]): |
@@ -183,7 +219,11 @@ def project(self) -> Optional[str]: |
183 | 219 | None or str: |
184 | 220 | Google Cloud project ID as a string; otherwise None. |
185 | 221 | """ |
186 | | - return self._project |
| 222 | + if self._project: |
| 223 | + return self._project |
| 224 | + |
| 225 | + _, project = self._try_set_default_credentials_and_project() |
| 226 | + return project |
187 | 227 |
|
188 | 228 | @project.setter |
189 | 229 | def project(self, value: Optional[str]): |
|
0 commit comments