Comparison

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
parent 3861:ede3d1724dd1
child 4246:53ae1df31950
comparison
equal deleted inserted replaced
3862:3b6b8dcff78e 3863:45b04f05d624
3 app = Flask("echobot") 3 app = Flask("echobot")
4 4
5 5
6 @app.route("/api", methods=["OPTIONS"]) 6 @app.route("/api", methods=["OPTIONS"])
7 def options(): 7 def options():
8 """
9 Startup check. Return an appropriate Accept header to confirm the
10 data type to use.
11 """
12
8 return Response(status=200, headers={"accept": "application/json"}) 13 return Response(status=200, headers={"accept": "application/json"})
9 14
10 15
11 @app.route("/api", methods=["POST"]) 16 @app.route("/api", methods=["POST"])
12 def hello(): 17 def hello():
18 """
19 Example RESTful JSON format stanza handler.
20 """
21
13 print(request.data) 22 print(request.data)
14 if request.is_json: 23 if request.is_json:
15 data = request.get_json() 24 data = request.get_json()
16 25
17 if "kind" not in data: 26 if "kind" not in data:
18 return Response(status=400) 27 return Response(status=400)
19 28
20 if data["kind"] == "message" and "body" in data: 29 if data["kind"] == "message" and "body" in data:
30 # Reply to a message
21 return jsonify({"body": "Yes this is flask app"}) 31 return jsonify({"body": "Yes this is flask app"})
22 32
23 elif data["kind"] == "iq" and data["type"] == "get": 33 elif data["kind"] == "iq" and data["type"] == "get":
24 if "ping" in data: 34 if "ping" in data:
35 # Respond to ping
25 return Response(status=204) 36 return Response(status=204)
26 37
27 elif "disco" in data: 38 elif "disco" in data:
39 # Return supported features
28 return jsonify( 40 return jsonify(
29 { 41 {
30 "disco": { 42 "disco": {
31 "identities": [ 43 "identities": [
32 { 44 {
43 } 55 }
44 } 56 }
45 ) 57 )
46 58
47 elif "items" in data: 59 elif "items" in data:
60 # Disco items
48 return jsonify( 61 return jsonify(
49 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]} 62 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]}
50 ) 63 )
51 64
52 elif "version" in data: 65 elif "version" in data:
66 # Version info
53 return jsonify({"version": {"name": "app.py", "version": "0"}}) 67 return jsonify({"version": {"name": "app.py", "version": "0"}})
54 68
55 return Response(status=501) 69 return Response(status=501)
56 70
57 71