Skip to content

Commit 76400c2

Browse files
Copilotjverce
andcommitted
Add async get_raw_access_token() method to AsyncPipedream for non-blocking OAuth token fetching
Co-authored-by: jverce <[email protected]>
1 parent aa4f32e commit 76400c2

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/pipedream/core/client_wrapper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,8 @@ async def async_get_headers(self) -> typing.Dict[str, str]:
120120
token = await self._async_token()
121121
headers["Authorization"] = f"Bearer {token}"
122122
return headers
123+
124+
async def _async_get_token(self) -> typing.Optional[str]:
125+
if self._async_token is not None:
126+
return await self._async_token()
127+
return self._get_token()

src/pipedream/pipedream.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,23 @@ def __init__(
110110
@property
111111
def raw_access_token(self) -> Optional[str]:
112112
"""
113-
Returns an access token that can be used to authenticate API requests
113+
Returns an access token that can be used to authenticate API requests.
114+
115+
Note: When using OAuth authentication (client_id/client_secret), this property
116+
may perform blocking network operations. For async applications, prefer using
117+
the async method `get_raw_access_token()` instead.
114118
"""
115119
return self._client_wrapper._get_token()
116120

121+
async def get_raw_access_token(self) -> Optional[str]:
122+
"""
123+
Asynchronously returns an access token that can be used to authenticate API requests.
124+
125+
This method is non-blocking and safe to use in async contexts such as FastAPI,
126+
Django ASGI, or any other asyncio-based application.
127+
"""
128+
return await self._client_wrapper._async_get_token()
129+
117130

118131
def _get_base_url(environment: PipedreamEnvironment) -> str:
119132
"""

tests/custom/test_client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1+
from unittest.mock import AsyncMock
2+
13
import pytest
24

5+
from pipedream import AsyncPipedream, Pipedream
6+
37

48
# Get started with writing tests with pytest at https://docs.pytest.org
59
@pytest.mark.skip(reason="Unimplemented")
610
def test_client() -> None:
711
assert True
12+
13+
14+
def test_sync_pipedream_raw_access_token() -> None:
15+
"""Test synchronous Pipedream client raw_access_token property."""
16+
client = Pipedream(access_token="test-token", project_id="test-project")
17+
assert client.raw_access_token == "test-token"
18+
19+
20+
def test_async_pipedream_raw_access_token() -> None:
21+
"""Test AsyncPipedream client raw_access_token property with access_token."""
22+
client = AsyncPipedream(access_token="test-token", project_id="test-project")
23+
assert client.raw_access_token == "test-token"
24+
25+
26+
async def test_async_pipedream_get_raw_access_token() -> None:
27+
"""Test AsyncPipedream async method get_raw_access_token() with access_token."""
28+
client = AsyncPipedream(access_token="test-token", project_id="test-project")
29+
token = await client.get_raw_access_token()
30+
assert token == "test-token"
31+
32+
33+
async def test_async_pipedream_get_raw_access_token_with_oauth() -> None:
34+
"""Test AsyncPipedream async method with OAuth flow."""
35+
client = AsyncPipedream(
36+
client_id="test-client-id",
37+
client_secret="test-client-secret",
38+
project_id="test-project",
39+
)
40+
41+
# The client should have _async_token set when using OAuth
42+
assert client._client_wrapper._async_token is not None
43+
44+
# Mock the async token provider
45+
client._client_wrapper._async_token = AsyncMock(return_value="mocked-oauth-token")
46+
47+
# Test the async method
48+
token = await client.get_raw_access_token()
49+
assert token == "mocked-oauth-token"

0 commit comments

Comments
 (0)