From b3c8fea49e634e04b1ec94d7a11ba8dd9ff627cf Mon Sep 17 00:00:00 2001 From: Kevin James Date: Wed, 15 Nov 2023 09:30:00 +0000 Subject: [PATCH] perf(auth): avoid unnecessary token refresh on get_project() --- auth/gcloud/aio/auth/token.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/auth/gcloud/aio/auth/token.py b/auth/gcloud/aio/auth/token.py index 80c79c186..48f7013ea 100644 --- a/auth/gcloud/aio/auth/token.py +++ b/auth/gcloud/aio/auth/token.py @@ -183,6 +183,8 @@ async def get_project(self) -> Optional[str]: or os.environ.get('GCLOUD_PROJECT') or os.environ.get('APPLICATION_ID') ) + if project: + return project if self.token_type == Type.GCE_METADATA: await self.ensure_token() @@ -191,16 +193,15 @@ async def get_project(self) -> Optional[str]: headers=GCE_METADATA_HEADERS, ) - if not project: - try: - project = await resp.text() - except (AttributeError, TypeError): - project = str(resp.text) + try: + return await resp.text() + except (AttributeError, TypeError): + return str(resp.text) - elif self.token_type == Type.SERVICE_ACCOUNT: - project = project or self.service_data.get('project_id') + if self.token_type == Type.SERVICE_ACCOUNT: + return self.service_data.get('project_id') - return project + return None async def get(self) -> Optional[str]: await self.ensure_token()