-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo_api.py
More file actions
207 lines (182 loc) Β· 7.53 KB
/
demo_api.py
File metadata and controls
207 lines (182 loc) Β· 7.53 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
"""
π Task Manager PRO - API Demo Script
Demonstrates all major API features with live testing
"""
import requests
import json
from datetime import datetime, timedelta
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
import time
console = Console()
BASE_URL = "http://localhost:8000"
def print_response(title, response):
"""Pretty print API response"""
console.print(f"\n[bold cyan]{title}[/bold cyan]")
console.print(f"Status: [green]{response.status_code}[/green]")
try:
console.print(Panel(json.dumps(response.json(), indent=2), title="Response"))
except:
console.print(response.text)
def main():
console.print(Panel.fit(
"π― Task Manager PRO - API Demo\n"
"Testing all major features...",
border_style="bold green"
))
# 1. Health Check
console.print("\n[bold yellow]βββ 1. Health Check βββ[/bold yellow]")
response = requests.get(f"{BASE_URL}/health")
print_response("Health Check", response)
time.sleep(1)
# 2. Register User
console.print("\n[bold yellow]βββ 2. Register New User βββ[/bold yellow]")
user_data = {
"username": "demo_user",
"email": "demo@taskmanager.com",
"password": "SecurePass123!"
}
response = requests.post(f"{BASE_URL}/api/auth/register", json=user_data)
print_response("User Registration", response)
time.sleep(1)
# 3. Login
console.print("\n[bold yellow]βββ 3. Login & Get Token βββ[/bold yellow]")
login_data = {
"username": "demo_user",
"password": "SecurePass123!"
}
response = requests.post(f"{BASE_URL}/api/auth/login", json=login_data)
print_response("User Login", response)
if response.status_code != 200:
console.print("[red]Login failed! Stopping demo.[/red]")
return
token = response.json()["access_token"]
headers = {"Authorization": f"Bearer {token}"}
console.print(f"\n[green]β Token acquired: {token[:30]}...[/green]")
time.sleep(1)
# 4. Create Multiple Tasks
console.print("\n[bold yellow]βββ 4. Create Tasks βββ[/bold yellow]")
tasks = [
{
"title": "Complete API Documentation",
"description": "Write comprehensive API docs with examples",
"due_date": (datetime.now() + timedelta(days=7)).strftime("%Y-%m-%d"),
"priority": "high"
},
{
"title": "Code Review",
"description": "Review pull requests from team",
"due_date": (datetime.now() + timedelta(days=2)).strftime("%Y-%m-%d"),
"priority": "medium"
},
{
"title": "Deploy to Production",
"description": "Deploy Task Manager PRO v0.3.0",
"due_date": (datetime.now() + timedelta(days=14)).strftime("%Y-%m-%d"),
"priority": "high"
}
]
task_ids = []
for task in tasks:
response = requests.post(f"{BASE_URL}/api/tasks", json=task, headers=headers)
if response.status_code == 201:
task_id = response.json()["id"]
task_ids.append(task_id)
console.print(f"[green]β Created task: {task['title']} (ID: {task_id})[/green]")
else:
console.print(f"[red]β Failed to create task: {task['title']}[/red]")
time.sleep(0.5)
# 5. List All Tasks
console.print("\n[bold yellow]βββ 5. List All Tasks βββ[/bold yellow]")
response = requests.get(f"{BASE_URL}/api/tasks", headers=headers)
if response.status_code == 200:
tasks_list = response.json()
table = Table(title="π Your Tasks")
table.add_column("ID", style="cyan")
table.add_column("Title", style="green")
table.add_column("Priority", style="yellow")
table.add_column("Due Date", style="magenta")
table.add_column("Status", style="blue")
for task in tasks_list:
status = "β
Done" if task["completed"] else "β³ Pending"
table.add_row(
str(task["id"]),
task["title"],
task.get("priority", "N/A"),
task["due_date"],
status
)
console.print(table)
time.sleep(1)
# 6. Get Single Task
if task_ids:
console.print("\n[bold yellow]βββ 6. Get Task Details βββ[/bold yellow]")
response = requests.get(f"{BASE_URL}/api/tasks/{task_ids[0]}", headers=headers)
print_response(f"Task Details (ID: {task_ids[0]})", response)
time.sleep(1)
# 7. Update Task
if task_ids:
console.print("\n[bold yellow]βββ 7. Update Task βββ[/bold yellow]")
update_data = {
"title": "Complete API Documentation (UPDATED)",
"completed": True
}
response = requests.put(
f"{BASE_URL}/api/tasks/{task_ids[0]}",
json=update_data,
headers=headers
)
print_response("Task Update", response)
time.sleep(1)
# 8. Get Current User
console.print("\n[bold yellow]βββ 8. Get Current User Info βββ[/bold yellow]")
response = requests.get(f"{BASE_URL}/api/users/me", headers=headers)
print_response("Current User", response)
time.sleep(1)
# 9. Toggle Email Reminders
console.print("\n[bold yellow]βββ 9. Toggle Email Reminders βββ[/bold yellow]")
response = requests.post(f"{BASE_URL}/api/users/me/toggle-reminders", headers=headers)
print_response("Toggle Email Reminders", response)
time.sleep(1)
# 10. Pagination Test
console.print("\n[bold yellow]βββ 10. Test Pagination βββ[/bold yellow]")
response = requests.get(f"{BASE_URL}/api/tasks?skip=0&limit=2", headers=headers)
print_response("Paginated Tasks (limit=2)", response)
time.sleep(1)
# 11. Delete Task
if len(task_ids) > 1:
console.print("\n[bold yellow]βββ 11. Delete Task βββ[/bold yellow]")
delete_id = task_ids[-1]
response = requests.delete(f"{BASE_URL}/api/tasks/{delete_id}", headers=headers)
print_response(f"Delete Task (ID: {delete_id})", response)
time.sleep(1)
# 12. Try Unauthorized Access
console.print("\n[bold yellow]βββ 12. Test Unauthorized Access βββ[/bold yellow]")
response = requests.get(f"{BASE_URL}/api/tasks") # No token
print_response("Unauthorized Access (no token)", response)
time.sleep(1)
# Summary
console.print("\n")
console.print(Panel.fit(
"β
Demo Complete!\n\n"
"π Next Steps:\n"
" β’ Visit http://localhost:8000/api/docs for interactive docs\n"
" β’ Visit http://localhost:8000/api/redoc for alternative docs\n"
" β’ Check out the test suite: pytest tests/ -v\n"
" β’ Review README.md for more examples",
border_style="bold green",
title="π Success"
))
if __name__ == "__main__":
try:
main()
except requests.exceptions.ConnectionError:
console.print("\n[bold red]β Error: Could not connect to API server![/bold red]")
console.print("\n[yellow]Make sure the server is running:[/yellow]")
console.print(" uvicorn task_manager_pro.api.main:app --reload\n")
except KeyboardInterrupt:
console.print("\n\n[yellow]Demo interrupted by user[/yellow]")
except Exception as e:
console.print(f"\n[bold red]Error: {e}[/bold red]")