TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
+class BugzillaError(Exception):
+ pass
+
+class BugzillaInvalidObjectError(BugzillaError):
+ pass
+
class BadRequestError(Exception):
pass
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):
"""