diff --git a/hipchat/connection.py b/hipchat/connection.py index da7d571..7f93a0e 100644 --- a/hipchat/connection.py +++ b/hipchat/connection.py @@ -30,7 +30,27 @@ def call_hipchat(cls, ReturnType, url, data=True, **kw): req.add_data(urlencode(kw.items())) if hipchat.config.proxy_server and hipchat.config.proxy_type: req.set_proxy(hipchat.config.proxy_server, hipchat.config.proxy_type) - return ReturnType(json.load(urlopen(req))) + try: + res = urlopen(req) + except HTTPError, e: + resp = "".join(e.readlines()) + try: + err_resp = json.loads(resp) + except ValueError: + raise Exception( + "unknown error: %d response was: %s" % ( + e.getcode(), resp + ), + ) + error = err_resp.get("error", {}) + raise Exception( + "%d %s error: %s" % ( + error.get("code", -1), + error.get("type", "unknown"), + error.get("message", "no message"), + ) + ) + return ReturnType(json.load(res)) class HipChatObject(object): diff --git a/hipchat/room.py b/hipchat/room.py index fd7fa89..4113a27 100644 --- a/hipchat/room.py +++ b/hipchat/room.py @@ -25,5 +25,13 @@ def __init__(self, jsono): ReturnType=lambda x: map(Room, map(lambda y: {'room': y}, x['rooms'])), url="https://api.hipchat.com/v1/rooms/list", data=False)) + +Room.create = classmethod( + partial(call_hipchat, Room, url="https://api.hipchat.com/v1/rooms/create", data=True) +) +Room.topic = classmethod( + partial(call_hipchat, ReturnType=lambda x: x['status'], url="https://api.hipchat.com/v1/rooms/topic", data=True) +) + Room.message = classmethod(partial(call_hipchat, ReturnType=MessageSentStatus, url="https://api.hipchat.com/v1/rooms/message", data=True)) Room.show = classmethod(partial(call_hipchat, Room, url="https://api.hipchat.com/v1/rooms/show", data=False))