Files
python-teos/pisa-btc/pisa/api.py
Sergi Delgado Segura 74ecf0ab54 Add temporary monitoring tools for pisa
Add endpoint and logic so appointment can be queried to pisa. A better implementation based on persistent storage (i.e. DB) should replace it in the future
2019-08-09 15:00:02 +01:00

121 lines
4.0 KiB
Python

from pisa import *
from pisa.watcher import Watcher
from pisa.inspector import Inspector
from pisa.appointment import Appointment
from flask import Flask, request, Response, abort
import json
app = Flask(__name__)
HTTP_OK = 200
HTTP_BAD_REQUEST = 400
@app.route('/', methods=['POST'])
def add_appointment():
remote_addr = request.environ.get('REMOTE_ADDR')
remote_port = request.environ.get('REMOTE_PORT')
if debug:
logging.info('[API] connection accepted from {}:{}'.format(remote_addr, remote_port))
# Check content type once if properly defined
request_data = json.loads(request.get_json())
appointment = inspector.inspect(request_data)
if type(appointment) == Appointment:
appointment_added = watcher.add_appointment(appointment, debug, logging)
rcode = HTTP_OK
# FIXME: Response should be signed receipt (created and signed by the API)
if appointment_added:
response = "appointment accepted"
else:
response = "appointment rejected"
# FIXME: change the response code maybe?
elif type(appointment) == tuple:
rcode = HTTP_BAD_REQUEST
response = "appointment rejected. Error {}: {}".format(appointment[0], appointment[1])
else:
rcode = HTTP_BAD_REQUEST
response = "appointment rejected. Request does not match the standard"
# Send response back. Change multiprocessing.connection for an http based connection
if debug:
logging.info('[API] sending response and disconnecting: {} --> {}:{}'.format(response, remote_addr,
remote_port))
return Response(response, status=rcode, mimetype='text/plain')
# FIXME: THE NEXT TWO API ENDPOINTS ARE FOR TESTING AND SHOULD BE REMOVED / PROPERLY MANAGED BEFORE PRODUCTION!
@app.route('/get_appointment', methods=['GET'])
def get_appointment():
locator = request.args.get('locator')
response = []
job_in_watcher = watcher.appointments.get(locator)
if job_in_watcher:
for job in job_in_watcher:
job_data = job.to_json()
job_data['status'] = "being watched"
response.append(job_data)
if watcher.responder:
responder_jobs = watcher.responder.jobs
for job_id, job in responder_jobs.items():
if job.locator == locator:
job_data = job.to_json()
job_data['status'] = "dispute responded"
job_data['confirmations'] = watcher.responder.confirmation_counter.get(job_id)
response.append(job_data)
if not response:
response.append({"locator": locator, "status": "not found"})
response = json.dumps(response)
return response
@app.route('/get_all_appointments', methods=['GET'])
def get_all_appointments():
watcher_appointments = []
responder_jobs = []
if request.remote_addr in ['localhost', '127.0.0.1']:
for app_id, appointment in watcher.appointments.items():
jobs_data = [job.to_json() for job in appointment]
watcher_appointments.append({app_id: jobs_data})
if watcher.responder:
for job_id, job in watcher.responder.jobs.items():
job_data = job.to_json()
job_data['confirmations'] = watcher.responder.confirmation_counter.get(job_id)
responder_jobs.append({job_id: job_data})
response = json.dumps({"watcher_appointments": watcher_appointments, "responder_jobs": responder_jobs})
else:
abort(404)
return response
def start_api(d, l):
# FIXME: Pretty ugly but I haven't found a proper way to pass it to add_appointment
global debug, logging, watcher, inspector
debug = d
logging = l
watcher = Watcher()
inspector = Inspector(debug, logging)
# Setting Flask log t ERROR only so it does not mess with out logging
logging.getLogger('werkzeug').setLevel(logging.ERROR)
app.run(host=HOST, port=PORT)