Skip to content

Commit bc88e91

Browse files
author
Cyrus Nouroozi
committed
Update pytest* to latest, pydantic=2
1 parent ae88b30 commit bc88e91

File tree

14 files changed

+310
-255
lines changed

14 files changed

+310
-255
lines changed

clerk/client.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import http
21
from contextlib import asynccontextmanager
3-
from typing import Any, Mapping, Optional
4-
2+
from typing import Any, AsyncIterator, Mapping
3+
import http
54
import aiohttp
5+
from pydantic import BaseModel
66

77
from clerk.errors import ClerkAPIException
88

@@ -59,31 +59,37 @@ def organizations(self):
5959

6060
@asynccontextmanager
6161
async def get(
62-
self, endpoint: str, params: Optional[Mapping[str, str]] = None
63-
) -> aiohttp.ClientResponse:
62+
self, endpoint: str, params: Mapping[str, str] | None = None
63+
) -> AsyncIterator[aiohttp.ClientResponse]:
6464
async with self._session.get(self._make_url(endpoint), params=params) as r:
6565
await self._check_response_err(r)
6666
yield r
6767

6868
@asynccontextmanager
6969
async def post(
70-
self, endpoint: str, data: Any = None, json: Any = None
71-
) -> aiohttp.ClientResponse:
72-
async with self._session.post(self._make_url(endpoint), data=data, json=json) as r:
70+
self, endpoint: str, request: BaseModel | None = None, json: Any = None
71+
) -> AsyncIterator[aiohttp.ClientResponse]:
72+
async with self._session.post(
73+
self._make_url(endpoint),
74+
data=request and request.model_dump_json(),
75+
json=json,
76+
) as r:
7377
await self._check_response_err(r)
7478
yield r
7579

7680
@asynccontextmanager
77-
async def delete(self, endpoint: str) -> aiohttp.ClientResponse:
81+
async def delete(self, endpoint: str) -> AsyncIterator[aiohttp.ClientResponse]:
7882
async with self._session.delete(self._make_url(endpoint)) as r:
7983
await self._check_response_err(r)
8084
yield r
8185

8286
@asynccontextmanager
8387
async def patch(
84-
self, endpoint: str, data: Any = None, json: Any = None
85-
) -> aiohttp.ClientResponse:
86-
async with self._session.patch(self._make_url(endpoint), data=data, json=json) as r:
88+
self, endpoint: str, request: BaseModel | None = None, json: Any = None
89+
) -> AsyncIterator[aiohttp.ClientResponse]:
90+
async with self._session.patch(
91+
self._make_url(endpoint), data=request and request.model_dump_json(), json=json
92+
) as r:
8793
await self._check_response_err(r)
8894
yield r
8995

clerk/clients.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ class ClientsService(Service):
1111
async def list(self) -> List[types.Client]:
1212
"""Retrieve a list of all clients"""
1313
async with self._client.get(self.endpoint) as r:
14-
return [types.Client.parse_obj(s) for s in await r.json()]
14+
return [types.Client.model_validate(s) for s in await r.json()]
1515

1616
async def get(self, client_id: str) -> types.Client:
1717
"""Retrieve a client by its id"""
1818
async with self._client.get(f"{self.endpoint}/{client_id}") as r:
19-
return types.Client.parse_obj(await r.json())
19+
return types.Client.model_validate(await r.json())
2020

2121
async def verify(self, token: str) -> types.Client:
2222
"""Verify a token and return its associated client, if valid"""
2323
request = types.VerifyRequest(token=token)
2424

25-
async with self._client.post(self.verify_endpoint, data=request.json()) as r:
26-
return types.Client.parse_obj(await r.json())
25+
async with self._client.post(self.verify_endpoint, request=request) as r:
26+
return types.Client.model_validate(await r.json())

clerk/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def from_response(cls, resp: aiohttp.ClientResponse) -> "ClerkAPIException
2121
api_errors = []
2222
else:
2323
errors = data.get("errors", [])
24-
api_errors = [types.Error.parse_obj(e) for e in errors]
24+
api_errors = [types.Error.model_validate(e) for e in errors]
2525

2626
return ClerkAPIException(resp.status, resp.method, str(resp.url), *api_errors)
2727

clerk/organizations.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ async def list(self) -> List[types.Organization]:
1111
"""Retrieve a list of all organizations"""
1212
async with self._client.get(self.endpoint) as r:
1313
s = await r.json()
14-
return [types.Organization.parse_obj(s) for s in s.get("data")]
14+
return [types.Organization.model_validate(s) for s in s.get("data")]
1515

