Skip to content

Commit ee9b890

Browse files
feat(api): create access token reads client opts if not provided (#281)
1 parent 9700447 commit ee9b890

File tree

5 files changed

+365
-57
lines changed

5 files changed

+365
-57
lines changed

src/finch/resources/access_tokens.py

+33-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .. import _legacy_response
88
from ..types import CreateAccessTokenResponse, access_token_create_params
99
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
10-
from .._utils import maybe_transform
10+
from .._utils import is_given, maybe_transform
1111
from .._compat import cached_property
1212
from .._resource import SyncAPIResource, AsyncAPIResource
1313
from .._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
@@ -30,10 +30,10 @@ def with_streaming_response(self) -> AccessTokensWithStreamingResponse:
3030
def create(
3131
self,
3232
*,
33-
client_id: str,
34-
client_secret: str,
3533
code: str,
3634
redirect_uri: str,
35+
client_id: str | NotGiven = NOT_GIVEN,
36+
client_secret: str | NotGiven = NOT_GIVEN,
3737
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
3838
# The extra values given here take precedence over values defined on the client or passed to this method.
3939
extra_headers: Headers | None = None,
@@ -53,6 +53,20 @@ def create(
5353
5454
timeout: Override the client-level default timeout for this request, in seconds
5555
"""
56+
if not is_given(client_id):
57+
if self._client.client_id is None:
58+
raise ValueError(
59+
"client_id must be provided as an argument or with the FINCH_CLIENT_ID environment variable"
60+
)
61+
client_id = self._client.client_id
62+
63+
if not is_given(client_secret):
64+
if self._client.client_secret is None:
65+
raise ValueError(
66+
"client_secret must be provided as an argument or with the FINCH_CLIENT_SECRET environment variable"
67+
)
68+
client_secret = self._client.client_secret
69+
5670
return self._post(
5771
"/auth/token",
5872
body=maybe_transform(
@@ -83,10 +97,10 @@ def with_streaming_response(self) -> AsyncAccessTokensWithStreamingResponse:
8397
async def create(
8498
self,
8599
*,
86-
client_id: str,
87-
client_secret: str,
88100
code: str,
89101
redirect_uri: str,
102+
client_id: str | NotGiven = NOT_GIVEN,
103+
client_secret: str | NotGiven = NOT_GIVEN,
90104
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
91105
# The extra values given here take precedence over values defined on the client or passed to this method.
92106
extra_headers: Headers | None = None,
@@ -106,6 +120,20 @@ async def create(
106120
107121
timeout: Override the client-level default timeout for this request, in seconds
108122
"""
123+
if not is_given(client_id):
124+
if self._client.client_id is None:
125+
raise ValueError(
126+
"client_id must be provided as an argument or with the FINCH_CLIENT_ID environment variable"
127+
)
128+
client_id = self._client.client_id
129+
130+
if not is_given(client_secret):
131+
if self._client.client_secret is None:
132+
raise ValueError(
133+
"client_secret must be provided as an argument or with the FINCH_CLIENT_SECRET environment variable"
134+
)
135+
client_secret = self._client.client_secret
136+
109137
return await self._post(
110138
"/auth/token",
111139
body=maybe_transform(

src/finch/types/access_token_create_params.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99

1010
class AccessTokenCreateParams(TypedDict, total=False):
11-
client_id: Required[str]
12-
13-
client_secret: Required[str]
14-
1511
code: Required[str]
1612

1713
redirect_uri: Required[str]
14+
15+
client_id: str
16+
17+
client_secret: str

tests/api_resources/test_access_tokens.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@ class TestAccessTokens:
2020
@parametrize
2121
def test_method_create(self, client: Finch) -> None:
2222
access_token = client.access_tokens.create(
23-
client_id="<your_client_id>",
24-
client_secret="<your_client_secret>",
2523
code="<your_authorization_code>",
2624
redirect_uri="https://example.com",
2725
)
2826
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])
2927

3028
@parametrize
31-
def test_raw_response_create(self, client: Finch) -> None:
32-
response = client.access_tokens.with_raw_response.create(
29+
def test_method_create_with_all_params(self, client: Finch) -> None:
30+
access_token = client.access_tokens.create(
31+
code="<your_authorization_code>",
32+
redirect_uri="https://example.com",
3333
client_id="<your_client_id>",
3434
client_secret="<your_client_secret>",
35+
)
36+
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])
37+
38+
@parametrize
39+
def test_raw_response_create(self, client: Finch) -> None:
40+
response = client.access_tokens.with_raw_response.create(
3541
code="<your_authorization_code>",
3642
redirect_uri="https://example.com",
3743
)
@@ -44,8 +50,6 @@ def test_raw_response_create(self, client: Finch) -> None:
4450
@parametrize
4551
def test_streaming_response_create(self, client: Finch) -> None:
4652
with client.access_tokens.with_streaming_response.create(
47-
client_id="<your_client_id>",
48-
client_secret="<your_client_secret>",
4953
code="<your_authorization_code>",
5054
redirect_uri="https://example.com",
5155
) as response:
@@ -64,18 +68,24 @@ class TestAsyncAccessTokens:
6468
@parametrize
6569
async def test_method_create(self, async_client: AsyncFinch) -> None:
6670
access_token = await async_client.access_tokens.create(
67-
client_id="<your_client_id>",
68-
client_secret="<your_client_secret>",
6971
code="<your_authorization_code>",
7072
redirect_uri="https://example.com",
7173
)
7274
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])
7375

