1- import httpx
1+ import requests
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" )
8- async def is_valid_data (token , data ):
9- return await is_valid_data_raw (token , data )
8+ 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.61" , "Authorization" : token })
12+ response .raise_for_status ()
13+ return response .json ()
14+ except requests .RequestException as e : raise APIError (str (e ))
1015
11- async def is_valid_data_raw (token : str , data : dict ) -> dict :
16+ def is_valid_data_raw (token , data ) :
1217 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." )
1318 try :
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 ))
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.61" , "Authorization" : token })
20+ response .raise_for_status ()
21+ return response .json ()
22+ except requests .RequestException as e : raise APIError (str (e ))
2823
29- async def is_valid_email (token : Optional [str ], email : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
24+ def is_valid_email (token : Optional [str ], email : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
3025 """
3126 Validates the given email against the configured deny rules.
3227
@@ -56,17 +51,11 @@ async def is_valid_email(token: Optional[str], email: str, rules: Optional[Dict[
5651 plugins = [p for p in plugins if p is not None ]
5752
5853 try :
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- )
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.61" , "Authorization" : token }
58+ )
7059 resp .raise_for_status ()
7160 data = resp .json ().get ("email" , {})
7261
@@ -99,9 +88,9 @@ async def is_valid_email(token: Optional[str], email: str, rules: Optional[Dict[
9988 "response" : data
10089 }
10190
102- except httpx . RequestError as e : raise APIError (f"[Dymo API] { str (e )} " )
91+ except requests . RequestException as e : raise APIError (f"[Dymo API] { str (e )} " )
10392
104- async def is_valid_ip (token : Optional [str ], ip : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
93+ def is_valid_ip (token : Optional [str ], ip : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
10594 """
10695 Validates the given IP against the configured deny rules.
10796
@@ -129,17 +118,11 @@ async def is_valid_ip(token: Optional[str], ip: str, rules: Optional[Dict[str, L
129118 plugins = [p for p in plugins if p is not None ]
130119
131120 try :
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- )
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.61" , "Authorization" : token }
125+ )
143126 resp .raise_for_status ()
144127 data = resp .json ().get ("ip" , {})
145128
@@ -170,9 +153,9 @@ async def is_valid_ip(token: Optional[str], ip: str, rules: Optional[Dict[str, L
170153 "response" : data
171154 }
172155
173- except httpx . RequestError as e : raise APIError (f"[Dymo API] { str (e )} " )
156+ except requests . RequestException as e : raise APIError (f"[Dymo API] { str (e )} " )
174157
175- async def is_valid_phone (token : Optional [str ], phone : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
158+ def is_valid_phone (token : Optional [str ], phone : str , rules : Optional [Dict [str , List [str ]]] = None ) -> Dict [str , Any ]:
176159 """
177160 Validates the given phone against the configured deny rules.
178161
@@ -199,17 +182,11 @@ async def is_valid_phone(token: Optional[str], phone: str, rules: Optional[Dict[
199182 plugins = [p for p in plugins if p is not None ]
200183
201184 try :
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- )
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.61" , "Authorization" : token }
189+ )
213190 resp .raise_for_status ()
214191 data = resp .json ().get ("phone" , {})
215192
@@ -239,30 +216,20 @@ async def is_valid_phone(token: Optional[str], phone: str, rules: Optional[Dict[
239216 "response" : data
240217 }
241218
242- except httpx . RequestError as e : raise APIError (f"[Dymo API] { str (e )} " )
219+ except requests . RequestException as e : raise APIError (f"[Dymo API] { str (e )} " )
243220
244- async def send_email (token , data ):
221+ def send_email (token , data ):
245222 if not data .get ("from" ): raise BadRequestError ("You must provide an email address from which the following will be sent." )
246223 if not data .get ("to" ): raise BadRequestError ("You must provide an email to be sent to." )
247224 if not data .get ("subject" ): raise BadRequestError ("You must provide a subject for the email to be sent." )
248225 if not data .get ("html" ): raise BadRequestError ("You must provide HTML." )
249226 try :
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 ))
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.61" , "Authorization" : token })
228+ response .raise_for_status ()
229+ return response .json ()
230+ except requests .RequestException as e : raise APIError (str (e ))
264231
265- async def get_random (token , data ):
232+ def get_random (token , data ):
266233 if not data .get ("from" ): raise BadRequestError ("You must provide an email address from which the following will be sent." )
267234 if not data .get ("to" ): raise BadRequestError ("You must provide an email to be sent to." )
268235 if not data .get ("subject" ): raise BadRequestError ("You must provide a subject for the email to be sent." )
@@ -273,38 +240,26 @@ async def get_random(token, data):
273240 if data .min < - 1000000000 or data .min > 1000000000 : raise BadRequestError ("'min' must be an integer in the interval [-1000000000}, 1000000000]." )
274241 if data .max < - 1000000000 or data .max > 1000000000 : raise BadRequestError ("'max' must be an integer in the interval [-1000000000}, 1000000000]." )
275242 try :
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 ))
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.61" , "Authorization" : token })
244+ response .raise_for_status ()
245+ return response .json ()
246+ except requests .RequestException as e : raise APIError (str (e ))
247+
290248
291- async def extract_with_textly (token : str , data : dict ) -> dict :
249+ def extract_with_textly (token : str , data : dict ) -> dict :
292250 if not data .get ("data" ): raise BadRequestError ("No data provided." )
293251 if not data .get ("format" ): raise BadRequestError ("No format provided." )
294252
295253 try :
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 ))
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 ))
0 commit comments