-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient.py
43 lines (30 loc) · 1.29 KB
/
client.py
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
36
37
38
39
40
41
42
43
import argparse
import logging
import requests
from pydantic import BaseModel
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] [%(process)d] [%(levelname)s] - %(message)s')
class User(BaseModel):
id: str
email: str
RESTATE_URL = "http://localhost:8080"
headers = {"Content-Type": "application/json"}
# The upload client calls the data upload workflow and awaits the result for 5 seconds.
# If the workflow doesn't complete within that time, it asks the
# workflow to send the upload url via email instead.
def upload_data(user: User):
logging.info(f"Start upload for {user.id}")
try:
url = f"{RESTATE_URL}/DataUploadService/{user.id}/run"
upload_url = requests.post(url, timeout=5).json()
except requests.exceptions.Timeout:
logging.info("Slow upload... Mail the link later")
email_url = f"{RESTATE_URL}/DataUploadService/{user.id}/resultAsEmail/send"
requests.post(email_url, json=user.email, headers=headers)
return
# ... process result directly ...
logging.info(f"Fast upload: URL was {upload_url}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("id", type=str, help="User ID")
args = parser.parse_args()
upload_data(User(id=args.id, email=f"{args.id}@example.com"))