Skip to content

Commit 266bfcd

Browse files
authored
Merge pull request #28 from HichamDz38/master
hande case when the response is json instead of xml
2 parents 39f1a51 + c478644 commit 266bfcd

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

bigbluebutton_api_python/bigbluebutton.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .util import UrlBuilder
1818
from .parameters import BBBModule
1919
import sys
20+
import json
2021
from jxmlease import parse
2122
from hashlib import sha1
2223
if sys.version_info[0] == 2:
@@ -127,23 +128,33 @@ def set_config_xml(self, meeting_id, xml):
127128
"meetingID": meeting_id})
128129
return SetConfigXMLResponse(response)
129130

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+
130144
def __send_api_request(self, api_call, params={}, data=None):
131145
url = self.__urlBuilder.buildUrl(api_call, params)
132146

133147
# if data is none, then we send a GET request, if not, then we send a POST request
134148
if data is None:
135-
response = urlopen(url, timeout=10).read()
149+
response = urlopen(url, timeout=10)
136150
else:
137151
if isinstance(data, str):
138152
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)
140154
else:
141-
response = urlopen(url, timeout=10, data=urlencode(data).encode()).read()
155+
response = urlopen(url, timeout=10, data=urlencode(data).encode())
142156

143-
try:
144-
rawXml = parse(response)["response"]
145-
except Exception as e:
146-
raise BBBException("XMLSyntaxError", e.message)
157+
rawXml = self._parse_response(response)
147158
# get default config xml and get attendance requests will simply return the xml file without
148159
# returncode, so it will cause an error when try to check the return code
149160
if api_call not in (ApiMethod.GET_DEFAULT_CONFIG_XML, ApiMethod.GET_ATTENDANCE):

0 commit comments

Comments
 (0)