-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_api_server.py
More file actions
executable file
·43 lines (35 loc) · 1.45 KB
/
mock_api_server.py
File metadata and controls
executable file
·43 lines (35 loc) · 1.45 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
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import sys
class MockAPIHandler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
def do_GET(self):
self._set_headers()
if self.path == '/health':
response = {"status": "ok"}
elif self.path == '/users':
response = {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}
else:
response = {"message": f"API endpoint hit: {self.path}"}
self.wfile.write(json.dumps(response).encode('utf-8'))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
self._set_headers()
response = {"message": f"Received POST data on {self.path}", "data": post_data.decode('utf-8')}
self.wfile.write(json.dumps(response).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=MockAPIHandler, port=3000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting mock API server on port {port}...')
httpd.serve_forever()
if __name__ == '__main__':
# Get port from command line arguments if provided
port = 3000
if len(sys.argv) > 1:
port = int(sys.argv[1])
run(port=port)