annotate mod_rest/example/app.py @ 5546:ae20da6d377d

mod_http_oauth2: Link to RFC 7628 in README Links are good.
author Kim Alvefur <zash@zash.se>
date Sun, 11 Jun 2023 14:03:27 +0200
parents f3fbfde9683d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 from flask import Flask, Response, request, jsonify
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 app = Flask("echobot")
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
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
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 @app.route("/api", methods=["POST"])
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
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
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 print(request.data)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if request.is_json:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 data = request.get_json()
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if "kind" not in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return Response(status=400)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
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
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 return jsonify({"body": "Yes this is flask app"})
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 elif data["kind"] == "iq" and data["type"] == "get":
4248
f3fbfde9683d mod_rest/example: Reorder things to match the disco#info feature order
Kim Alvefur <zash@zash.se>
parents: 4246
diff changeset
34 if "disco" in data:
3863
45b04f05d624 mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents: 3861
diff changeset
35 # Return supported features
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 return jsonify(
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 "disco": {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 "identities": [
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 "category": "component",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 "type": "generic",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 "name": "Flask app",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 }
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 ],
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 "features": [
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 "http://jabber.org/protocol/disco#info",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 "http://jabber.org/protocol/disco#items",
4246
53ae1df31950 mod_rest/example: Advertise version query support
Kim Alvefur <zash@zash.se>
parents: 3863
diff changeset
49 "jabber:iq:version",
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 "urn:xmpp:ping",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 ],
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 }
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 }
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 )
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 elif "items" in data:
3863
45b04f05d624 mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents: 3861
diff changeset
57 # Disco items
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 return jsonify(
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]}
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 )
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3853
diff changeset
62 elif "version" in data:
3863
45b04f05d624 mod_rest: Add some comments to example code
Kim Alvefur <zash@zash.se>
parents: 3861
diff changeset
63 # Version info
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3853
diff changeset
64 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
65
4248
f3fbfde9683d mod_rest/example: Reorder things to match the disco#info feature order
Kim Alvefur <zash@zash.se>
parents: 4246
diff changeset
66 elif "ping" in data:
f3fbfde9683d mod_rest/example: Reorder things to match the disco#info feature order
Kim Alvefur <zash@zash.se>
parents: 4246
diff changeset
67 # Respond to ping
f3fbfde9683d mod_rest/example: Reorder things to match the disco#info feature order
Kim Alvefur <zash@zash.se>
parents: 4246
diff changeset
68 return Response(status=204)
f3fbfde9683d mod_rest/example: Reorder things to match the disco#info feature order
Kim Alvefur <zash@zash.se>
parents: 4246
diff changeset
69
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 return Response(status=501)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if __name__ == "__main__":
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 app.run()