from flasgger import Swagger, swag_from
from flask import Flask, request
from nameko.standalone.rpc import ClusterRpcProxy

app = Flask(__name__)
Swagger(app)
CONFIG = {"AMQP_URI": "amqp://guest:guest@localhost"}


@app.route("/compute", methods=["POST"])
@swag_from("compute.yml")
def compute():
    operation = request.json.get("operation")
    value = request.json.get("value")
    other = request.json.get("other")
    email = request.json.get("email")
    msg = "Please wait the calculation, you'll receive an email with results"
    subject = "API Notification"
    with ClusterRpcProxy(CONFIG) as rpc:
        # asynchronously spawning and email notification
        rpc.mail.send.call_async(email, subject, msg)
        # asynchronously spawning the compute task
        result = rpc.compute.compute.call_async(operation, value, other, email)
        print(f"{result.result()}\n")
        rpc.service_a.dispatching_method.call_async("send message")

        return msg, 200


if __name__ == "__main__":
    app.run(debug=True)
