summaryrefslogtreecommitdiffstats
path: root/pyload/remote/JSONClient.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyload/remote/JSONClient.py')
-rw-r--r--pyload/remote/JSONClient.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/pyload/remote/JSONClient.py b/pyload/remote/JSONClient.py
new file mode 100644
index 000000000..a2c07a132
--- /dev/null
+++ b/pyload/remote/JSONClient.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from urllib import urlopen, urlencode
+from httplib import UNAUTHORIZED, FORBIDDEN
+
+from json_converter import loads, dumps
+from apitypes import Unauthorized, Forbidden
+
+class JSONClient:
+ URL = "http://localhost:8001/api"
+
+ def __init__(self, url=None):
+ self.url = url or self.URL
+ self.session = None
+
+ def request(self, path, data):
+ ret = urlopen(self.url + path, urlencode(data))
+ if ret.code == 400:
+ raise loads(ret.read())
+ if ret.code == 404:
+ raise AttributeError("Unknown Method")
+ if ret.code == 500:
+ raise Exception("Remote Exception")
+ if ret.code == UNAUTHORIZED:
+ raise Unauthorized()
+ if ret.code == FORBIDDEN:
+ raise Forbidden()
+ return ret.read()
+
+ def login(self, username, password):
+ self.session = loads(self.request("/login", {'username': username, 'password': password}))
+ return self.session
+
+ def logout(self):
+ self.call("logout")
+ self.session = None
+
+ def call(self, func, *args, **kwargs):
+ # Add the current session
+ kwargs["session"] = self.session
+ path = "/" + func + "/" + "/".join(dumps(x) for x in args)
+ data = dict((k, dumps(v)) for k, v in kwargs.iteritems())
+ rep = self.request(path, data)
+ return loads(rep)
+
+ def __getattr__(self, item):
+ def call(*args, **kwargs):
+ return self.call(item, *args, **kwargs)
+
+ return call
+
+if __name__ == "__main__":
+ api = JSONClient()
+ api.login("User", "test")
+ print api.getServerVersion() \ No newline at end of file