def __http_call(self, path, method="GET", query=None, data=None): """Make a HTTP call and return the retrieved object.""" if method not in ("GET", "POST", "PUT"): raise ValueError("Unknown method %s" % method) if method == "GET" and data: raise TypeError("GET does not take data") if query is None: query = [] query.extend(self._auth_data) scheme, netloc, basepath, _, _ = urlparse.urlsplit(self._uri) url = urlparse.urlunsplit( (scheme, netloc, urlparse.urljoin(basepath, path), urllib.urlencode(query), None)) req = urllib2.Request(url) req.add_header('Content-Type', 'application/json') req.add_header('Accept', 'application/json') req.get_method = lambda: method if data: req.add_data(json.dumps(data)) try: resp = self.opener.open(req).read() except urllib2.HTTPError as exc: if exc.getcode() == 400: resp = exc.read() else: raise try: return json.loads(resp) except ValueError as exc: raise BugzillaError(None, "Bad response from %s:\n%s" % (url, resp))
def comment_get(self, bug_id, comment_id): result = self.__xmlrpc_call("Bug.comments", {"ids": [bug_id]}) if result.get("bugs"): bug = result["bugs"][bug_id] for comment in bug['comments']: if comment['count'] == comment_id: return comment else: raise BugzillaError(None, "Failed to get bug %s" % bug_id)
def bug_update(self, bug_data): bug_id = bug_data.get("id") if bug_id is None: raise BugzillaError(None, "Bug ID needed for update") params = {"ids": [bug_id]} for key in bug_data: if key in ("id", ): continue params[key] = bug_data[key] self.__xmlrpc_call("Bug.update", params)
def __check_error(self, result): """If call result was an error, raise it.""" if result.get("error"): code = result.get("code") if not code: msg = str(result) else: msg = result.get( "message", "https://wiki.mozilla.org/Bugzilla:WebServices:Errors") raise BugzillaError(code, msg)
def bug_update(self, data): bug_id = data.get("id") if not bug_id: raise BugzillaError(None, "Bug ID required for update") if not data.get("token"): bug = self.bug_get(bug_id) bug.update(data) else: bug = data result = self.__http_call("bug/%s" % bug_id, method="PUT", data=bug) self.__check_error(result)
def __xmlrpc_call(self, name, param): """Helper to make the xml-rpc call and handle faults.""" try: method = getattr(self._rpc, name) return method(param) except xmlrpclib.Fault as fault: code = getattr(fault, "faultCode", None) if code is None: msg = str(fault) else: msg = getattr(fault, "faultString", str(fault)) raise BugzillaError(code, msg)
def comments_get(self, bug_id): result = self.__xmlrpc_call("Bug.comments", {"ids": [bug_id]}) if result.get("bugs"): return result["bugs"][bug_id] else: raise BugzillaError(None, "Failed to get bug %s" % bug_id)