Skip to content

Commit 7fcdd2f

Browse files
committed
Add stub _BackendREST
This only implements 'version' and 'extensions' APIs for now. Nothing wires it up yet Signed-off-by: Cole Robinson <[email protected]>
1 parent 6c734d1 commit 7fcdd2f

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

bugzilla/_backendrest.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# This work is licensed under the GNU GPLv2 or later.
2+
# See the COPYING file in the top-level directory.
3+
4+
import json
5+
import logging
6+
import os
7+
8+
from ._backendbase import _BackendBase
9+
from .exceptions import BugzillaError
10+
11+
12+
log = logging.getLogger(__name__)
13+
14+
15+
# XXX remove this pylint disable
16+
# pylint: disable=abstract-method
17+
18+
19+
class _BackendREST(_BackendBase):
20+
"""
21+
Internal interface for direct calls to bugzilla's REST API
22+
"""
23+
def __init__(self, url, bugzillasession):
24+
_BackendBase.__init__(self, url, bugzillasession)
25+
self._bugzillasession.set_content_type("application/json")
26+
27+
28+
#########################
29+
# Internal REST helpers #
30+
#########################
31+
32+
def _handle_response(self, response):
33+
response.raise_for_status()
34+
text = response.text.encode("utf-8")
35+
36+
try:
37+
ret = dict(json.loads(text))
38+
except Exception:
39+
log.debug("Failed to parse REST response. Output is:\n%s", text)
40+
raise
41+
42+
if ret.get("error", False):
43+
raise BugzillaError(ret["message"], code=ret["code"])
44+
return ret
45+
46+
def _op(self, optype, apiurl, paramdict=None):
47+
fullurl = os.path.join(self._url, apiurl.lstrip("/"))
48+
log.debug("Bugzilla REST %s %s params=%s", optype, fullurl, paramdict)
49+
session = self._bugzillasession.get_requests_session()
50+
data = json.dumps(paramdict or {})
51+
52+
if optype == "POST":
53+
response = session.post(fullurl, data=data)
54+
elif optype == "PUT":
55+
response = session.put(fullurl, data=data)
56+
else:
57+
response = session.get(fullurl, params=paramdict)
58+
59+
return self._handle_response(response)
60+
61+
def _get(self, *args, **kwargs):
62+
return self._op("GET", *args, **kwargs)
63+
def _put(self, *args, **kwargs):
64+
return self._op("PUT", *args, **kwargs)
65+
def _post(self, *args, **kwargs):
66+
return self._op("POST", *args, **kwargs)
67+
68+
69+
#######################
70+
# API implementations #
71+
#######################
72+
73+
def get_xmlrpc_proxy(self):
74+
raise BugzillaError("You are using the bugzilla REST API, "
75+
"so raw XMLRPC access is not provided.")
76+
def is_rest(self):
77+
return True
78+
79+
def bugzilla_version(self):
80+
return self._get("/version")
81+
def bugzilla_extensions(self):
82+
return self._get("/extensions")

0 commit comments

Comments
 (0)