1- import requests
1+ import httpx
22from ..config import get_base_url
33from ..utils .decorators import deprecated
44from typing import Optional , Dict , Any , List
55from ..exceptions import APIError , BadRequestError
66
77@deprecated ("is_valid_data_raw" )
88async def is_valid_data (token , data ):
9- if not any ([key in list (data .keys ()) for key in ["url" , "email" , "phone" , "domain" , "creditCard" , "ip" , "wallet" , "userAgent" , "iban" ]]): raise BadRequestError ("You must provide at least one parameter." )
10- try :
11- response = requests .post (f"{ get_base_url ()} /v1/private/secure/verify" , json = data , headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token })
12- response .raise_for_status ()
13- return response .json ()
14- except requests .RequestException as e : raise APIError (str (e ))
9+ return await is_valid_data_raw (token , data )
1510
16- async def is_valid_data_raw (token , data ) :
11+ async def is_valid_data_raw (token : str , data : dict ) -> dict :
1712 if not any ([key in list (data .keys ()) for key in ["url" , "email" , "phone" , "domain" , "creditCard" , "ip" , "wallet" , "userAgent" , "iban" ]]): raise BadRequestError ("You must provide at least one parameter." )
1813 try :
19- response = requests .post (f"{ get_base_url ()} /v1/private/secure/verify" , json = data , headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token })
20- response .raise_for_status ()
21- return response .json ()
22- except requests .RequestException as e : raise APIError (str (e ))
14+ async with httpx .AsyncClient () as client :
15+ resp = await client .post (
16+ f"{ get_base_url ()} /v1/private/secure/verify" ,
17+ json = data ,
18+ headers = {
19+ "User-Agent" : "DymoAPISDK/1.0.0" ,
20+ "X-Dymo-SDK-Env" : "Python" ,
21+ "X-Dymo-SDK-Version" : "0.0.60" ,
22+ "Authorization" : token
23+ }
24+ )
25+ resp .raise_for_status ()
26+ return resp .json ()
27+ except httpx .RequestError as e : raise APIError (str (e ))
2328
2429async def is_valid_email (token : Optional [str ], email : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
2530 """
@@ -51,11 +56,17 @@ async def is_valid_email(token: Optional[str], email: str, rules: Optional[Dict[
5156 plugins = [p for p in plugins if p is not None ]
5257
5358 try :
54- resp = requests .post (
55- f"{ get_base_url ()} /v1/private/secure/verify" ,
56- json = {"email" : email , "plugins" : plugins },
57- headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token }
58- )
59+ async with httpx .AsyncClient () as client :
60+ resp = await client .post (
61+ f"{ get_base_url ()} /v1/private/secure/verify" ,
62+ json = {"email" : email , "plugins" : plugins },
63+ headers = {
64+ "User-Agent" : "DymoAPISDK/1.0.0" ,
65+ "X-Dymo-SDK-Env" : "Python" ,
66+ "X-Dymo-SDK-Version" : "0.0.60" ,
67+ "Authorization" : token
68+ }
69+ )
5970 resp .raise_for_status ()
6071 data = resp .json ().get ("email" , {})
6172
@@ -88,7 +99,7 @@ async def is_valid_email(token: Optional[str], email: str, rules: Optional[Dict[
8899 "response" : data
89100 }
90101
91- except requests . RequestException as e : raise APIError (f"[Dymo API] { str (e )} " )
102+ except httpx . RequestError as e : raise APIError (f"[Dymo API] { str (e )} " )
92103
93104async def is_valid_ip (token : Optional [str ], ip : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
94105 """
@@ -118,11 +129,17 @@ async def is_valid_ip(token: Optional[str], ip: str, rules: Optional[Dict[str, L
118129 plugins = [p for p in plugins if p is not None ]
119130
120131 try :
121- resp = requests .post (
122- f"{ get_base_url ()} /v1/private/secure/verify" ,
123- json = {"email" : ip , "plugins" : plugins },
124- headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token }
125- )
132+ async with httpx .AsyncClient () as client :
133+ resp = await client .post (
134+ f"{ get_base_url ()} /v1/private/secure/verify" ,
135+ json = {"email" : ip , "plugins" : plugins },
136+ headers = {
137+ "User-Agent" : "DymoAPISDK/1.0.0" ,
138+ "X-Dymo-SDK-Env" : "Python" ,
139+ "X-Dymo-SDK-Version" : "0.0.60" ,
140+ "Authorization" : token
141+ }
142+ )
126143 resp .raise_for_status ()
127144 data = resp .json ().get ("ip" , {})
128145
@@ -153,7 +170,7 @@ async def is_valid_ip(token: Optional[str], ip: str, rules: Optional[Dict[str, L
153170 "response" : data
154171 }
155172
156- except requests . RequestException as e : raise APIError (f"[Dymo API] { str (e )} " )
173+ except httpx . RequestError as e : raise APIError (f"[Dymo API] { str (e )} " )
157174
158175async def is_valid_phone (token : Optional [str ], phone : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
159176 """
@@ -182,11 +199,17 @@ async def is_valid_phone(token: Optional[str], phone: str, rules: Optional[Dict[
182199 plugins = [p for p in plugins if p is not None ]
183200
184201 try :
185- resp = requests .post (
186- f"{ get_base_url ()} /v1/private/secure/verify" ,
187- json = {"phone" : phone , "plugins" : plugins },
188- headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token }
189- )
202+ async with httpx .AsyncClient () as client :
203+ resp = await client .post (
204+ f"{ get_base_url ()} /v1/private/secure/verify" ,
205+ json = {"phone" : phone , "plugins" : plugins },
206+ headers = {
207+ "User-Agent" : "DymoAPISDK/1.0.0" ,
208+ "X-Dymo-SDK-Env" : "Python" ,
209+ "X-Dymo-SDK-Version" : "0.0.60" ,
210+ "Authorization" : token
211+ }
212+ )
190213 resp .raise_for_status ()
191214 data = resp .json ().get ("phone" , {})
192215
@@ -216,18 +239,28 @@ async def is_valid_phone(token: Optional[str], phone: str, rules: Optional[Dict[
216239 "response" : data
217240 }
218241
219- except requests . RequestException as e : raise APIError (f"[Dymo API] { str (e )} " )
242+ except httpx . RequestError as e : raise APIError (f"[Dymo API] { str (e )} " )
220243
221244async def send_email (token , data ):
222245 if not data .get ("from" ): raise BadRequestError ("You must provide an email address from which the following will be sent." )
223246 if not data .get ("to" ): raise BadRequestError ("You must provide an email to be sent to." )
224247 if not data .get ("subject" ): raise BadRequestError ("You must provide a subject for the email to be sent." )
225248 if not data .get ("html" ): raise BadRequestError ("You must provide HTML." )
226249 try :
227- response = requests .post (f"{ get_base_url ()} /v1/private/sender/sendEmail" , json = data , headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token })
228- response .raise_for_status ()
229- return response .json ()
230- except requests .RequestException as e : raise APIError (str (e ))
250+ async with httpx .AsyncClient () as client :
251+ resp = await client .post (
252+ f"{ get_base_url ()} /v1/private/sender/sendEmail" ,
253+ json = data ,
254+ headers = {
255+ "User-Agent" : "DymoAPISDK/1.0.0" ,
256+ "X-Dymo-SDK-Env" : "Python" ,
257+ "X-Dymo-SDK-Version" : "0.0.60" ,
258+ "Authorization" : token
259+ }
260+ )
261+ resp .raise_for_status ()
262+ return resp .json ()
263+ except httpx .RequestError as e : raise APIError (str (e ))
231264
232265async def get_random (token , data ):
233266 if not data .get ("from" ): raise BadRequestError ("You must provide an email address from which the following will be sent." )
@@ -240,26 +273,38 @@ async def get_random(token, data):
240273 if data .min < - 1000000000 or data .min > 1000000000 : raise BadRequestError ("'min' must be an integer in the interval [-1000000000}, 1000000000]." )
241274 if data .max < - 1000000000 or data .max > 1000000000 : raise BadRequestError ("'max' must be an integer in the interval [-1000000000}, 1000000000]." )
242275 try :
243- response = requests .post (f"{ get_base_url ()} /v1/private/srng" , json = data , headers = {"User-Agent" : "DymoAPISDK/1.0.0" , "X-Dymo-SDK-Env" : "Python" , "X-Dymo-SDK-Version" : "0.0.59" , "Authorization" : token })
244- response .raise_for_status ()
245- return response .json ()
246- except requests .RequestException as e : raise APIError (str (e ))
247-
276+ async with httpx .AsyncClient () as client :
277+ resp = await client .post (
278+ f"{ get_base_url ()} /v1/private/srng" ,
279+ json = data ,
280+ headers = {
281+ "User-Agent" : "DymoAPISDK/1.0.0" ,
282+ "X-Dymo-SDK-Env" : "Python" ,
283+ "X-Dymo-SDK-Version" : "0.0.60" ,
284+ "Authorization" : token
285+ }
286+ )
287+ resp .raise_for_status ()
288+ return resp .json ()
289+ except httpx .RequestError as e : raise APIError (str (e ))
248290
249291async def extract_with_textly (token : str , data : dict ) -> dict :
250292 if not data .get ("data" ): raise BadRequestError ("No data provided." )
251293 if not data .get ("format" ): raise BadRequestError ("No format provided." )
252294
253295 try :
254- response = requests .post (
255- f"{ get_base_url ()} /private/textly/extract" ,
256- json = data ,
257- headers = {
258- "Content-Type" : "application/json" ,
259- "User-Agent" : "DymoAPISDK/1.0.0" ,
260- "Authorization" : token
261- }
262- )
263- response .raise_for_status ()
264- return response .json ()
265- except requests .RequestException as e : raise APIError (str (e ))
296+ async with httpx .AsyncClient () as client :
297+ resp = await client .post (
298+ f"{ get_base_url ()} /private/textly/extract" ,
299+ json = data ,
300+ headers = {
301+ "Content-Type" : "application/json" ,
302+ "User-Agent" : "DymoAPISDK/1.0.0" ,
303+ "X-Dymo-SDK-Env" : "Python" ,
304+ "X-Dymo-SDK-Version" : "0.0.60" ,
305+ "Authorization" : token
306+ }
307+ )
308+ resp .raise_for_status ()
309+ return resp .json ()
310+ except httpx .RequestError as e : raise APIError (str (e ))
0 commit comments