Created
December 4, 2023 22:25
-
-
Save yujiterada/4bd7332de926525ee388ba2c1219779c to your computer and use it in GitHub Desktop.
Flask app to receive MT30 webhooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
import meraki | |
import json | |
import os | |
import threading | |
import time | |
app = Flask(__name__) | |
SHARED_SECRET = "Meraki1234" | |
API_KEY = os.environ.get('MERAKI_API_KEY') | |
DATA = { | |
"Q3CH-XXXX-YYYY": { | |
"Q4CD-XXXX-YYYY": [11] | |
} | |
} | |
def background_task(data): | |
# This code will be executed in the background after sending the response | |
print("Background task is running") | |
m_client = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True) | |
for serial, ports in DATA[data['deviceSerial']].items(): | |
for port in ports: | |
resp = m_client.switch.updateDeviceSwitchPort( | |
serial=serial, portId=port, portScheduleId=None) | |
print(json.dumps(resp, indent=4)) | |
@app.route('/receive_json', methods=['POST']) | |
def receive_json(): | |
try: | |
# Get JSON data from the request | |
data = request.get_json() | |
if data.get('sharedSecret') != SHARED_SECRET: | |
raise ValueError("Invalid sharedSecret provided.") | |
# Start the background task | |
threading.Thread(target=background_task(data)).start() | |
# Create response to Dashboard | |
response = jsonify(success=True), 200 | |
# Respond to Dashboard | |
return response | |
except Exception as e: | |
print("An error occurred:", str(e)) | |
return jsonify(success=False, error=str(e)), 400 | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment