Refactor code
This commit is contained in:
parent
86ff31c03e
commit
f531fc487c
46
src/api.py
46
src/api.py
|
@ -24,21 +24,9 @@ def create_endpoint():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
new_url = data.get("url")
|
new_url = data.get("url")
|
||||||
|
|
||||||
if new_url is None or not is_valid_url(new_url):
|
endpoint_exists = check_endpoint_exists(data, new_url)
|
||||||
return jsonify({"status": "error", "message": "Invalid or missing URL"}), 400
|
if endpoint_exists:
|
||||||
|
return endpoint_exists
|
||||||
existing_endpoint = get_endpoint_by_url(new_url)
|
|
||||||
if existing_endpoint:
|
|
||||||
return (
|
|
||||||
jsonify(
|
|
||||||
{
|
|
||||||
"status": "error",
|
|
||||||
"message": "Endpoint already exists",
|
|
||||||
"id": existing_endpoint["id"],
|
|
||||||
}
|
|
||||||
),
|
|
||||||
409,
|
|
||||||
)
|
|
||||||
|
|
||||||
endpoint_id = add_endpoint_to_db(new_url)
|
endpoint_id = add_endpoint_to_db(new_url)
|
||||||
return jsonify({"id": endpoint_id, "url": new_url}), 201
|
return jsonify({"id": endpoint_id, "url": new_url}), 201
|
||||||
|
@ -58,14 +46,15 @@ def update_endpoint(endpoint_id):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
new_url = data.get("url")
|
new_url = data.get("url")
|
||||||
|
|
||||||
if not new_url or not is_valid_url(new_url):
|
endpoint_exists = check_endpoint_exists(data, new_url)
|
||||||
return jsonify({"status": "error", "message": "Invalid or missing URL"}), 400
|
if endpoint_exists:
|
||||||
|
return endpoint_exists
|
||||||
|
|
||||||
updated = update_endpoint_in_db(endpoint_id, new_url)
|
updated = update_endpoint_in_db(endpoint_id, new_url)
|
||||||
if updated:
|
if updated:
|
||||||
return "", 204
|
return "", 204
|
||||||
else:
|
else:
|
||||||
return jsonify({"status": "error", "message": "Endpoint not found"}), 404
|
return jsonify({"message": "Endpoint not found"}), 404
|
||||||
|
|
||||||
|
|
||||||
@api.route("/api/endpoints/<int:endpoint_id>", methods=["DELETE"])
|
@api.route("/api/endpoints/<int:endpoint_id>", methods=["DELETE"])
|
||||||
|
@ -74,9 +63,28 @@ def delete_endpoint(endpoint_id):
|
||||||
if deleted:
|
if deleted:
|
||||||
return "", 204
|
return "", 204
|
||||||
else:
|
else:
|
||||||
return jsonify({"status": "error", "message": "Endpoint not found"}), 404
|
return jsonify({"message": "Endpoint not found"}), 404
|
||||||
|
|
||||||
|
|
||||||
@api.route("/api/health", methods=["GET"])
|
@api.route("/api/health", methods=["GET"])
|
||||||
def health():
|
def health():
|
||||||
return jsonify({"status": "ok"})
|
return jsonify({"status": "ok"})
|
||||||
|
|
||||||
|
|
||||||
|
def check_endpoint_exists(data, new_url):
|
||||||
|
new_url = data.get("url")
|
||||||
|
|
||||||
|
if not new_url or not is_valid_url(new_url):
|
||||||
|
return jsonify({"message": "Invalid or missing URL"}), 400
|
||||||
|
|
||||||
|
existing_endpoint = get_endpoint_by_url(new_url)
|
||||||
|
if existing_endpoint:
|
||||||
|
return (
|
||||||
|
jsonify(
|
||||||
|
{
|
||||||
|
"message": "Endpoint already exists",
|
||||||
|
"id": existing_endpoint["id"],
|
||||||
|
}
|
||||||
|
),
|
||||||
|
409,
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue