Skip to content

Commit

Permalink
perf(auth): avoid unnecessary token refresh on get_project()
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKevJames committed Nov 15, 2023
1 parent 14d5d3b commit b3c8fea
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions auth/gcloud/aio/auth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit b3c8fea

Please sign in to comment.