-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_task.py
More file actions
35 lines (31 loc) · 1.01 KB
/
Copy pathbulk_task.py
File metadata and controls
35 lines (31 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import time
import requests
api_key = os.environ["WALOOKUP_API_KEY"]
base_url = os.getenv("API_BASE_URL", "https://walookup.com")
product = os.getenv("PRODUCT", "ws_active_batch")
country = os.getenv("COUNTRY", "US")
path = os.getenv("FILE", "numbers.txt")
with open(path, "rb") as handle:
created = requests.post(
f"{base_url}/api/v1/bulk-tasks",
headers={"X-API-Key": api_key},
data={"product": product, "country": country},
files={"file": handle},
timeout=120,
)
created.raise_for_status()
task_id = created.json()["data"]["id"]
print("submitted", task_id)
# 轮询间隔不得短于 30 秒,这是服务端的契约而不是建议。
while True:
time.sleep(30)
status = requests.get(
f"{base_url}/api/v1/bulk-tasks/{task_id}", headers={"X-API-Key": api_key}, timeout=60
)
status.raise_for_status()
payload = status.json()["data"]
print(payload["status"])
if payload["status"] != "processing":
print(payload)
break