pip install Flask |
# In your Flask app from flask import Flask, request, jsonify from flasgger import Swagger from flask_cors import CORS app = Flask(__name__) swagger = Swagger(app) CORS(app) # In-memory database devices = [ {'id': 1, 'type': 'switch', 'name': 'Switch 1', 'ip_address': '192.168.1.1'}, {'id': 2, 'type': 'router', 'name': 'Router 1', 'ip_address': '192.168.1.2'} ] links = [] # Helper function to find a
device by ID def find_device(device_id): return next((device for device in devices if device['id'] == device_id), None) # Route to get the complete
network @app.route('/network', methods=['GET']) def get_network(): return jsonify({'devices': devices, 'links': links}) # Route to get all devices @app.route('/devices', methods=['GET']) def get_devices(): return jsonify(devices) # Route to get a single
device by ID @app.route('/devices/ def get_device(device_id): device = find_device(device_id) if device is None: return jsonify({'error': 'Device not found'}), 404 return jsonify(device) # Route to add a new device @app.route('/devices', methods=['POST']) def add_device(): new_device = request.get_json() new_device['id'] = devices[-1]['id'] + 1 if devices else 1 devices.append(new_device) return jsonify(new_device), 201 # Route to update an
existing device @app.route('/devices/ def update_device(device_id): device = find_device(device_id) if device is None: return jsonify({'error': 'Device not found'}), 404 update_data = request.get_json() device.update(update_data) return jsonify(device) # Route to delete a device @app.route('/devices/ def delete_device(device_id): device = find_device(device_id) if device is None: return jsonify({'error': 'Device not found'}), 404 # Remove device devices.remove(device) # Remove all links associated with the device global links links = [link for link in links if link['source']
!=
device_id and link['target'] != device_id] return jsonify({'message': 'Device and associated
links deleted'}) # Route to get all links @app.route('/links', methods=['GET']) def get_links(): return jsonify(links) # Route to add a new link @app.route('/links', methods=['POST']) def add_link(): new_link = request.get_json() source_device = find_device(new_link['source']) target_device = find_device(new_link['target']) if source_device is None or target_device is None: return jsonify({'error': 'One or both devices not
found'}), 404 links.append(new_link) return jsonify(new_link), 201 # Route to delete a link @app.route('/links/ def delete_link(link_id): link = next((link for link in links if link['id'] == link_id), None) if link is None: return jsonify({'error': 'Link not found'}), 404 links.remove(link) return jsonify({'message': 'Link deleted'}) if __name__ == '__main__': app.run(debug=True) |
python app.py |
curl http://127.0.0.1:5000/network |
curl http://127.0.0.1:5000/devices |
curl http://127.0.0.1:5000/devices/1 |
curl -X POST -H "Content-Type:
application/json" -d '{"type":"router","name":"Router 2","ip_address":"192.168.1.3"}' http://127.0.0.1:5000/devices |
curl -X PUT -H "Content-Type: application/json" -d '{"name":"Updated Router 1","ip_address":"192.168.1.254"}' http://127.0.0.1:5000/devices/2 |
curl -X DELETE http://127.0.0.1:5000/devices/1 |
curl http://127.0.0.1:5000/links |
curl -X POST -H "Content-Type: application/json" -d '{"source":1,"target":2}' http://127.0.0.1:5000/links |
curl -X DELETE http://127.0.0.1:5000/links/1 |
pip install flasgger |
from flask import Flask, request, jsonify |
python app.py |