annotate mod_rest/example/app.py @ 3853:fb29d7cd698b

mod_rest: Add an examlpe Flask app
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2020 00:37:06 +0100
parents
children 25c34c9f755c
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
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 @app.route("/api", methods=["POST"])
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 def hello():
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 print(request.data)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 if request.is_json:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 data = request.get_json()
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 if "kind" not in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 return Response(status=400)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if data["kind"] == "message" and "body" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 return jsonify({"body": "Yes this is flask app"})
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 elif data["kind"] == "iq" and data["type"] == "get":
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if "ping" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 return Response(status=204)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 elif "disco" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 return jsonify(
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 "disco": {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 "identities": [
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 {
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 "category": "component",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 "type": "generic",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 "name": "Flask app",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 }
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 "features": [
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 "http://jabber.org/protocol/disco#info",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 "http://jabber.org/protocol/disco#items",
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 "urn:xmpp:ping",
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 }
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 }
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
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 elif "items" in data:
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 return jsonify(
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 {"items": [{"jid": "example.org", "name": "Example Dot Org"}]}
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 return Response(status=501)
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if __name__ == "__main__":
fb29d7cd698b mod_rest: Add an examlpe Flask app
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 app.run()