add get_status function to get API server status
diff --git a/docs/source/predictionio.rst b/docs/source/predictionio.rst
index 3ca88e3..79a1b70 100644
--- a/docs/source/predictionio.rst
+++ b/docs/source/predictionio.rst
@@ -135,6 +135,7 @@
.. note:: The following is blocking (synchronous) request methods
+ .. automethod:: get_status
.. automethod:: create_user
.. automethod:: get_user
.. automethod:: delete_user
diff --git a/lib/predictionio/__init__.py b/lib/predictionio/__init__.py
index 6e3a277..345d0d3 100644
--- a/lib/predictionio/__init__.py
+++ b/lib/predictionio/__init__.py
@@ -28,6 +28,10 @@
Should be handled by the user
"""
+class ServerStatusError(PredictionIOAPIError):
+ "Error happened when tried to get status of the API server"
+ pass
+
class UserNotCreatedError(PredictionIOAPIError):
"Error happened when tried to create user"
pass
@@ -119,7 +123,34 @@
It will wait for all pending requests to finish.
"""
self._connection.close()
-
+
+ def get_status(self):
+ """Get the status of the PredictionIO API Server
+
+ :returns:
+ status message.
+
+ :raises:
+ ServerStatusError.
+ """
+ path = "/"
+ request = AsyncRequest("GET", path)
+ request.set_rfunc(self._aget_status_resp)
+ self._connection.make_request(request)
+ result = self.aresp(request)
+ return result
+
+ def _aget_status_resp(self, response):
+ if response.error is not None:
+ raise ServerStatusError("Exception happened: %s for request %s" % \
+ (response.error, response.request))
+ elif response.status != httplib.OK:
+ raise ServerStatusError("request: %s status: %s body: %s" % \
+ (response.request, response.status, response.body))
+
+ #data = json.loads(response.body) # convert json string to dict
+ return response.body
+
def acreate_user(self, uid, **params):
"""Asynchronously create a user.
diff --git a/tests/predictionio_test.py b/tests/predictionio_test.py
index 872bdd1..6aa99e7 100644
--- a/tests/predictionio_test.py
+++ b/tests/predictionio_test.py
@@ -19,6 +19,12 @@
def tearDown(self):
pass
+ def test_status(self):
+ client = predictionio.Client(APP_KEY, 1, apiurl=API_URL)
+ status = client.get_status()
+ self.assertEqual(status, "PredictionIO Output API is online.")
+ client.close()
+
def test_user(self):
client = predictionio.Client(APP_KEY, 1, apiurl=API_URL)