200_motels/source/exercice199/daemon.py

78 lines
2.0 KiB
Python

#!/usr/bin/env python
from flask import Flask, request, jsonify
from waitress import serve
import os
def handle(event, context):
return {
"statusCode": 200,
"body": {
"_valide": True,
"_messages": ["T'es un·e champion·ne", "C'est exactement tralala199 que j'attendais"],
'feedbacks_html': "<div>\n<p>Exercice réussi!</p>\n<ul>\n<li>T'es un·e champion·ne</li>\n<li>C'est exactement 'b'coucou'' que j'attendais</li>\n</ul>\n</div>\n"
}
}
app = Flask(__name__)
class Event:
def __init__(self):
self.body = request.get_data()
self.headers = request.headers
self.method = request.method
self.query = request.args
self.path = request.path
class Context:
def __init__(self):
self.hostname = os.getenv('HOSTNAME', 'localhost')
def format_status_code(resp):
if 'statusCode' in resp:
return resp['statusCode']
return 200
def format_body(resp):
if 'body' not in resp:
return ""
elif type(resp['body']) == dict:
return jsonify(resp['body'])
else:
return str(resp['body'])
def format_headers(resp):
if 'headers' not in resp:
return []
elif type(resp['headers']) == dict:
headers = []
for key in resp['headers'].keys():
header_tuple = (key, resp['headers'][key])
headers.append(header_tuple)
return headers
return resp['headers']
def format_response(resp):
if resp == None:
return ('', 200)
statusCode = format_status_code(resp)
body = format_body(resp)
headers = format_headers(resp)
return (body, statusCode, headers)
@app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
@app.route('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
def call_handler(path):
event = Event()
context = Context()
response_data = handle(event, context)
resp = format_response(response_data)
return resp
if __name__ == '__main__':
serve(app, port=8082)