view mod_rest/example/app.py @ 3856:8bdb1729529b

mod_rest: Make XHTML-IM mapping more convenient Avoid burdening web developers with XML namespace attribute Might be a bit fragile Note that the <body> element must be included
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2020 00:46:09 +0100
parents 25c34c9f755c
children ede3d1724dd1
line wrap: on
line source

from flask import Flask, Response, request, jsonify

app = Flask("echobot")


@app.route("/api", methods=["POST"])
def hello():
    print(request.data)
    if request.is_json:
        data = request.get_json()

        if "kind" not in data:
            return Response(status=400)

        if data["kind"] == "message" and "body" in data:
            return jsonify({"body": "Yes this is flask app"})

        elif data["kind"] == "iq" and data["type"] == "get":
            if "ping" in data:
                return Response(status=204)

            elif "disco" in data:
                return jsonify(
                    {
                        "disco": {
                            "identities": [
                                {
                                    "category": "component",
                                    "type": "generic",
                                    "name": "Flask app",
                                }
                            ],
                            "features": [
                                "http://jabber.org/protocol/disco#info",
                                "http://jabber.org/protocol/disco#items",
                                "urn:xmpp:ping",
                            ],
                        }
                    }
                )

            elif "items" in data:
                return jsonify(
                    {"items": [{"jid": "example.org", "name": "Example Dot Org"}]}
                )

            elif "version" in data:
                return jsonify({"version": {"name": "app.py", "version": "0"}})

    return Response(status=501)


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