From dd54a1ff5edfa5c0857748a77af1fcdd1c382d10 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Jul 2025 12:44:50 +1000 Subject: [PATCH] feat: Add API routes --- server.py | 88 ++++++++++++++++++++++++++++++++++++++++++ templates/account.html | 2 +- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index 9d5018e..25181c7 100644 --- a/server.py +++ b/server.py @@ -238,6 +238,94 @@ def catch_all(path: str): return render_template("404.html"), 404 +# endregion + +# region API routes +@app.route("/api/v1/domain/") +def api_get_domain(domain: str): + """ + Get the expiry date of a domain. + """ + expiry_date = domains.get_domain_expiry_block(domain) + current_block = domains.get_current_block() + expires_in_blocks = expiry_date - current_block if expiry_date != -1 else -1 + + return jsonify({"domain": domain, "expiry_date": expiry_date, "expires_in_blocks": expires_in_blocks}) + +@app.route("/api/v1/current_block") +def api_get_current_block(): + """ + Get the current block number. + """ + current_block = domains.get_current_block() + + return jsonify({"current_block": current_block}) + +@app.route("/api/v1/notifications/") +def api_get_notifications(token: str): + """ + Get all notifications for a user. + """ + user_data = requests.get(f"https://login.hns.au/auth/user?token={token}") + if user_data.status_code != 200: + return jsonify({"error": "Invalid token"}), 401 + user_data = user_data.json() + + notifications = domains.get_account_notifications(user_data["username"]) + + return jsonify(notifications) + +@app.route("/api/v1/notifications/", methods=["POST"]) +def api_add_notification(token: str): + """ + Add a notification for a user. + """ + user_data = requests.get(f"https://login.hns.au/auth/user?token={token}") + if user_data.status_code != 200: + return jsonify({"error": "Invalid token"}), 401 + user_data = user_data.json() + + username = user_data.get("username", None) + if not username: + return jsonify({"error": "Invalid user data"}), 400 + + data = request.json + if not data or 'domain' not in data or 'blocks' not in data or 'type' not in data: + return jsonify({"error": "Invalid request data"}), 400 + + domain = data['domain'] + + notificationtype = data['type'] + notificationType = None + for notification in NOTIFICATION_TYPES: + if notification['type'] == notificationtype: + notificationType = notification + break + else: + return jsonify({"error": "Invalid notification type"}), 400 + + for field in notificationType['fields']: + if field['name'] not in data and field.get('required', False): + return jsonify({"error": f"Missing field: {field['name']}"}), 400 + # Validate blocks + try: + blocks = int(data['blocks']) + if blocks <= 0: + return jsonify({"error": "Blocks must be a positive integer"}), 400 + except ValueError: + return jsonify({"error": "Invalid blocks value"}), 400 + + notification = data + notification['blocks'] = blocks + notification['type'] = notificationtype + notification['id'] = os.urandom(16).hex() # Generate a random ID for the notification + notification['user_name'] = username + # Delete domain duplicate from data + notification.pop('domain', None) + domains.add_notification(domain, notification) + return jsonify({"message": "Notification added successfully", "notification": notification}), 201 + + # endregion diff --git a/templates/account.html b/templates/account.html index b6a1d7b..0c0178c 100644 --- a/templates/account.html +++ b/templates/account.html @@ -86,7 +86,7 @@
- +