@@ -66,15 +66,7 @@ async def get(self, url: str, params: Optional[Dict] = None) -> Any:
66
66
httpx.HTTPError: If the request fails
67
67
ValueError: If the response is not valid JSON
68
68
"""
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 )
78
70
79
71
async def post (self , url : str , data : Dict ) -> Any :
80
72
"""Make a POST request to the Extend API.
@@ -90,15 +82,7 @@ async def post(self, url: str, data: Dict) -> Any:
90
82
httpx.HTTPError: If the request fails
91
83
ValueError: If the response is not valid JSON
92
84
"""
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 )
102
86
103
87
async def put (self , url : str , data : Dict ) -> Any :
104
88
"""Make a PUT request to the Extend API.
@@ -114,15 +98,7 @@ async def put(self, url: str, data: Dict) -> Any:
114
98
httpx.HTTPError: If the request fails
115
99
ValueError: If the response is not valid JSON
116
100
"""
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 )
126
102
127
103
async def patch (self , url : str , data : Dict ) -> Any :
128
104
"""Make a PATCH request to the Extend API.
@@ -138,15 +114,7 @@ async def patch(self, url: str, data: Dict) -> Any:
138
114
httpx.HTTPError: If the request fails
139
115
ValueError: If the response is not valid JSON
140
116
"""
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 )
150
118
151
119
async def post_multipart (
152
120
self ,
@@ -173,16 +141,34 @@ async def post_multipart(
173
141
"""
174
142
# When sending multipart data, we pass `data` (for non-file fields)
175
143
# 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 :
176
159
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 ),
179
163
headers = self .headers ,
164
+ params = params ,
165
+ json = json ,
180
166
data = data ,
181
167
files = files ,
182
168
timeout = httpx .Timeout (30 )
183
169
)
184
170
response .raise_for_status ()
185
- return response .json ()
186
171
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