1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Some improvements over REST api for actor

This commit is contained in:
Adolfo Gómez García 2015-07-14 12:47:03 +02:00
parent d4135d527a
commit 6979d9bc17

View File

@ -51,6 +51,10 @@ startTime = time.time()
class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.0'
server_version = 'UDS Actor Server'
sys_version = ''
uuid = None uuid = None
service = None service = None
lock = threading.Lock() lock = threading.Lock()
@ -62,6 +66,15 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.wfile.write(json.dumps({'error': message})) self.wfile.write(json.dumps({'error': message}))
return return
def sendJsonResponse(self, data):
self.send_response(200)
data = json.dumps(data)
self.send_header('Content-type', 'application/json')
self.send_header('Content-Length', len(data))
self.end_headers()
# Send the html message
self.wfile.write(data)
def do_GET(self): def do_GET(self):
# Very simple path & params splitter # Very simple path & params splitter
path = self.path.split('?')[0][1:].split('/') path = self.path.split('?')[0][1:].split('/')
@ -75,11 +88,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
return return
if len(path) != 2: if len(path) != 2:
self.send_response(200) self.sendJsonResponse("UDS Actor has been running for {} seconds".format(time.time() - startTime))
self.send_header('Content-type', 'application/json')
self.end_headers()
# Send the html message
self.wfile.write(json.dumps("UDS Actor has been running for {} seconds".format(time.time() - startTime)))
return return
try: try:
@ -93,11 +102,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.sendJsonError(500, str(e)) self.sendJsonError(500, str(e))
return return
self.send_response(200) self.sendJsonResponse(result)
self.send_header('Content-type', 'application/json')
self.end_headers()
# Send the html message
self.wfile.write(json.dumps(result))
def do_POST(self): def do_POST(self):
path = self.path.split('?')[0][1:].split('/') path = self.path.split('?')[0][1:].split('/')
@ -113,7 +118,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
HTTPServerHandler.lock.acquire() HTTPServerHandler.lock.acquire()
length = int(self.headers.getheader('content-length')) length = int(self.headers.getheader('content-length'))
content = self.rfile.read(length) content = self.rfile.read(length)
print(length, ">>", content, '<<') logger.debug('length: {}, content >>{}<<'.format(length, content))
params = json.loads(content) params = json.loads(content)
operation = getattr(self, 'post_' + path[1]) operation = getattr(self, 'post_' + path[1])
@ -128,11 +133,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
finally: finally:
HTTPServerHandler.lock.release() HTTPServerHandler.lock.release()
self.send_response(200) self.sendJsonResponse(result)
self.send_header('Content-type', 'application/json')
self.end_headers()
# Send the html message
self.wfile.write(json.dumps(result))
def post_logoff(self, params): def post_logoff(self, params):
logger.debug('Sending LOGOFF to clients') logger.debug('Sending LOGOFF to clients')
@ -173,6 +174,12 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# TODO: Return something useful? :) # TODO: Return something useful? :)
return 'Information' return 'Information'
def log_error(self, fmt, *args):
logger.error('HTTP ' + fmt % args)
def log_message(self, fmt, *args):
logger.info('HTTP ' + fmt % args)
class HTTPServerThread(threading.Thread): class HTTPServerThread(threading.Thread):
def __init__(self, address, service): def __init__(self, address, service):