73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
from flask import Flask, jsonify, request
|
|
import requests
|
|
import uuid
|
|
|
|
app = Flask(__name__)
|
|
|
|
builds = {}
|
|
COMPONENTS_SERVICE = "http://microservice_components:5001"
|
|
|
|
def get_component_details(component_id):
|
|
try:
|
|
response = requests.get(f"{COMPONENTS_SERVICE}/{component_id}")
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
return None
|
|
except requests.exceptions.RequestException:
|
|
return None
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def get_builds():
|
|
return jsonify(list(builds.values()))
|
|
|
|
@app.route('/<build_id>', methods=['GET'])
|
|
def get_build(build_id):
|
|
build = builds.get(build_id)
|
|
if not build:
|
|
return jsonify({"error": "Build not found"}), 404
|
|
build_with_details = build.copy()
|
|
build_with_details["components_details"] = []
|
|
|
|
for component_uuid in build.get("components", []):
|
|
component_details = get_component_details(component_uuid)
|
|
if component_details:
|
|
build_with_details["components_details"].append(component_details)
|
|
else:
|
|
return jsonify({"error": "Build not found"}), 404
|
|
|
|
return jsonify(build_with_details)
|
|
|
|
@app.route('/', methods=['POST'])
|
|
def create_build():
|
|
data = request.get_json()
|
|
build_id = str(uuid.uuid4())
|
|
|
|
build = {
|
|
"uuid": build_id,
|
|
"name": data['name'],
|
|
"components": data['component_id']
|
|
}
|
|
|
|
builds[build_id] = build
|
|
return jsonify(build), 201
|
|
|
|
@app.route('/<build_id>', methods=['PUT'])
|
|
def update_build(build_id):
|
|
build = builds.get(str(build_id))
|
|
if not build:
|
|
return jsonify({'error': 'Not found'}), 404
|
|
data = request.json
|
|
build['name'] = data.get('name', build['name'])
|
|
build["components"] = data.get('components', build['components'])
|
|
return jsonify(build)
|
|
|
|
@app.route('/<build_id>', methods=['DELETE'])
|
|
def delete_build(build_id):
|
|
if build_id not in builds:
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
del builds[build_id]
|
|
return jsonify({"message": "Build deleted"}), 200
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5002, debug=True) |