@@ -66,15 +66,7 @@ async def get(self, url: str, params: Optional[Dict] = None) -> Any:
6666 httpx.HTTPError: If the request fails
6767 ValueError: If the response is not valid JSON
6868 """
69- async with httpx .AsyncClient () as client :
70- response = await client .get (
71- self .build_full_url (url ),
72- headers = self .headers ,
73- params = params ,
74- timeout = httpx .Timeout (30 )
75- )
76- response .raise_for_status ()
77- return response .json ()
69+ return await self ._send_request ("GET" , url , params = params )
7870
7971 async def post (self , url : str , data : Dict ) -> Any :
8072 """Make a POST request to the Extend API.
@@ -90,15 +82,7 @@ async def post(self, url: str, data: Dict) -> Any:
9082 httpx.HTTPError: If the request fails
9183 ValueError: If the response is not valid JSON
9284 """
93- async with httpx .AsyncClient () as client :
94- response = await client .post (
95- self .build_full_url (url ),
96- headers = self .headers ,
97- json = data ,
98- timeout = httpx .Timeout (30 )
99- )
100- response .raise_for_status ()
101- return response .json ()
85+ return await self ._send_request ("POST" , url , json = data )
10286
10387 async def put (self , url : str , data : Dict ) -> Any :
10488 """Make a PUT request to the Extend API.
@@ -114,15 +98,7 @@ async def put(self, url: str, data: Dict) -> Any:
11498 httpx.HTTPError: If the request fails
11599 ValueError: If the response is not valid JSON
116100 """
117- async with httpx .AsyncClient () as client :
118- response = await client .put (
119- self .build_full_url (url ),
120- headers = self .headers ,
121- json = data ,
122- timeout = httpx .Timeout (30 )
123- )
124- response .raise_for_status ()
125- return response .json ()
101+ return await self ._send_request ("PUT" , url , json = data )
126102
127103 async def patch (self , url : str , data : Dict ) -> Any :
128104 """Make a PATCH request to the Extend API.
@@ -138,15 +114,7 @@ async def patch(self, url: str, data: Dict) -> Any:
138114 httpx.HTTPError: If the request fails
139115 ValueError: If the response is not valid JSON
140116 """
141- async with httpx .AsyncClient () as client :
142- response = await client .patch (
143- self .build_full_url (url ),
144- headers = self .headers ,
145- json = data ,
146- timeout = httpx .Timeout (30 )
147- )
148- response .raise_for_status ()
149- return response .json ()
117+ return await self ._send_request ("PATCH" , url , json = data )
150118
151119 async def post_multipart (
152120 self ,
@@ -173,16 +141,34 @@ async def post_multipart(
173141 """
174142 # When sending multipart data, we pass `data` (for non-file fields)
175143 # and `files` (for file uploads) separately.
144+ return await self ._send_request ("POST" , url , data = data , files = files )
145+
146+ def build_full_url (self , url : Optional [str ]):
147+ return f"https://{ API_HOST } { url or '' } "
148+
149+ async def _send_request (
150+ self ,
151+ method : str ,
152+ url : str ,
153+ * ,
154+ params : Optional [Dict ] = None ,
155+ json : Optional [Dict ] = None ,
156+ data : Optional [Dict ] = None ,
157+ files : Optional [Dict ] = None
158+ ) -> Any :
176159 async with httpx .AsyncClient () as client :
177- response = await client .post (
178- self .build_full_url (url ),
160+ response = await client .request (
161+ method = method .upper (),
162+ url = self .build_full_url (url ),
179163 headers = self .headers ,
164+ params = params ,
165+ json = json ,
180166 data = data ,
181167 files = files ,
182168 timeout = httpx .Timeout (30 )
183169 )
184170 response .raise_for_status ()
185- return response .json ()
186171
187- def build_full_url (self , url : Optional [str ]):
188- return f"https://{ API_HOST } { url or '' } "
172+ if response .content :
173+ return response .json ()
174+ return None
0 commit comments