Mercurial > prosody-modules
annotate mod_rest/example/app.py @ 3863:45b04f05d624
mod_rest: Add some comments to example code
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 25 Jan 2020 20:11:00 +0100 |
parents | ede3d1724dd1 |
children | 53ae1df31950 |
rev | line source |
---|---|
3853 | 1 from flask import Flask, Response, request, jsonify |
2 | |
3 app = Flask("echobot") | |
4 | |
5 | |
3861
ede3d1724dd1
mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
6 @app.route("/api", methods=["OPTIONS"]) |
ede3d1724dd1
mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
7 def options(): |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
8 """ |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
9 Startup check. Return an appropriate Accept header to confirm the |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
10 data type to use. |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
11 """ |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
12 |
3861
ede3d1724dd1
mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
13 return Response(status=200, headers={"accept": "application/json"}) |
ede3d1724dd1
mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
14 |
ede3d1724dd1
mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
15 |
3853 | 16 @app.route("/api", methods=["POST"]) |
17 def hello(): | |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
18 """ |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
19 Example RESTful JSON format stanza handler. |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
20 """ |
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
21 |
3853 | 22 print(request.data) |
23 if request.is_json: | |
24 data = request.get_json() | |
25 | |
26 if "kind" not in data: | |
27 return Response(status=400) | |
28 | |
29 if data["kind"] == "message" and "body" in data: | |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
30 # Reply to a message |
3853 | 31 return jsonify({"body": "Yes this is flask app"}) |
32 | |
33 elif data["kind"] == "iq" and data["type"] == "get": | |
34 if "ping" in data: | |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
35 # Respond to ping |
3853 | 36 return Response(status=204) |
37 | |
38 elif "disco" in data: | |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
39 # Return supported features |
3853 | 40 return jsonify( |
41 { | |
42 "disco": { | |
43 "identities": [ | |
44 { | |
45 "category": "component", | |
46 "type": "generic", | |
47 "name": "Flask app", | |
48 } | |
49 ], | |
50 "features": [ | |
51 "http://jabber.org/protocol/disco#info", | |
52 "http://jabber.org/protocol/disco#items", | |
53 "urn:xmpp:ping", | |
54 ], | |
55 } | |
56 } | |
57 ) | |
58 | |
59 elif "items" in data: | |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
60 # Disco items |
3853 | 61 return jsonify( |
62 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]} | |
63 ) | |
64 | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3853
diff
changeset
|
65 elif "version" in data: |
3863
45b04f05d624
mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents:
3861
diff
changeset
|
66 # Version info |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3853
diff
changeset
|
67 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
|
68 |
3853 | 69 return Response(status=501) |
70 | |
71 | |
72 if __name__ == "__main__": | |
73 app.run() |