From 5aa6e6a26cbc351f312f6172263c3c1177532a65 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Sun, 9 Feb 2014 19:26:48 -0500 Subject: [PATCH] Further make the sockapi and confetty client fleshed out Here, fix a few mistakes with how relation objects got messaged out. Add the ability to cd and ls in the confetty interactive mode --- bin/confetty | 29 ++++++++++++++++++++++++----- confluent/messages.py | 2 +- confluent/sockapi.py | 9 ++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/bin/confetty b/bin/confetty index fcaf8f13..9fc19886 100755 --- a/bin/confetty +++ b/bin/confetty @@ -71,8 +71,19 @@ def parse_command(command): args = shlex.split(command, posix=True) return args + +currchildren = None + +def send_request(operation, path, server): + tlvdata.send_tlvdata(server, {'operation': operation, 'path': path}) + result = tlvdata.recv_tlvdata(server) + while '_requestdone' not in result: + yield result + result = tlvdata.recv_tlvdata(server) + def do_command(command, server): global target + global currchildren if command == "": # result of ctrl-d command = "exit\n" print("exit") @@ -88,11 +99,19 @@ def do_command(command, server): if argv[0] == "cd": otarget = target change_target(argv[1]) - tlvdata.send_tlvdata(server, {"operation": 'retrieve', 'path': target}) - result = tlvdata.recv_tlvdata(server) - if 'error' in result: - print target + ':' + result['error'] - target = otarget + for res in send_request('retrieve', target, server): + if 'error' in res: + print target + ': ' + res['error'] + target = otarget + if argv[0] in ('show', 'ls'): + for res in send_request('retrieve', target, server): + if type(res['item']) == dict: + print res['item']["href"] + else: + for item in res['item']: + print item["href"] + + diff --git a/confluent/messages.py b/confluent/messages.py index 4cb633f2..9638e875 100644 --- a/confluent/messages.py +++ b/confluent/messages.py @@ -98,7 +98,7 @@ class LinkRelation(ConfluentMessage): """ return {self.rel: '{ "href": "%s" }' % self.href} - def raw_rel(self): + def raw(self): """Provide python structure of the relation. This currently is only sensible to consume from httpapi. diff --git a/confluent/sockapi.py b/confluent/sockapi.py index 655cf9a3..ee2dab05 100644 --- a/confluent/sockapi.py +++ b/confluent/sockapi.py @@ -61,8 +61,11 @@ def sessionhdl(connection, authname): def send_response(responses, connection): + if responses is None: + return for rsp in responses: - tlvdata.send_tlvdata(connection, json.dumps(rsp.json())) + tlvdata.send_tlvdata(connection, rsp.raw()) + def process_request(connection, request, cfm, authdata): #TODO(jbjohnso): authorize each request @@ -70,17 +73,17 @@ def process_request(connection, request, cfm, authdata): operation = request['operation'] path = request['path'] params = request.get('parameters', None) + hdlr = None try: hdlr = pluginapi.handle_path(path, operation, cfm, params) except exc.NotFoundException: tlvdata.send_tlvdata(connection, {"errorcode": 404, "error": "Target not found"}) - return except exc.InvalidArgumentException: tlvdata.send_tlvdata(connection, {"errorcode": 400, "error": "Bad Request"}) - return send_response(hdlr, connection) + tlvdata.send_tlvdata(connection, {'_requestdone': 1}) return ccons = ClientConsole(connection) consession = consoleserver.ConsoleSession(node='n4', configmanager=cfm,