]> git.ipfire.org Git - pbs.git/commitdiff
bugtracker: Send payload for POST requests as JSON
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 20 May 2023 09:48:24 +0000 (09:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 20 May 2023 09:48:24 +0000 (09:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/bugtracker.py

index 4a29a105180d5fce740a3c86d3ebf84eeb4511dc..4eb47d3951cf375efe1e7bf18e5e5eda35e3e96e 100644 (file)
@@ -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):
                """