Software / code / prosody-modules
Annotate
mod_rest/example/app.py @ 3860:9752a6f1b9f3
mod_rest: Avoid treating special json.null value as any other table
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 25 Jan 2020 02:06:07 +0100 |
| parent | 3854:25c34c9f755c |
| child | 3861:ede3d1724dd1 |
| rev | line source |
|---|---|
| 3853 | 1 from flask import Flask, Response, request, jsonify |
| 2 | |
| 3 app = Flask("echobot") | |
| 4 | |
| 5 | |
| 6 @app.route("/api", methods=["POST"]) | |
| 7 def hello(): | |
| 8 print(request.data) | |
| 9 if request.is_json: | |
| 10 data = request.get_json() | |
| 11 | |
| 12 if "kind" not in data: | |
| 13 return Response(status=400) | |
| 14 | |
| 15 if data["kind"] == "message" and "body" in data: | |
| 16 return jsonify({"body": "Yes this is flask app"}) | |
| 17 | |
| 18 elif data["kind"] == "iq" and data["type"] == "get": | |
| 19 if "ping" in data: | |
| 20 return Response(status=204) | |
| 21 | |
| 22 elif "disco" in data: | |
| 23 return jsonify( | |
| 24 { | |
| 25 "disco": { | |
| 26 "identities": [ | |
| 27 { | |
| 28 "category": "component", | |
| 29 "type": "generic", | |
| 30 "name": "Flask app", | |
| 31 } | |
| 32 ], | |
| 33 "features": [ | |
| 34 "http://jabber.org/protocol/disco#info", | |
| 35 "http://jabber.org/protocol/disco#items", | |
| 36 "urn:xmpp:ping", | |
| 37 ], | |
| 38 } | |
| 39 } | |
| 40 ) | |
| 41 | |
| 42 elif "items" in data: | |
| 43 return jsonify( | |
| 44 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]} | |
| 45 ) | |
| 46 | |
|
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3853
diff
changeset
|
47 elif "version" in data: |
|
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3853
diff
changeset
|
48 return jsonify({"version": {"name": "app.py", "version": "0"}}) |
|
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3853
diff
changeset
|
49 |
| 3853 | 50 return Response(status=501) |
| 51 | |
| 52 | |
| 53 if __name__ == "__main__": | |
| 54 app.run() |