feat: Update api routes to use similar json format to other routes
This commit is contained in:
@@ -4,7 +4,7 @@ import datetime
|
|||||||
import requests
|
import requests
|
||||||
from mail import sendEmail
|
from mail import sendEmail
|
||||||
from sol import create_transaction
|
from sol import create_transaction
|
||||||
from tools import getClientIP, getGitCommit
|
from tools import getClientIP, getGitCommit, json_response
|
||||||
|
|
||||||
|
|
||||||
api_bp = Blueprint('api', __name__)
|
api_bp = Blueprint('api', __name__)
|
||||||
@@ -32,13 +32,18 @@ def help_get():
|
|||||||
"/version": "Get the current version of the website",
|
"/version": "Get the current version of the website",
|
||||||
"/help": "Get this help message"
|
"/help": "Get this help message"
|
||||||
},
|
},
|
||||||
"version": getGitCommit()
|
"base_url": "/api/v1",
|
||||||
|
"version": getGitCommit(),
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/version")
|
@api_bp.route("/version")
|
||||||
def version_get():
|
def version_get():
|
||||||
return jsonify({"version": getGitCommit()})
|
return jsonify({"version": getGitCommit()})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/time")
|
@api_bp.route("/time")
|
||||||
def time_get():
|
def time_get():
|
||||||
timezone_offset = datetime.timedelta(hours=ncConfig["time-zone"])
|
timezone_offset = datetime.timedelta(hours=ncConfig["time-zone"])
|
||||||
@@ -48,12 +53,20 @@ def time_get():
|
|||||||
"timestring": time.strftime("%A, %B %d, %Y %I:%M %p"),
|
"timestring": time.strftime("%A, %B %d, %Y %I:%M %p"),
|
||||||
"timestamp": time.timestamp(),
|
"timestamp": time.timestamp(),
|
||||||
"timezone": ncConfig["time-zone"],
|
"timezone": ncConfig["time-zone"],
|
||||||
"timeISO": time.isoformat()
|
"timeISO": time.isoformat(),
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/timezone")
|
@api_bp.route("/timezone")
|
||||||
def timezone_get():
|
def timezone_get():
|
||||||
return jsonify({"timezone": ncConfig["time-zone"]})
|
return jsonify({
|
||||||
|
"timezone": ncConfig["time-zone"],
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/timezone", methods=["POST"])
|
@api_bp.route("/timezone", methods=["POST"])
|
||||||
def timezone_post():
|
def timezone_post():
|
||||||
@@ -62,88 +75,93 @@ def timezone_post():
|
|||||||
conf = requests.get(
|
conf = requests.get(
|
||||||
"https://cloud.woodburn.au/s/4ToXgFe3TnnFcN7/download/website-conf.json")
|
"https://cloud.woodburn.au/s/4ToXgFe3TnnFcN7/download/website-conf.json")
|
||||||
if conf.status_code != 200:
|
if conf.status_code != 200:
|
||||||
return jsonify({"message": "Error: Could not get timezone"})
|
return json_response(request, "Error: Could not get timezone", 500)
|
||||||
if not conf.json():
|
if not conf.json():
|
||||||
return jsonify({"message": "Error: Could not get timezone"})
|
return json_response(request, "Error: Could not get timezone", 500)
|
||||||
conf = conf.json()
|
conf = conf.json()
|
||||||
if "time-zone" not in conf:
|
if "time-zone" not in conf:
|
||||||
return jsonify({"message": "Error: Could not get timezone"})
|
return json_response(request, "Error: Could not get timezone", 500)
|
||||||
|
|
||||||
ncConfig = conf
|
ncConfig = conf
|
||||||
return jsonify({"message": "Successfully pulled latest timezone", "timezone": ncConfig["time-zone"]})
|
return jsonify({
|
||||||
|
"message": "Successfully pulled latest timezone",
|
||||||
|
"timezone": ncConfig["time-zone"],
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/message")
|
@api_bp.route("/message")
|
||||||
def message_get():
|
def message_get():
|
||||||
return jsonify({"message": ncConfig["message"]})
|
return jsonify({
|
||||||
|
"message": ncConfig["message"],
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/ip")
|
@api_bp.route("/ip")
|
||||||
def ip_get():
|
def ip_get():
|
||||||
return jsonify({"ip": getClientIP(request)})
|
return jsonify({
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/email", methods=["POST"])
|
@api_bp.route("/email", methods=["POST"])
|
||||||
def email_post():
|
def email_post():
|
||||||
# Verify json
|
# Verify json
|
||||||
if not request.is_json:
|
if not request.is_json:
|
||||||
return jsonify({
|
return json_response(request, "415 Unsupported Media Type", 415)
|
||||||
"status": 400,
|
|
||||||
"error": "Bad request JSON Data missing"
|
|
||||||
})
|
|
||||||
|
|
||||||
# Check if api key sent
|
# Check if api key sent
|
||||||
data = request.json
|
data = request.json
|
||||||
if not data:
|
if not data:
|
||||||
return jsonify({
|
return json_response(request, "400 Bad Request", 400)
|
||||||
"status": 400,
|
|
||||||
"error": "Bad request JSON Data missing"
|
|
||||||
})
|
|
||||||
|
|
||||||
if "key" not in data:
|
if "key" not in data:
|
||||||
return jsonify({
|
return json_response(request, "400 Bad Request 'key' missing", 400)
|
||||||
"status": 401,
|
|
||||||
"error": "Unauthorized 'key' missing"
|
|
||||||
})
|
|
||||||
|
|
||||||
if data["key"] != os.getenv("EMAIL_KEY"):
|
if data["key"] != os.getenv("EMAIL_KEY"):
|
||||||
return jsonify({
|
return json_response(request, "401 Unauthorized", 401)
|
||||||
"status": 401,
|
|
||||||
"error": "Unauthorized 'key' invalid"
|
|
||||||
})
|
|
||||||
|
|
||||||
|
# TODO: Add client info to email
|
||||||
return sendEmail(data)
|
return sendEmail(data)
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/project")
|
@api_bp.route("/project")
|
||||||
def project_get():
|
def project_get():
|
||||||
|
gitinfo = {
|
||||||
|
"website": None,
|
||||||
|
}
|
||||||
try:
|
try:
|
||||||
git = requests.get(
|
git = requests.get(
|
||||||
"https://git.woodburn.au/users/nathanwoodburn/activities/feeds?only-performed-by=true&limit=1",
|
"https://git.woodburn.au/api/v1/users/nathanwoodburn/activities/feeds?only-performed-by=true&limit=1",
|
||||||
headers={"Authorization": os.getenv("git_token")},
|
headers={"Authorization": os.getenv("git_token")},
|
||||||
)
|
)
|
||||||
git = git.json()
|
git = git.json()
|
||||||
git = git[0]
|
git = git[0]
|
||||||
repo_name = git["repo"]["name"]
|
repo_name = git["repo"]["name"]
|
||||||
repo_name = repo_name.lower()
|
repo_name = repo_name.lower()
|
||||||
repo_description = git["repo"]["description"]
|
repo_description = git["repo"]["description"]
|
||||||
|
gitinfo["name"] = repo_name
|
||||||
|
gitinfo["description"] = repo_description
|
||||||
|
gitinfo["url"] = git["repo"]["html_url"]
|
||||||
|
if "website" in git["repo"]:
|
||||||
|
gitinfo["website"] = git["repo"]["website"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
repo_name = "nathanwoodburn.github.io"
|
|
||||||
repo_description = "Personal website"
|
|
||||||
git = {
|
|
||||||
"repo": {
|
|
||||||
"html_url": "https://nathan.woodburn.au",
|
|
||||||
"name": "nathanwoodburn.github.io",
|
|
||||||
"description": "Personal website",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
print(f"Error getting git data: {e}")
|
print(f"Error getting git data: {e}")
|
||||||
|
return json_response(request, "500 Internal Server Error", 500)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"repo_name": repo_name,
|
"repo_name": repo_name,
|
||||||
"repo_description": repo_description,
|
"repo_description": repo_description,
|
||||||
"git": git,
|
"repo": gitinfo,
|
||||||
|
"ip": getClientIP(request),
|
||||||
|
"status": 200
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
# region Solana Links
|
# region Solana Links
|
||||||
SOLANA_HEADERS = {
|
SOLANA_HEADERS = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -152,7 +170,6 @@ SOLANA_HEADERS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/donate", methods=["GET", "OPTIONS"])
|
@api_bp.route("/donate", methods=["GET", "OPTIONS"])
|
||||||
def sol_donate_get():
|
def sol_donate_get():
|
||||||
data = {
|
data = {
|
||||||
@@ -204,8 +221,7 @@ def sol_donate_post(amount):
|
|||||||
|
|
||||||
if not request.json:
|
if not request.json:
|
||||||
return jsonify({"message": "Error: No JSON data provided"}), 400, SOLANA_HEADERS
|
return jsonify({"message": "Error: No JSON data provided"}), 400, SOLANA_HEADERS
|
||||||
|
|
||||||
|
|
||||||
if "account" not in request.json:
|
if "account" not in request.json:
|
||||||
return jsonify({"message": "Error: No account provided"}), 400, SOLANA_HEADERS
|
return jsonify({"message": "Error: No account provided"}), 400, SOLANA_HEADERS
|
||||||
|
|
||||||
@@ -215,7 +231,7 @@ def sol_donate_post(amount):
|
|||||||
try:
|
try:
|
||||||
amount = float(amount)
|
amount = float(amount)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
amount = 1 # Default to 1 SOL if invalid
|
amount = 1 # Default to 1 SOL if invalid
|
||||||
|
|
||||||
if amount < 0.0001:
|
if amount < 0.0001:
|
||||||
return jsonify({"message": "Error: Amount too small"}), 400, SOLANA_HEADERS
|
return jsonify({"message": "Error: Amount too small"}), 400, SOLANA_HEADERS
|
||||||
@@ -224,4 +240,3 @@ def sol_donate_post(amount):
|
|||||||
return jsonify({"message": "Success", "transaction": transaction}), 200, SOLANA_HEADERS
|
return jsonify({"message": "Success", "transaction": transaction}), 200, SOLANA_HEADERS
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
|||||||
@@ -207,6 +207,8 @@ def links_get():
|
|||||||
|
|
||||||
@app.route("/api/<path:function>")
|
@app.route("/api/<path:function>")
|
||||||
def api_legacy_get(function):
|
def api_legacy_get(function):
|
||||||
|
# Check if function is in api blueprint
|
||||||
|
|
||||||
return redirect(f"/api/v1/{function}", code=301)
|
return redirect(f"/api/v1/{function}", code=301)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user