Skip to content

Commit 0e50d90

Browse files
committed
✨ publish: async/await 2
1 parent b4a88e6 commit 0e50d90

File tree

4 files changed

+157
-88
lines changed

4 files changed

+157
-88
lines changed

dymoapi/branches/private.py

Lines changed: 96 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import requests
1+
import httpx
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")
88
async 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

2429
async 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

93104
async 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

158175
async 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

221244
async 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

232265
async 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

249291
async 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))

dymoapi/branches/public.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import re, requests
1+
import re, httpx
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.59"}
7+
headers = {"User-Agent": "DymoAPISDK/1.0.0", "X-Dymo-SDK-Env": "Python", "X-Dymo-SDK-Version" : "0.0.60"}
88

99
async def get_prayer_times(data):
1010
"""
@@ -26,10 +26,11 @@ async def get_prayer_times(data):
2626
"lon": data.get("lon")
2727
}
2828
try:
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))
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))
3334

3435
@deprecated("satinize")
3536
async def satinizer(data):
@@ -49,10 +50,15 @@ async def satinizer(data):
4950
try:
5051
input_value = data.get("input")
5152
if input_value is None: raise BadRequestError("You must specify at least the input.")
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))
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))
5662

5763
async def satinize(input_value):
5864
"""
@@ -70,10 +76,15 @@ async def satinize(input_value):
7076
"""
7177
try:
7278
if input_value is None: raise BadRequestError("You must specify at least the input.")
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))
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))
7788

7889
async def is_valid_pwd(data):
7990
"""
@@ -113,10 +124,11 @@ async def is_valid_pwd(data):
113124
if max_length is not None:
114125
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.")
115126
params["max"] = max_length
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))
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))
120132

121133
async def new_url_encrypt(url):
122134
"""
@@ -134,7 +146,8 @@ async def new_url_encrypt(url):
134146
"""
135147
try:
136148
if url is None or not (url.startswith("https://") or url.startswith("http://")): raise BadRequestError("You must provide a valid url.")
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))
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))

0 commit comments

Comments
 (0)