Skip to content

Commit 52bbf8e

Browse files
committed
✨ publish: async/await 3
1 parent 0e50d90 commit 52bbf8e

File tree

4 files changed

+114
-183
lines changed

4 files changed

+114
-183
lines changed

dymoapi/branches/private.py

Lines changed: 58 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
import httpx
1+
import requests
22
from ..config import get_base_url
33
from ..utils.decorators import deprecated
44
from typing import Optional, Dict, Any, List
55
from ..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))

dymoapi/branches/public.py

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import re, httpx
1+
import re, requests
22
from urllib.parse import quote
33
from ..config import get_base_url
44
from ..utils.decorators import deprecated
55
from ..exceptions import APIError, BadRequestError
66

7-
headers = {"User-Agent": "DymoAPISDK/1.0.0", "X-Dymo-SDK-Env": "Python", "X-Dymo-SDK-Version" : "0.0.60"}
7+
headers = {"User-Agent": "DymoAPISDK/1.0.0", "X-Dymo-SDK-Env": "Python", "X-Dymo-SDK-Version" : "0.0.61"}
88

9-
async def get_prayer_times(data):
9+
def get_prayer_times(data):
1010
"""
1111
Gets the prayer times for a given latitude and longitude.
1212
@@ -26,14 +26,13 @@ async def get_prayer_times(data):
2626
"lon": data.get("lon")
2727
}
2828
try:
29-
async with httpx.AsyncClient() as client:
30-
resp = await client.get(f"{get_base_url()}/v1/public/islam/prayertimes", params=params, headers=headers)
31-
resp.raise_for_status()
32-
return resp.json()
33-
except httpx.RequestError as e: raise APIError(str(e))
29+
response = requests.get(f"{get_base_url()}/v1/public/islam/prayertimes", params=params)
30+
response.raise_for_status()
31+
return response.json()
32+
except requests.RequestException as e: raise APIError(str(e))
3433

3534
@deprecated("satinize")
36-
async def satinizer(data):
35+
def satinizer(data):
3736
"""
3837
Sanitizes the given input according to the Dymo API standard.
3938
@@ -50,17 +49,12 @@ async def satinizer(data):
5049
try:
5150
input_value = data.get("input")
5251
if input_value is None: raise BadRequestError("You must specify at least the input.")
53-
async with httpx.AsyncClient() as client:
54-
resp = await client.get(
55-
f"{get_base_url()}/v1/public/inputSatinizer",
56-
params={"input": quote(input_value)},
57-
headers=headers
58-
)
59-
resp.raise_for_status()
60-
return resp.json()
61-
except httpx.RequestError as e: raise APIError(str(e))
62-
63-
async def satinize(input_value):
52+
response = requests.get(f"{get_base_url()}/v1/public/inputSatinizer", params={"input":quote(input_value)}, headers=headers)
53+
response.raise_for_status()
54+
return response.json()
55+
except requests.RequestException as e: raise APIError(str(e))
56+
57+
def satinize(input_value):
6458
"""
6559
Sanitizes the given input according to the Dymo API standard.
6660
@@ -76,17 +70,12 @@ async def satinize(input_value):
7670
"""
7771
try:
7872
if input_value is None: raise BadRequestError("You must specify at least the input.")
79-
async with httpx.AsyncClient() as client:
80-
resp = await client.get(
81-
f"{get_base_url()}/v1/public/inputSatinizer",
82-
params={"input": quote(input_value)},
83-
headers=headers
84-
)
85-
resp.raise_for_status()
86-
return resp.json()
87-
except httpx.RequestError as e: raise APIError(str(e))
88-
89-
async def is_valid_pwd(data):
73+
response = requests.get(f"{get_base_url()}/v1/public/inputSatinizer", params={"input":quote(input_value)}, headers=headers)
74+
response.raise_for_status()
75+
return response.json()
76+
except requests.RequestException as e: raise APIError(str(e))
77+
78+
def is_valid_pwd(data):
9079
"""
9180
Validates the given password against the configured deny rules.
9281
@@ -124,13 +113,12 @@ async def is_valid_pwd(data):
124113
if max_length is not None:
125114
if not isinstance(max_length, int) or max_length < 32 or max_length > 100: raise BadRequestError("If you provide a maximum it must be valid.")
126115
params["max"] = max_length
127-
async with httpx.AsyncClient() as client:
128-
resp = await client.get(f"{get_base_url()}/v1/public/validPwd", params=params, headers=headers)
129-
resp.raise_for_status()
130-
return resp.json()
131-
except httpx.RequestError as e: raise APIError(str(e))
116+
response = requests.get(f"{get_base_url()}/v1/public/validPwd", params=params, headers=headers)
117+
response.raise_for_status()
118+
return response.json()
119+
except requests.RequestException as e: raise APIError(str(e))
132120

133-
async def new_url_encrypt(url):
121+
def new_url_encrypt(url):
134122
"""
135123
Encrypts the given URL.
136124
@@ -146,8 +134,7 @@ async def new_url_encrypt(url):
146134
"""
147135
try:
148136
if url is None or not (url.startswith("https://") or url.startswith("http://")): raise BadRequestError("You must provide a valid url.")
149-
async with httpx.AsyncClient() as client:
150-
resp = await client.get(f"{get_base_url()}/v1/public/url-encrypt", params={"url": url}, headers=headers)
151-
resp.raise_for_status()
152-
return resp.json()
153-
except httpx.RequestError as e: raise APIError(str(e))
137+
response = requests.get(f"{get_base_url()}/v1/public/url-encrypt", params={"url": url}, headers=headers)
138+
response.raise_for_status()
139+
return response.json()
140+
except requests.RequestException as e: raise APIError(str(e))

0 commit comments

Comments
 (0)