mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Converted response format of add_appointment to json
This commit is contained in:
22
pisa/api.py
22
pisa/api.py
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
from flask import Flask, request, Response, abort, jsonify
|
from flask import Flask, request, Response, abort, jsonify
|
||||||
|
from binascii import hexlify
|
||||||
|
|
||||||
from pisa import HOST, PORT, logging
|
from pisa import HOST, PORT, logging
|
||||||
from pisa.logger import Logger
|
from pisa.logger import Logger
|
||||||
@@ -29,30 +30,39 @@ def add_appointment():
|
|||||||
request_data = json.loads(request.get_json())
|
request_data = json.loads(request.get_json())
|
||||||
appointment = inspector.inspect(request_data)
|
appointment = inspector.inspect(request_data)
|
||||||
|
|
||||||
|
error = None
|
||||||
|
response = None
|
||||||
|
|
||||||
if type(appointment) == Appointment:
|
if type(appointment) == Appointment:
|
||||||
appointment_added, signature = watcher.add_appointment(appointment)
|
appointment_added, signature = watcher.add_appointment(appointment)
|
||||||
|
|
||||||
# ToDo: #13-create-server-side-signature-receipt
|
# ToDo: #13-create-server-side-signature-receipt
|
||||||
if appointment_added:
|
if appointment_added:
|
||||||
rcode = HTTP_OK
|
rcode = HTTP_OK
|
||||||
response = "appointment accepted. locator: {}. signature: {}".format(appointment.locator, signature)
|
response = {
|
||||||
|
"locator": appointment.locator,
|
||||||
|
"signature": hexlify(signature).decode('utf-8')
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
rcode = HTTP_SERVICE_UNAVAILABLE
|
rcode = HTTP_SERVICE_UNAVAILABLE
|
||||||
response = "appointment rejected"
|
error = "appointment rejected"
|
||||||
|
|
||||||
elif type(appointment) == tuple:
|
elif type(appointment) == tuple:
|
||||||
rcode = HTTP_BAD_REQUEST
|
rcode = HTTP_BAD_REQUEST
|
||||||
response = "appointment rejected. Error {}: {}".format(appointment[0], appointment[1])
|
error = "appointment rejected. Error {}: {}".format(appointment[0], appointment[1])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# We should never end up here, since inspect only returns appointments or tuples. Just in case.
|
# We should never end up here, since inspect only returns appointments or tuples. Just in case.
|
||||||
rcode = HTTP_BAD_REQUEST
|
rcode = HTTP_BAD_REQUEST
|
||||||
response = "appointment rejected. Request does not match the standard"
|
error = "appointment rejected. Request does not match the standard"
|
||||||
|
|
||||||
logger.info('Sending response and disconnecting',
|
logger.info('Sending response and disconnecting',
|
||||||
from_addr_port='{}:{}'.format(remote_addr, remote_port), response=response)
|
from_addr_port='{}:{}'.format(remote_addr, remote_port), response=response, error=error)
|
||||||
|
|
||||||
return Response(response, status=rcode, mimetype='text/plain')
|
if error is None:
|
||||||
|
return jsonify(response), rcode
|
||||||
|
else:
|
||||||
|
return jsonify({"error": error}), rcode
|
||||||
|
|
||||||
|
|
||||||
# FIXME: THE NEXT THREE API ENDPOINTS ARE FOR TESTING AND SHOULD BE REMOVED / PROPERLY MANAGED BEFORE PRODUCTION!
|
# FIXME: THE NEXT THREE API ENDPOINTS ARE FOR TESTING AND SHOULD BE REMOVED / PROPERLY MANAGED BEFORE PRODUCTION!
|
||||||
|
|||||||
Reference in New Issue
Block a user