Permalink
Browse files

added methods for staus information, also tests for that

  • Loading branch information...
1 parent 4ed685c commit 028cc36495e25d607048c8d246a72d5c2ea15860 @domoritz domoritz committed Aug 7, 2012
Showing with 53 additions and 0 deletions.
  1. +1 −0 config_example.py
  2. +32 −0 popit.py
  3. +20 −0 test.py
View
1 config_example.py
@@ -1,5 +1,6 @@
instance = 'people'
hostname = 'popit.mysociety.org'
port = 3000
+api_version = 'v1'
user = 'test@test.co.uk'
password = 'password'
View
32 popit.py
@@ -5,6 +5,7 @@
import logging
from pprint import pprint
from requests.exceptions import *
+from slumber.exceptions import *
FORMAT = "[PopIt | %(levelname)s] %(message)s"
log = logging.getLogger(__name__)
@@ -19,6 +20,11 @@ def __init__(self, value):
def __str__(self):
return repr(self.value)
+class NotInitializedError(RuntimeError):
+ def __str__(self):
+ return "The PopIt api wrapper is not yet initialized. Check if PopIt is running and then retry."
+
+
class PopIt(object):
def __init__(self, lazy = False, **args):
""" Lazy means that you have to call PopIt(true) AND then set_up()
@@ -50,10 +56,36 @@ def __str__(self):
else:
return str(self)
+ def get_url(self):
+ if self.initialized:
+ return self.__url()
+ else:
+ raise NotInitializedError()
+
+ def get_api_version(self):
+ if self.initialized:
+ return self.api_version
+ else:
+ raise NotInitializedError()
+
+ def is_online(self):
+ if self.initialized:
+ try:
+ # protocol ping
+ self.api.get()
+ except ConnectionError, e:
+ return False
+ else:
+ return True
+ else:
+ raise NotInitializedError()
+
def getGenericApi(self):
return self.api
def __getattr__(self, key):
+ if not self.initialized:
+ raise NotInitializedError()
if key in self.schemas:
return self.api.__call__(key)
else:
View
20 test.py
@@ -40,6 +40,26 @@ def f():
self.p.sadfhoi83jhk3323
ok (f).raises(SchemaError)
+class StatusTest(object):
+ @classmethod
+ def before_all(cls):
+ cls.p = PopIt(**conf)
+
+ def before(self):
+ self.p = self.__class__.p
+
+ @test("is_online should return true")
+ def _(self):
+ ok(self.p.is_online()) == True
+
+ @test("api_version should return right version")
+ def _(self):
+ ok(self.p.get_api_version()) == conf['api_version']
+
+ @test("get_url should return a string")
+ def _(self):
+ ok(self.p.get_url()).is_a(str)
+
class LazyTest(object):
def before(self):
self.p = PopIt(lazy=True)

0 comments on commit 028cc36

Please sign in to comment.