Annotate

mod_rest/example/app.py @ 3861:ede3d1724dd1

mod_rest: Attempt to auto-discover data type wanted by callback
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2020 20:04:39 +0100
parent 3854:25c34c9f755c
child 3863:45b04f05d624
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():
ede3d1724dd1 mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents: 3854
diff changeset
8 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
9
ede3d1724dd1 mod_rest: Attempt to auto-discover data type wanted by callback
Kim Alvefur <zash@zash.se>
parents: 3854
diff changeset
10
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 @app.route("/api", methods=["POST"])
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 def hello():
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 print(request.data)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 if request.is_json:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 data = request.get_json()
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 if "kind" not in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 return Response(status=400)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if data["kind"] == "message" and "body" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return jsonify({"body": "Yes this is flask app"})
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 elif data["kind"] == "iq" and data["type"] == "get":
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if "ping" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 return Response(status=204)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 elif "disco" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 return jsonify(
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 "disco": {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 "identities": [
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 "category": "component",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 "type": "generic",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 "name": "Flask app",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 }
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 "features": [
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 "http://jabber.org/protocol/disco#info",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 "http://jabber.org/protocol/disco#items",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 "urn:xmpp:ping",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 ],
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 }
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
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 elif "items" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return jsonify(
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]}
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 )
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3853
diff changeset
52 elif "version" in data:
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3853
diff changeset
53 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
54
3853
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return Response(status=501)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if __name__ == "__main__":
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 app.run()