summaryrefslogtreecommitdiffstats
path: root/module/remote/WSClient.py
diff options
context:
space:
mode:
authorGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2012-10-13 22:27:10 +0200
committerGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2012-10-13 22:27:10 +0200
commit46173e352cf3c55ecf9f5892ff83d78c1996da77 (patch)
tree502236eecfbdad2fee6b83f5147135c4b76ba21f /module/remote/WSClient.py
parentadded JSON and WS client, re organized tests, new classes for easier api tests (diff)
downloadpyload-46173e352cf3c55ecf9f5892ff83d78c1996da77.tar.xz
added exceptions for clients, created test for WS client
Diffstat (limited to 'module/remote/WSClient.py')
-rw-r--r--module/remote/WSClient.py32
1 files changed, 24 insertions, 8 deletions
diff --git a/module/remote/WSClient.py b/module/remote/WSClient.py
index c06bab661..23b5fc3ca 100644
--- a/module/remote/WSClient.py
+++ b/module/remote/WSClient.py
@@ -1,8 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-from json_converter import loads, dumps
from websocket import create_connection
+from httplib import UNAUTHORIZED, FORBIDDEN
+
+from json_converter import loads, dumps
+from ttypes import Unauthorized, Forbidden
class WSClient:
URL = "ws://localhost:7227/api"
@@ -11,21 +14,34 @@ class WSClient:
self.url = url or self.URL
self.ws = None
- def login(self, username, password):
+ def connect(self):
self.ws = create_connection(self.URL)
- return self.call("login", username, password)
- def logout(self):
- self.call("logout")
+ def close(self):
self.ws.close()
+ def login(self, username, password):
+ if not self.ws: self.connect()
+ return self.call("login", username, password)
+
def call(self, func, *args, **kwargs):
- self.ws.send(dumps([func, args, kwargs]))
+ if not self.ws:
+ raise Exception("Not Connected")
+
+ if kwargs:
+ self.ws.send(dumps([func, args, kwargs]))
+ else: # omit kwargs
+ self.ws.send(dumps([func, args]))
+
code, result = loads(self.ws.recv())
if code == 404:
raise AttributeError("Unknown Method")
- elif code == 505:
- raise Exception("Remote Exception")
+ elif code == 500:
+ raise Exception("Remote Exception: %s" % result)
+ elif code == UNAUTHORIZED:
+ raise Unauthorized()
+ elif code == FORBIDDEN:
+ raise Forbidden()
return result