1- from contextlib import asynccontextmanager
2- from typing import Any , AsyncIterator , Mapping
1+ from typing import Any , Mapping
32import http
4- import aiohttp
3+
54from pydantic import BaseModel
5+ import httpx
66
77from clerk .errors import ClerkAPIException
88
@@ -15,18 +15,12 @@ class Client:
1515 def __init__ (
1616 self , token : str , base_url : str = "https://api.clerk.dev/v1/" , timeout_seconds : float = 30.0
1717 ) -> None :
18- self ._session = aiohttp . ClientSession (
18+ self ._session = httpx . AsyncClient (
1919 headers = {"Authorization" : f"Bearer { token } " , "Content-Type" : "application/json" },
20- timeout = aiohttp . ClientTimeout ( total = timeout_seconds ),
20+ timeout = httpx . Timeout ( timeout_seconds ),
2121 )
2222 self ._base_url = base_url
2323
24- async def __aenter__ (self ):
25- return self
26-
27- async def __aexit__ (self , exc_type , exc_val , exc_tb ):
28- await self ._session .close ()
29-
3024 @property
3125 def verification (self ):
3226 from clerk .verification import VerificationService
@@ -57,46 +51,39 @@ def organizations(self):
5751
5852 return OrganizationsService (self )
5953
60- @asynccontextmanager
61- async def get (
62- self , endpoint : str , params : Mapping [str , str ] | None = None
63- ) -> AsyncIterator [aiohttp .ClientResponse ]:
64- async with self ._session .get (self ._make_url (endpoint ), params = params ) as r :
65- await self ._check_response_err (r )
66- yield r
54+ async def get (self , endpoint : str , params : Mapping [str , str ] | None = None ) -> httpx .Response :
55+ r = await self ._session .get (self ._make_url (endpoint ), params = params )
56+ await self ._check_response_err (r )
57+ return r
6758
68- @asynccontextmanager
6959 async def post (
7060 self , endpoint : str , request : BaseModel | None = None , json : Any = None
71- ) -> AsyncIterator [ aiohttp . ClientResponse ] :
72- async with self ._session .post (
61+ ) -> httpx . Response :
62+ r = await self ._session .post (
7363 self ._make_url (endpoint ),
74- data = request and request .model_dump_json (),
64+ data = request .model_dump_json () if request else None ,
7565 json = json ,
76- ) as r :
77- await self ._check_response_err (r )
78- yield r
66+ )
67+ await self ._check_response_err (r )
68+ return r
7969
80- @asynccontextmanager
81- async def delete (self , endpoint : str ) -> AsyncIterator [aiohttp .ClientResponse ]:
82- async with self ._session .delete (self ._make_url (endpoint )) as r :
83- await self ._check_response_err (r )
84- yield r
70+ async def delete (self , endpoint : str ) -> httpx .Response :
71+ r = await self ._session .delete (self ._make_url (endpoint ))
72+ await self ._check_response_err (r )
73+ return r
8574
86- @asynccontextmanager
8775 async def patch (
8876 self , endpoint : str , request : BaseModel | None = None , json : Any = None
89- ) -> AsyncIterator [ aiohttp . ClientResponse ] :
90- async with self ._session .patch (
77+ ) -> httpx . Response :
78+ r = await self ._session .patch (
9179 self ._make_url (endpoint ), data = request and request .model_dump_json (), json = json
92- ) as r :
93- await self ._check_response_err (r )
94- yield r
95-
96- async def _check_response_err (self , r : aiohttp .ClientResponse ):
97- if http .HTTPStatus .OK <= r .status < http .HTTPStatus .BAD_REQUEST :
98- return # no error
99- raise await ClerkAPIException .from_response (r )
80+ )
81+ await self ._check_response_err (r )
82+ return r
83+
84+ async def _check_response_err (self , r : httpx .Response ):
85+ if not http .HTTPStatus .OK <= r .status_code < http .HTTPStatus .BAD_REQUEST :
86+ raise await ClerkAPIException .from_response (r )
10087
10188 def _make_url (self , endpoint : str ) -> str :
10289 return f"{ self ._base_url .rstrip ('/' )} /{ endpoint .strip ('/' )} /"
0 commit comments