-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_transactions.py
More file actions
49 lines (40 loc) · 1.79 KB
/
Copy pathmock_transactions.py
File metadata and controls
49 lines (40 loc) · 1.79 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
import requests
from datetime import datetime
from typing import List, Dict
API_BASE_URL = os.environ.get("API_BASE_URL", "http://localhost:5000")
ENDPOINT = "/transactions/add"
URL = API_BASE_URL.rstrip("/") + ENDPOINT
# you can override USER_ID by setting env var or passing as first arg
USER_ID = os.environ.get("TEST_USER_ID") or (sys.argv[1] if len(sys.argv) > 1 else "68fd580c9683f20dd51a4974")
MERCHANT_DATA: List[Dict] = [
{"merchant_id": "68fd00639683f20dd51a4653", "amount": 200.00},
{"merchant_id": "68fd004c9683f20dd51a464e", "amount": 50.00},
{"merchant_id": "68fd00719683f20dd51a4657", "amount": 20.00},
{"merchant_id": "68fd007d9683f20dd51a4659", "amount": 30.00},
{"merchant_id": "68fd008c9683f20dd51a465a", "amount": 100.00},
]
def post_transactions(user_id: str, merchant_data: List[Dict]):
current_date = datetime.now().strftime("%Y-%m-%d")
for i, data in enumerate(merchant_data):
transaction_id = f"txn_{i+1:03}"
payload = {
"_id": transaction_id,
"user_id": user_id,
"merchant_id": data["merchant_id"],
"amount": data["amount"],
"date": current_date,
}
try:
resp = requests.post(URL, json=payload, timeout=10)
if resp.status_code == 200:
print(f"[OK] Posted {transaction_id} -> {data['merchant_id']} ${data['amount']}")
else:
print(f"[ERR] {transaction_id} returned {resp.status_code}: {resp.text}")
except requests.exceptions.RequestException as e:
print(f"[FAIL] {transaction_id} -> {e}")
if __name__ == "__main__":
print(f"Posting {len(MERCHANT_DATA)} mock transactions to {URL} as user {USER_ID}")
post_transactions(USER_ID, MERCHANT_DATA)
print("Done.")