-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
54 lines (46 loc) · 1.53 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pyjokes
from flask import Flask, request, jsonify, redirect
app = Flask(__name__)
app.json.sort_keys = False
app.json.ensure_ascii = False
@app.route("/")
def index():
return redirect("/docs/")
@app.route("/docs")
@app.route("/docs/")
def docs():
api_routes = {
"routes": {
"/": {
"description": "Redirects to the docs page."
},
"/docs": {
"description": "Shows the routes for this api."
},
"/joke": {
"description": "Tells a random joke.",
"parameters": {
"lang": {
"description": "Specifies the language of the joke.",
"valid_options": "'en', 'de', 'es', 'gl', 'eu'', 'it'"
}
}
}
}
}
return jsonify(api_routes), 200
@app.route("/joke/")
def jokeredirect():
return redirect("/joke")
@app.route("/joke", methods=["GET"])
def joke():
lang = "en" if request.args.get("lang") is None else request.args.get("lang")
if lang.lower() not in ["en", "de", "es", "gl", "eu", "it"]:
return jsonify({"error": "Invalid lang value. Valid options are 'en', 'de', 'es', 'gl', 'eu', or 'it'"}), 400
try:
joke = pyjokes.get_joke(language=lang)
return jsonify({"joke": joke}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)