From: Michael Tremer Date: Sat, 20 May 2023 09:48:24 +0000 (+0000) Subject: bugtracker: Send payload for POST requests as JSON X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e6d2928cd0e7a97784a682e1e9dcd5dd17ddef25;p=pbs.git bugtracker: Send payload for POST requests as JSON Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/bugtracker.py b/src/buildservice/bugtracker.py index 4a29a105..4eb47d39 100644 --- a/src/buildservice/bugtracker.py +++ b/src/buildservice/bugtracker.py @@ -36,6 +36,12 @@ log = logging.getLogger("pbs.bugzilla") TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" +class BugzillaError(Exception): + pass + +class BugzillaInvalidObjectError(BugzillaError): + pass + class BadRequestError(Exception): pass @@ -122,25 +128,43 @@ class Bugzilla(base.Object): data |= { "api_key" : self.api_key } # Encode body - body = urllib.parse.urlencode(data) + body = None # For GET requests, append query arguments if method == "GET": - if body: - url = "%s?%s" % (url, body) + if data: + url = "%s?%s" % (url, urllib.parse.urlencode(data)) - body = None + # For POST encode all arguments as JSON + elif method == "POST": + headers |= { + "Content-Type" : "application/json", + } + + body = json.dumps(data) # Send the request and wait for a response res = await self.backend.httpclient.fetch(url, method=method, headers=headers, body=body) # Decode JSON response - if res.body: - return json.loads(res.body) + body = json.loads(res.body) + + # Check for any errors + if "error" in body: + # Fetch code and message + code, message = body.get("code"), body.get("message") + + # 51 - Invalid object + if code == 51: + raise BugzillaInvalidObjectError(message) + + # Handle any so far unhandled errors + else: + raise BugzillaError(message) # Return an empty response - return {} + return body def bug_url(self, id): """