Skip to content

Commit 00fea16

Browse files
authored
Merge pull request #139 from mistralai/harizo/chore-improve-gcp-auth
GCP client improvements
2 parents f62530a + b7315e5 commit 00fea16

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed

packages/mistralai_gcp/.speakeasy/gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ python:
1818
pytest: ^8.2.2
1919
pytest-asyncio: ^0.23.7
2020
main:
21-
google-auth: ^2.31.0
21+
google-auth: 2.27.0
2222
requests: ^2.32.3
2323
authors:
2424
- Mistral

packages/mistralai_gcp/poetry.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mistralai_gcp/src/mistralai_gcp/sdk.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(
3232
self,
3333
region: str = "europe-west4",
3434
project_id: Optional[str] = None,
35+
access_token: Optional[str] = None,
3536
client: Optional[HttpClient] = None,
3637
async_client: Optional[AsyncHttpClient] = None,
3738
retry_config: Optional[Nullable[RetryConfig]] = None,
@@ -46,26 +47,30 @@ def __init__(
4647
:param retry_config: The retry configuration to use for all supported methods
4748
"""
4849

49-
credentials, loaded_project_id = google.auth.default(
50-
scopes=["https://www.googleapis.com/auth/cloud-platform"],
51-
)
52-
53-
if not isinstance(credentials, google.auth.credentials.Credentials):
54-
raise models.SDKError(
55-
"credentials must be an instance of google.auth.credentials.Credentials"
50+
if not access_token:
51+
credentials, loaded_project_id = google.auth.default(
52+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
5653
)
54+
credentials.refresh(google.auth.transport.requests.Request())
55+
56+
if not isinstance(credentials, google.auth.credentials.Credentials):
57+
raise models.SDKError(
58+
"credentials must be an instance of google.auth.credentials.Credentials"
59+
)
5760

5861
project_id = project_id or loaded_project_id
5962
if project_id is None:
6063
raise models.SDKError("project_id must be provided")
6164

6265
def auth_token() -> str:
63-
if credentials.expired:
66+
if access_token:
67+
return access_token
68+
else:
6469
credentials.refresh(google.auth.transport.requests.Request())
65-
token = credentials.token
66-
if not token:
67-
raise models.SDKError("Failed to get token from credentials")
68-
return token
70+
token = credentials.token
71+
if not token:
72+
raise models.SDKError("Failed to get token from credentials")
73+
return token
6974

7075
if client is None:
7176
client = httpx.Client()

poetry.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jsonpath-python = "^1.0.6"
2121
pydantic = "~2.8.2"
2222
python-dateutil = "^2.9.0.post0"
2323
typing-inspect = "^0.9.0"
24-
google-auth = { version = "^2.31.0", optional = true }
24+
google-auth = { version = "2.27.0", optional = true }
2525
requests = { version = "^2.32.3", optional = true }
2626

2727
[tool.poetry.group.dev.dependencies]

0 commit comments

Comments
 (0)