-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
executable file
·226 lines (179 loc) · 6.49 KB
/
Copy pathdemo.py
File metadata and controls
executable file
·226 lines (179 loc) · 6.49 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
"""
Demo script to test Qt API Client functionality.
This script demonstrates the core features without requiring the GUI.
"""
import sys
sys.path.insert(0, '.')
from src.core.request_manager import RequestManager
from src.core.collection_manager import CollectionManager
from src.core.environment_manager import EnvironmentManager
from src.core.history_manager import HistoryManager
from src.core.authentication import Authentication, AuthType
def demo_requests():
"""Demonstrate API request functionality."""
print("=" * 60)
print("DEMO: API Requests")
print("=" * 60)
rm = RequestManager()
# Simple GET request
print("\n1. Simple GET request to httpbin.org...")
response = rm.send_request('GET', 'https://httpbin.org/get', timeout=10)
if response.get('success'):
print(f" ✓ Status: {response.get('status_code')} {response.get('status_text')}")
print(f" ✓ Duration: {response.get('duration'):.2f}s")
else:
print(f" ✗ Error: {response.get('error')}")
# POST request with JSON body
print("\n2. POST request with JSON body...")
headers = {'Content-Type': 'application/json'}
body = '{"name": "Test User", "email": "test@example.com"}'
response = rm.send_request(
'POST',
'https://httpbin.org/post',
headers=headers,
body=body,
timeout=10
)
if response.get('success'):
print(f" ✓ Status: {response.get('status_code')}")
print(f" ✓ Response contains submitted data")
else:
print(f" ✗ Error: {response.get('error')}")
def demo_collections():
"""Demonstrate collection management."""
print("\n" + "=" * 60)
print("DEMO: Collections")
print("=" * 60)
cm = CollectionManager('/tmp/demo_collections')
# Create a collection
print("\n1. Creating a collection...")
collection = cm.create_collection('API Tests', 'Collection of test requests')
print(f" ✓ Created: {collection.name}")
# Add requests
print("\n2. Adding requests to collection...")
collection.add_request({
'method': 'GET',
'url': 'https://api.github.com/users/octocat',
'headers': {'Accept': 'application/json'},
'params': {},
'body': ''
})
collection.add_request({
'method': 'POST',
'url': 'https://httpbin.org/post',
'headers': {'Content-Type': 'application/json'},
'params': {},
'body': '{"test": "data"}'
})
print(f" ✓ Added {len(collection.requests)} requests")
# Save collection
print("\n3. Saving collection...")
cm.save_collections()
print(f" ✓ Saved to disk")
# Export collection
print("\n4. Exporting collection...")
cm.export_collection(0, '/tmp/exported_collection.json')
print(f" ✓ Exported to /tmp/exported_collection.json")
def demo_environments():
"""Demonstrate environment management."""
print("\n" + "=" * 60)
print("DEMO: Environments")
print("=" * 60)
em = EnvironmentManager('/tmp/demo_environments')
# Create environments
print("\n1. Creating environments...")
dev_env = em.create_environment('Development')
dev_env.set_variable('base_url', 'https://dev.api.example.com')
dev_env.set_variable('api_key', 'dev-key-12345')
prod_env = em.create_environment('Production')
prod_env.set_variable('base_url', 'https://api.example.com')
prod_env.set_variable('api_key', 'prod-key-67890')
print(f" ✓ Created 2 environments")
# Variable substitution
print("\n2. Testing variable substitution...")
em.set_active_environment('Development')
url = '{{base_url}}/users'
substituted = em.substitute_variables(url)
print(f" Original: {url}")
print(f" Substituted: {substituted}")
em.set_active_environment('Production')
substituted = em.substitute_variables(url)
print(f" With Production env: {substituted}")
def demo_authentication():
"""Demonstrate authentication methods."""
print("\n" + "=" * 60)
print("DEMO: Authentication")
print("=" * 60)
# Basic Auth
print("\n1. Basic Authentication...")
auth = Authentication()
auth.set_basic_auth('user123', 'password456')
auth_tuple = auth.get_auth_tuple()
print(f" ✓ Auth tuple: {auth_tuple}")
# Bearer Token
print("\n2. Bearer Token...")
auth = Authentication()
auth.set_bearer_token('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...')
headers = auth.get_auth_headers()
print(f" ✓ Headers: {headers}")
# API Key
print("\n3. API Key...")
auth = Authentication()
auth.set_api_key('my-secret-api-key', 'X-API-Key')
headers = auth.get_auth_headers()
print(f" ✓ Headers: {headers}")
def demo_history():
"""Demonstrate history management."""
print("\n" + "=" * 60)
print("DEMO: History")
print("=" * 60)
hm = HistoryManager('/tmp/demo_history', max_entries=50)
# Add entries
print("\n1. Adding history entries...")
for i in range(5):
request = {
'method': 'GET',
'url': f'https://api.example.com/endpoint{i}',
'headers': {},
'params': {},
'body': ''
}
response = {
'success': True,
'status_code': 200,
'duration': 0.5 + i * 0.1
}
hm.add_entry(request, response)
print(f" ✓ Added 5 entries")
# List entries
print("\n2. Recent history:")
for i, entry in enumerate(hm.get_entries()[:3], 1):
req = entry.request
resp = entry.response
print(f" {i}. {req['method']} {req['url']} - {resp.get('status_code', 'Failed')}")
def main():
"""Run all demos."""
print("\n")
print("╔" + "═" * 58 + "╗")
print("║" + " " * 15 + "Qt API Client - Demo" + " " * 23 + "║")
print("╚" + "═" * 58 + "╝")
try:
demo_requests()
demo_collections()
demo_environments()
demo_authentication()
demo_history()
print("\n" + "=" * 60)
print("All demos completed successfully!")
print("=" * 60)
print("\nTo run the GUI application, use: python main.py")
print()
except Exception as e:
print(f"\n✗ Error during demo: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == '__main__':
sys.exit(main())