|
17 | 17 | from .util import UrlBuilder
|
18 | 18 | from .parameters import BBBModule
|
19 | 19 | import sys
|
| 20 | +import json |
20 | 21 | from jxmlease import parse
|
21 | 22 | from hashlib import sha1
|
22 | 23 | if sys.version_info[0] == 2:
|
@@ -127,23 +128,33 @@ def set_config_xml(self, meeting_id, xml):
|
127 | 128 | "meetingID": meeting_id})
|
128 | 129 | return SetConfigXMLResponse(response)
|
129 | 130 |
|
| 131 | + def _parse_response(self, response): |
| 132 | + # parse response to the correct form |
| 133 | + content_type = response.getheader('Content-Type') |
| 134 | + response = response.read() |
| 135 | + match content_type: |
| 136 | + case "application/json": |
| 137 | + return json.loads(response)["response"] |
| 138 | + case _: |
| 139 | + try: |
| 140 | + return parse(response)["response"] |
| 141 | + except Exception as e: |
| 142 | + raise BBBException("XMLSyntaxError", e) |
| 143 | + |
130 | 144 | def __send_api_request(self, api_call, params={}, data=None):
|
131 | 145 | url = self.__urlBuilder.buildUrl(api_call, params)
|
132 | 146 |
|
133 | 147 | # if data is none, then we send a GET request, if not, then we send a POST request
|
134 | 148 | if data is None:
|
135 |
| - response = urlopen(url, timeout=10).read() |
| 149 | + response = urlopen(url, timeout=10) |
136 | 150 | else:
|
137 | 151 | if isinstance(data, str):
|
138 | 152 | request = Request(url, data=bytes(data, "utf8"), headers={'Content-Type': 'application/xml'})
|
139 |
| - response = urlopen(request, timeout=10).read() |
| 153 | + response = urlopen(request, timeout=10) |
140 | 154 | else:
|
141 |
| - response = urlopen(url, timeout=10, data=urlencode(data).encode()).read() |
| 155 | + response = urlopen(url, timeout=10, data=urlencode(data).encode()) |
142 | 156 |
|
143 |
| - try: |
144 |
| - rawXml = parse(response)["response"] |
145 |
| - except Exception as e: |
146 |
| - raise BBBException("XMLSyntaxError", e.message) |
| 157 | + rawXml = self._parse_response(response) |
147 | 158 | # get default config xml and get attendance requests will simply return the xml file without
|
148 | 159 | # returncode, so it will cause an error when try to check the return code
|
149 | 160 | if api_call not in (ApiMethod.GET_DEFAULT_CONFIG_XML, ApiMethod.GET_ATTENDANCE):
|
|
0 commit comments