From 69f2cb38eabace694cc821c98b88930ce845941b Mon Sep 17 00:00:00 2001 From: Sam Vilain Date: Tue, 27 Aug 2013 15:25:16 -0700 Subject: [PATCH 1/2] Raise remote errors better If the remote raises an error like 400 Bad Request, the error is generally lost, and all you see is "HTTPError()". Interpret the error document in the response and raise a better exception in lieu of more formal exception decoding. --- hipchat/connection.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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): From 7dfe1a95f805725b536d62c54dfd264462d8dc30 Mon Sep 17 00:00:00 2001 From: Sam Vilain Date: Tue, 27 Aug 2013 15:25:42 -0700 Subject: [PATCH 2/2] Implement room.create and room.topic A couple of API functions were missed; add hooks to create binding functions. --- hipchat/room.py | 8 ++++++++ 1 file changed, 8 insertions(+) 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))