changeset 3853:fb29d7cd698b

mod_rest: Add an examlpe Flask app
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2020 00:37:06 +0100
parents 66f96b98d219
children 25c34c9f755c
files mod_rest/example/app.py
diffstat 1 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_rest/example/app.py	Sat Jan 25 00:37:06 2020 +0100
@@ -0,0 +1,51 @@
+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"}]}
+                )
+
+    return Response(status=501)
+
+
+if __name__ == "__main__":
+    app.run()