-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
143 lines (113 loc) · 5.25 KB
/
demo.py
File metadata and controls
143 lines (113 loc) · 5.25 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
"""
Demo script to showcase the DealNest Partner Request API functionality.
This script demonstrates the complete workflow of creating users, sending requests, and responding to them.
"""
import requests
import json
import time
BASE_URL = "http://localhost:8004"
def make_request(method, endpoint, data=None):
"""Helper function to make HTTP requests"""
url = f"{BASE_URL}{endpoint}"
try:
if method.upper() == "GET":
response = requests.get(url)
elif method.upper() == "POST":
response = requests.post(url, json=data)
else:
raise ValueError(f"Unsupported method: {method}")
print(f"{method.upper()} {endpoint}")
print(f"Status: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
print("-" * 50)
return response.json()
except requests.exceptions.ConnectionError:
print(f"❌ Could not connect to {BASE_URL}")
print("Make sure the API server is running with: poetry run python -m app.main")
return None
except Exception as e:
print(f"❌ Error: {e}")
return None
def demo_partner_request_workflow():
"""Demonstrate the complete partner request workflow"""
print("🚀 DealNest Partner Request API Demo")
print("=" * 50)
# Step 1: Create users
print("📝 Step 1: Creating users...")
alice_data = {"email": "[email protected]", "name": "Alice Johnson"}
bob_data = {"email": "[email protected]", "name": "Bob Smith"}
charlie_data = {"email": "[email protected]", "name": "Charlie Brown"}
alice = make_request("POST", "/users/", alice_data)
if not alice:
return
bob = make_request("POST", "/users/", bob_data)
if not bob:
return
charlie = make_request("POST", "/users/", charlie_data)
if not charlie:
return
# Step 2: Send partner requests
print("📤 Step 2: Sending partner requests...")
# Alice sends request to Bob
request1_data = {"sender_id": alice["id"], "recipient_id": bob["id"]}
request1 = make_request("POST", "/partner-requests/", request1_data)
# Charlie sends request to Alice
request2_data = {"sender_id": charlie["id"], "recipient_id": alice["id"]}
request2 = make_request("POST", "/partner-requests/", request2_data)
# Step 3: Check received requests
print("📥 Step 3: Checking received requests...")
# Check Bob's received requests
bob_requests = make_request("GET", f"/partner-requests/received/{bob['id']}/")
# Check Alice's received requests
alice_requests = make_request("GET", f"/partner-requests/received/{alice['id']}/")
# Step 4: Respond to requests
print("✅ Step 4: Responding to requests...")
# Bob accepts Alice's request
if bob_requests and bob_requests["count"] > 0:
request_id = bob_requests["pending_requests"][0]["id"]
response_data = {"request_id": request_id, "action": "accept"}
make_request("POST", "/partner-requests/respond/", response_data)
# Alice rejects Charlie's request
if alice_requests and alice_requests["count"] > 0:
request_id = alice_requests["pending_requests"][0]["id"]
response_data = {"request_id": request_id, "action": "reject"}
make_request("POST", "/partner-requests/respond/", response_data)
# Step 5: Check final state
print("🔍 Step 5: Checking final state...")
# Check Bob's requests again (should be empty now)
bob_requests_final = make_request("GET", f"/partner-requests/received/{bob['id']}/")
# Check Alice's requests again (should be empty now)
alice_requests_final = make_request("GET", f"/partner-requests/received/{alice['id']}/")
print("🎉 Demo completed!")
print("\nSummary:")
print("- Alice and Bob are now partners (request accepted)")
print("- Charlie's request to Alice was rejected")
print("- Check the server logs for email notifications")
def demo_error_cases():
"""Demonstrate error handling"""
print("\n🚨 Error Cases Demo")
print("=" * 50)
# Try to create duplicate user
print("❌ Creating duplicate user...")
duplicate_data = {"email": "[email protected]", "name": "Alice Duplicate"}
make_request("POST", "/users/", duplicate_data)
# Try to send request to non-existent user
print("❌ Sending request to non-existent user...")
invalid_request = {"sender_id": 1, "recipient_id": 999}
make_request("POST", "/partner-requests/", invalid_request)
# Try to send request to self
print("❌ Sending request to self...")
self_request = {"sender_id": 1, "recipient_id": 1}
make_request("POST", "/partner-requests/", self_request)
if __name__ == "__main__":
print("Make sure the API server is running on http://localhost:8004")
print("Start it with: poetry run python -m app.main")
print()
input("Press Enter to start the demo...")
demo_partner_request_workflow()
demo_error_cases()
print("\n📚 For more information, visit:")
print("- API Documentation: http://localhost:8004/docs")
print("- Alternative Docs: http://localhost:8004/redoc")
print("- Health Check: http://localhost:8004/health")