1616
async def get(self, organization_id: str) -> types.Organization:
1717
"""Retrieve an organization by their id"""
1818
async with self._client.get(f"{self.endpoint}/{organization_id}") as r:
19-
return types.Organization.parse_obj(await r.json())
19+
return types.Organization.model_validate(await r.json())
2020

2121
async def delete(self, organization_id: str) -> types.DeleteOrganizationResponse:
2222
"""Delete an organization by their id"""
2323
async with self._client.delete(f"{self.endpoint}/{organization_id}") as r:
24-
return types.DeleteOrganizationResponse.parse_obj(await r.json())
24+
return types.DeleteOrganizationResponse.model_validate(await r.json())
2525

26-
async def update(self, organization_id: str, request: types.UpdateOrganizationRequest) -> types.Organization:
26+
async def update(
27+
self, organization_id: str, request: types.UpdateOrganizationRequest
28+
) -> types.Organization:
2729
"""Update an organization by their id"""
2830
async with self._client.patch(
29-
f"{self.endpoint}/{organization_id}", data=request.json(exclude_unset=True)
31+
f"{self.endpoint}/{organization_id}", json=request.model_dump_json(exclude_unset=True)
3032
) as r:
31-
return types.Organization.parse_obj(await r.json())
33+
return types.Organization.model_validate(await r.json())

clerk/sessions.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@ class SessionsService(Service):
1010
async def list(self) -> List[types.Session]:
1111
"""Retrieve a list of all sessions"""
1212
async with self._client.get(self.endpoint) as r:
13-
return [types.Session.parse_obj(s) for s in await r.json()]
13+
return [types.Session.model_validate(s) for s in await r.json()]
1414

1515
async def get(self, session_id: str) -> types.Session:
1616
"""Retrieve a session by its id"""
1717
async with self._client.get(f"{self.endpoint}/{session_id}") as r:
18-
return types.Session.parse_obj(await r.json())
18+
return types.Session.model_validate(await r.json())
1919

2020
async def revoke(self, session_id: str) -> types.Session:
2121
"""Revoke a session by its id"""
2222
async with self._client.post(f"{self.endpoint}/{session_id}/revoke") as r:
23-
return types.Session.parse_obj(await r.json())
23+
return types.Session.model_validate(await r.json())
2424

2525
async def verify(self, session_id: str, token: str) -> types.Session:
2626
"""Verify a session by its id and a given token"""
2727
request = types.VerifyRequest(token=token)
2828

29-
async with self._client.post(
30-
f"{self.endpoint}/{session_id}/verify", data=request.json()
31-
) as r:
32-
return types.Session.parse_obj(await r.json())
29+
async with self._client.post(f"{self.endpoint}/{session_id}/verify", request=request) as r:
30+
return types.Session.model_validate(await r.json())

clerk/users.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ class UsersService(Service):
1010
async def list(self) -> List[types.User]:
1111
"""Retrieve a list of all users"""
1212
async with self._client.get(self.endpoint) as r:
13-
return [types.User.parse_obj(s) for s in await r.json()]
13+
return [types.User.model_validate(s) for s in await r.json()]
1414

1515
async def get(self, user_id: str) -> types.User:
1616
"""Retrieve a user by their id"""
1717
async with self._client.get(f"{self.endpoint}/{user_id}") as r:
18-
return types.User.parse_obj(await r.json())
18+
return types.User.model_validate(await r.json())
1919

2020
async def delete(self, user_id: str) -> types.DeleteUserResponse:
2121
"""Delete a user by their id"""
2222
async with self._client.delete(f"{self.endpoint}/{user_id}") as r:
23-
return types.DeleteUserResponse.parse_obj(await r.json())
23+
return types.DeleteUserResponse.model_validate(await r.json())
2424

2525
async def update(self, user_id: str, request: types.UpdateUserRequest) -> types.User:
2626
"""Update a user by their id"""
2727
async with self._client.patch(
28-
f"{self.endpoint}/{user_id}", data=request.json(exclude_unset=True)
28+
f"{self.endpoint}/{user_id}", json=request.model_dump_json(exclude_unset=True)
2929
) as r:
30-
return types.User.parse_obj(await r.json())
30+
return types.User.model_validate(await r.json())

0 commit comments

Comments
 (0)