7476
@parametrize
75-
async def test_raw_response_create(self, async_client: AsyncFinch) -> None:
76-
response = await async_client.access_tokens.with_raw_response.create(
77+
async def test_method_create_with_all_params(self, async_client: AsyncFinch) -> None:
78+
access_token = await async_client.access_tokens.create(
79+
code="<your_authorization_code>",
80+
redirect_uri="https://example.com",
7781
client_id="<your_client_id>",
7882
client_secret="<your_client_secret>",
83+
)
84+
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])
85+
86+
@parametrize
87+
async def test_raw_response_create(self, async_client: AsyncFinch) -> None:
88+
response = await async_client.access_tokens.with_raw_response.create(
7989
code="<your_authorization_code>",
8090
redirect_uri="https://example.com",
8191
)
@@ -88,8 +98,6 @@ async def test_raw_response_create(self, async_client: AsyncFinch) -> None:
8898
@parametrize
8999
async def test_streaming_response_create(self, async_client: AsyncFinch) -> None:
90100
async with async_client.access_tokens.with_streaming_response.create(
91-
client_id="<your_client_id>",
92-
client_secret="<your_client_secret>",
93101
code="<your_authorization_code>",
94102
redirect_uri="https://example.com",
95103
) as response:

tests/conftest.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def event_loop() -> Iterator[asyncio.AbstractEventLoop]:
2727
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
2828

2929
access_token = "My Access Token"
30+
client_id = "My Client ID"
31+
client_secret = "My Client Secret"
3032

3133

3234
@pytest.fixture(scope="session")
@@ -35,7 +37,13 @@ def client(request: FixtureRequest) -> Iterator[Finch]:
3537
if not isinstance(strict, bool):
3638
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
3739

38-
with Finch(base_url=base_url, access_token=access_token, _strict_response_validation=strict) as client:
40+
with Finch(
41+
base_url=base_url,
42+
access_token=access_token,
43+
client_id=client_id,
44+
client_secret=client_secret,
45+
_strict_response_validation=strict,
46+
) as client:
3947
yield client
4048

4149

@@ -45,5 +53,11 @@ async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncFinch]:
4553
if not isinstance(strict, bool):
4654
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
4755

48-
async with AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=strict) as client:
56+
async with AsyncFinch(
57+
base_url=base_url,
58+
access_token=access_token,
59+
client_id=client_id,
60+
client_secret=client_secret,
61+
_strict_response_validation=strict,
62+
) as client:
4963
yield client

0 commit comments

Comments
 (0)