This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.py
More file actions
43 lines (30 loc) · 1.22 KB
/
Copy pathserver.py
File metadata and controls
43 lines (30 loc) · 1.22 KB
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
from flask import Flask, request, jsonify, request_started, request_finished
from jwkest.jwk import SYMKey
from cachelib import SimpleCache
from exceptions import HttpException
from signals import OrderedSignalHandler
app = Flask(__name__)
signal_handler = OrderedSignalHandler()
request_started.connect(signal_handler.request_started_handler, app)
request_finished.connect(signal_handler.request_finished_handler, app)
@app.errorhandler(Exception)
def error_handler(exception: Exception):
if isinstance(exception, HttpException):
response = jsonify({'error': exception.message}), exception.code
else:
response = jsonify({'error': str(exception)}), 500
return response
@app.route('/', methods=('GET', 'POST'))
def root():
if request.method == 'POST':
try:
name = request.json['name']
if not isinstance(name, str) or not 0 < len(name) < 26:
raise HttpException("Name attribute must be a string between 1 and 25 characters", 400)
except (TypeError, KeyError):
raise HttpException("Must be JSON object with a name attribute!", 400)
else:
name = 'World!'
return jsonify(Hello=name), 200
if __name__ == '__main__':
app.run()