Skip to content

Commit 2fbd7b5

Browse files
author
Kenneth Chan
committed
add get_status function to get API server status
1 parent ac68e48 commit 2fbd7b5

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/source/predictionio.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ predictionio.Client Class
135135

136136
.. note:: The following is blocking (synchronous) request methods
137137

138+
.. automethod:: get_status
138139
.. automethod:: create_user
139140
.. automethod:: get_user
140141
.. automethod:: delete_user

lib/predictionio/__init__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
2929
Should be handled by the user
3030
"""
31+
class ServerStatusError(PredictionIOAPIError):
32+
"Error happened when tried to get status of the API server"
33+
pass
34+
3135
class UserNotCreatedError(PredictionIOAPIError):
3236
"Error happened when tried to create user"
3337
pass
@@ -119,7 +123,34 @@ def close(self):
119123
It will wait for all pending requests to finish.
120124
"""
121125
self._connection.close()
122-
126+
127+
def get_status(self):
128+
"""Get the status of the PredictionIO API Server
129+
130+
:returns:
131+
status message.
132+
133+
:raises:
134+
ServerStatusError.
135+
"""
136+
path = "/"
137+
request = AsyncRequest("GET", path)
138+
request.set_rfunc(self._aget_status_resp)
139+
self._connection.make_request(request)
140+
result = self.aresp(request)
141+
return result
142+
143+
def _aget_status_resp(self, response):
144+
if response.error is not None:
145+
raise ServerStatusError("Exception happened: %s for request %s" % \
146+
(response.error, response.request))
147+
elif response.status != httplib.OK:
148+
raise ServerStatusError("request: %s status: %s body: %s" % \
149+
(response.request, response.status, response.body))
150+
151+
#data = json.loads(response.body) # convert json string to dict
152+
return response.body
153+
123154
def acreate_user(self, uid, **params):
124155
"""Asynchronously create a user.
125156

tests/predictionio_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def setUp(self):
1919
def tearDown(self):
2020
pass
2121

22+
def test_status(self):
23+
client = predictionio.Client(APP_KEY, 1, apiurl=API_URL)
24+
status = client.get_status()
25+
self.assertEqual(status, "PredictionIO Output API is online.")
26+
client.close()
27+
2228
def test_user(self):
2329
client = predictionio.Client(APP_KEY, 1, apiurl=API_URL)
2430

0 commit comments

Comments
 (0)