from flask import Flask, request, jsonify
from Temps2 import convert_temperature

app = Flask(__name__)


@app.route('/convert', methods=['POST'])
def convert():
    # 1. Grab the JSON data sent by the client
    data = request.get_json()
    
    # 2. Check if data was actually provided
    if not data:
        return jsonify({"error": "Missing JSON request body"}), 400
    
    try:
        # 3. Pass the dictionary directly to your function
        result = convert_temperature(data)
        
        # 4. Return the calculated dictionary back as JSON
        return jsonify(result), 200
        
    except Exception as e:
        # Catch errors (like missing keys or an invalid scale)
        return jsonify({"error": str(e)}), 400


if __name__ == '__main__':
    app.run(debug=True)

