Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Make get_access_token sync compatible #80

Merged
merged 3 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `VertexAICustomTrainingJob` infrastructure block - [#75](https://github.com/PrefectHQ/prefect-gcp/pull/75)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate entry (has already been released)


### Changed

- Made `GcpCredentials.get_access_token` sync compatible - [#80](https://github.com/PrefectHQ/prefect-gcp/pull/80)

### Deprecated

### Removed
Expand Down
3 changes: 2 additions & 1 deletion prefect_gcp/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
pass

from prefect.blocks.core import Block
from prefect.utilities.asyncutils import run_sync_in_worker_thread
from prefect.utilities.asyncutils import run_sync_in_worker_thread, sync_compatible


def _raise_help_msg(key: str):
Expand Down Expand Up @@ -153,6 +153,7 @@ def get_credentials_from_service_account(self) -> Credentials:
credentials, _ = google.auth.default()
return credentials

@sync_compatible
async def get_access_token(self):
"""
See: https://stackoverflow.com/a/69107745
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,19 @@ def destroy_secret_version(self, name, **kwds):
@pytest.fixture
def mock_credentials(monkeypatch):
mock_credentials = MagicMock(name="MockGoogleCredentials")
mock_authenticated_credentials = MagicMock(token="my-token")
mock_credentials.from_service_account_info = mock_authenticated_credentials
mock_credentials.from_service_account_file = mock_authenticated_credentials
monkeypatch.setattr(
"prefect_gcp.cloud_run.GcpCredentials.get_credentials_from_service_account", # noqa
"prefect_gcp.credentials.Credentials", # noqa
mock_credentials,
)
mock_auth = MagicMock()
mock_auth.default.return_value = (mock_authenticated_credentials, "project")
monkeypatch.setattr(
"prefect_gcp.credentials.google.auth", # noqa
mock_auth,
)
return mock_credentials


Expand Down
11 changes: 11 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,14 @@ def test_flow():
}
}
assert test_flow() == expected


async def test_get_access_token_async(gcp_credentials):
print(gcp_credentials.get_credentials_from_service_account().token)
token = await gcp_credentials.get_access_token()
assert token == "my-token"


def test_get_access_token_sync_compatible(gcp_credentials):
token = gcp_credentials.get_access_token()
assert token == "my-token"