diff --git a/iot-integration/api/iot_api.py b/iot-integration/api/iot_api.py index 5e6d3573d..4d047284c 100644 --- a/iot-integration/api/iot_api.py +++ b/iot-integration/api/iot_api.py @@ -1,43 +1,46 @@ -from flask import Flask, request, jsonify +from data_encryption import encrypt_data from device_auth import DeviceAuth -from iot_devices import SmartHomeDevice, WearableDevice, AutonomousVehicleDevice +from flask import Flask, jsonify, request +from iot_devices import AutonomousVehicleDevice, SmartHomeDevice, WearableDevice from microtransactions import MicrotransactionHandler -from data_encryption import encrypt_data app = Flask(__name__) # Initialize the device authentication instance -device_auth = DeviceAuth('device_id', 'device_secret') +device_auth = DeviceAuth("device_id", "device_secret") # Initialize the microtransaction handler instance -microtransaction_handler = MicrotransactionHandler('microtransactions.db') +microtransaction_handler = MicrotransactionHandler("microtransactions.db") -@app.route('/iot/devices', methods=['GET']) + +@app.route("/iot/devices", methods=["GET"]) def get_iot_devices(): # Return a list of supported IoT devices - return jsonify(['Smart Home', 'Wearable', 'Autonomous Vehicle']) + return jsonify(["Smart Home", "Wearable", "Autonomous Vehicle"]) + -@app.route('/iot/devices/', methods=['POST']) +@app.route("/iot/devices/", methods=["POST"]) def register_iot_device(device_type): # Register a new IoT device - device_id = request.json['device_id'] - device_token = request.json['device_token'] - if device_type == 'Smart Home': + device_id = request.json["device_id"] + device_token = request.json["device_token"] + if device_type == "Smart Home": device = SmartHomeDevice(device_id, device_token) - elif device_type == 'Wearable': + elif device_type == "Wearable": device = WearableDevice(device_id, device_token) - elif device_type == 'Autonomous Vehicle': + elif device_type == "Autonomous Vehicle": device = AutonomousVehicleDevice(device_id, device_token) else: - return jsonify({'error': 'Invalid device type'}), 400 - return jsonify({'device_id': device_id, 'device_token': device_token}) + return jsonify({"error": "Invalid device type"}), 400 + return jsonify({"device_id": device_id, "device_token": device_token}) -@app.route('/iot/transactions', methods=['POST']) + +@app.route("/iot/transactions", methods=["POST"]) def make_iot_transaction(): # Make a transaction using an IoT device - device_id = request.json['device_id'] - transaction_id = request.json['transaction_id'] - amount = request.json['amount'] + device_id = request.json["device_id"] + transaction_id = request.json["transaction_id"] + amount = request.json["amount"] device = get_iot_device(device_id) if device: # Authenticate the device @@ -46,24 +49,26 @@ def make_iot_transaction(): if device.make_payment(amount): # Create a microtransaction microtransaction_handler.create_microtransaction(transaction_id, amount) - return jsonify({'transaction_id': transaction_id, 'amount': amount}) + return jsonify({"transaction_id": transaction_id, "amount": amount}) else: - return jsonify({'error': 'Transaction failed'}), 400 + return jsonify({"error": "Transaction failed"}), 400 else: - return jsonify({'error': 'Device authentication failed'}), 401 + return jsonify({"error": "Device authentication failed"}), 401 else: - return jsonify({'error': 'Invalid device ID'}), 404 + return jsonify({"error": "Invalid device ID"}), 404 + def get_iot_device(device_id): # Retrieve an IoT device instance - if device_id.startswith('SH'): - return SmartHomeDevice(device_id, 'device_token') - elif device_id.startswith('WR'): - return WearableDevice(device_id, 'device_token') - elif device_id.startswith('AV'): - return AutonomousVehicleDevice(device_id, 'device_token') + if device_id.startswith("SH"): + return SmartHomeDevice(device_id, "device_token") + elif device_id.startswith("WR"): + return WearableDevice(device_id, "device_token") + elif device_id.startswith("AV"): + return AutonomousVehicleDevice(device_id, "device_token") else: return None -if __name__ == '__main__': + +if __name__ == "__main__": app.run(debug=True)