Skip to content

feat(api): create access token reads client opts if not provided #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2024
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
38 changes: 33 additions & 5 deletions src/finch/resources/access_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .. import _legacy_response
from ..types import CreateAccessTokenResponse, access_token_create_params
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
from .._utils import maybe_transform
from .._utils import is_given, maybe_transform
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
Expand All @@ -30,10 +30,10 @@ def with_streaming_response(self) -> AccessTokensWithStreamingResponse:
def create(
self,
*,
client_id: str,
client_secret: str,
code: str,
redirect_uri: str,
client_id: str | NotGiven = NOT_GIVEN,
client_secret: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
extra_headers: Headers | None = None,
Expand All @@ -53,6 +53,20 @@ def create(

timeout: Override the client-level default timeout for this request, in seconds
"""
if not is_given(client_id):
if self._client.client_id is None:
raise ValueError(
"client_id must be provided as an argument or with the FINCH_CLIENT_ID environment variable"
)
client_id = self._client.client_id

if not is_given(client_secret):
if self._client.client_secret is None:
raise ValueError(
"client_secret must be provided as an argument or with the FINCH_CLIENT_SECRET environment variable"
)
client_secret = self._client.client_secret

return self._post(
"/auth/token",
body=maybe_transform(
Expand Down Expand Up @@ -83,10 +97,10 @@ def with_streaming_response(self) -> AsyncAccessTokensWithStreamingResponse:
async def create(
self,
*,
client_id: str,
client_secret: str,
code: str,
redirect_uri: str,
client_id: str | NotGiven = NOT_GIVEN,
client_secret: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
extra_headers: Headers | None = None,
Expand All @@ -106,6 +120,20 @@ async def create(

timeout: Override the client-level default timeout for this request, in seconds
"""
if not is_given(client_id):
if self._client.client_id is None:
raise ValueError(
"client_id must be provided as an argument or with the FINCH_CLIENT_ID environment variable"
)
client_id = self._client.client_id

if not is_given(client_secret):
if self._client.client_secret is None:
raise ValueError(
"client_secret must be provided as an argument or with the FINCH_CLIENT_SECRET environment variable"
)
client_secret = self._client.client_secret

return await self._post(
"/auth/token",
body=maybe_transform(
Expand Down
8 changes: 4 additions & 4 deletions src/finch/types/access_token_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


class AccessTokenCreateParams(TypedDict, total=False):
client_id: Required[str]

client_secret: Required[str]

code: Required[str]

redirect_uri: Required[str]

client_id: str

client_secret: str
32 changes: 20 additions & 12 deletions tests/api_resources/test_access_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,24 @@ class TestAccessTokens:
@parametrize
def test_method_create(self, client: Finch) -> None:
access_token = client.access_tokens.create(
client_id="<your_client_id>",
client_secret="<your_client_secret>",
code="<your_authorization_code>",
redirect_uri="https://example.com",
)
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])

@parametrize
def test_raw_response_create(self, client: Finch) -> None:
response = client.access_tokens.with_raw_response.create(
def test_method_create_with_all_params(self, client: Finch) -> None:
access_token = client.access_tokens.create(
code="<your_authorization_code>",
redirect_uri="https://example.com",
client_id="<your_client_id>",
client_secret="<your_client_secret>",
)
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])

@parametrize
def test_raw_response_create(self, client: Finch) -> None:
response = client.access_tokens.with_raw_response.create(
code="<your_authorization_code>",
redirect_uri="https://example.com",
)
Expand All @@ -44,8 +50,6 @@ def test_raw_response_create(self, client: Finch) -> None:
@parametrize
def test_streaming_response_create(self, client: Finch) -> None:
with client.access_tokens.with_streaming_response.create(
client_id="<your_client_id>",
client_secret="<your_client_secret>",
code="<your_authorization_code>",
redirect_uri="https://example.com",
) as response:
Expand All @@ -64,18 +68,24 @@ class TestAsyncAccessTokens:
@parametrize
async def test_method_create(self, async_client: AsyncFinch) -> None:
access_token = await async_client.access_tokens.create(
client_id="<your_client_id>",
client_secret="<your_client_secret>",
code="<your_authorization_code>",
redirect_uri="https://example.com",
)
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])

@parametrize
async def test_raw_response_create(self, async_client: AsyncFinch) -> None:
response = await async_client.access_tokens.with_raw_response.create(
async def test_method_create_with_all_params(self, async_client: AsyncFinch) -> None:
access_token = await async_client.access_tokens.create(
code="<your_authorization_code>",
redirect_uri="https://example.com",
client_id="<your_client_id>",
client_secret="<your_client_secret>",
)
assert_matches_type(CreateAccessTokenResponse, access_token, path=["response"])

@parametrize
async def test_raw_response_create(self, async_client: AsyncFinch) -> None:
response = await async_client.access_tokens.with_raw_response.create(
code="<your_authorization_code>",
redirect_uri="https://example.com",
)
Expand All @@ -88,8 +98,6 @@ async def test_raw_response_create(self, async_client: AsyncFinch) -> None:
@parametrize
async def test_streaming_response_create(self, async_client: AsyncFinch) -> None:
async with async_client.access_tokens.with_streaming_response.create(
client_id="<your_client_id>",
client_secret="<your_client_secret>",
code="<your_authorization_code>",
redirect_uri="https://example.com",
) as response:
Expand Down
18 changes: 16 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def event_loop() -> Iterator[asyncio.AbstractEventLoop]:
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")

access_token = "My Access Token"
client_id = "My Client ID"
client_secret = "My Client Secret"


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

with Finch(base_url=base_url, access_token=access_token, _strict_response_validation=strict) as client:
with Finch(
base_url=base_url,
access_token=access_token,
client_id=client_id,
client_secret=client_secret,
_strict_response_validation=strict,
) as client:
yield client


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

async with AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=strict) as client:
async with AsyncFinch(
base_url=base_url,
access_token=access_token,
client_id=client_id,
client_secret=client_secret,
_strict_response_validation=strict,
) as client:
yield client
Loading