Skip to content

Commit

Permalink
Python Bindings: Services API, logging & XML enhancements
Browse files Browse the repository at this point in the history
- Improve logging dump of 'postdata'
- Add an option to return raw XML data, {'rawxml': True}

Some lxml.etree functions, e.g. fromstring()/tostring() cause pylint 'I'
messages. users may want to add the following to their .pylintrc (or just
add # pylint: disable=c-extension-no-member inline):

    [MESSAGE-CONTROL]
    disable=c-extension-no-member

Closes #13619

Signed-off-by: Bill Meek <billmeek@mythtv.org>
  • Loading branch information
rcrdnalor authored and Bill Meek committed May 21, 2020
1 parent 416170b commit 1a1b698
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions mythtv/bindings/python/MythTV/services_api/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def send(self, endpoint='', postdata=None, rest='', opts=None):
its response in XML rather than JSON. Defaults to
False.
opts['rawxml']: If True, causes the backend to send it's response in
XML as bytes. This can be easily parsed by Python's
'lxml.etree.fromstring()'. Defaults to False.
opts['wrmi']: If True and there is postdata, the URL is then sent to
the server.
Expand Down Expand Up @@ -296,6 +300,9 @@ def send(self, endpoint='', postdata=None, rest='', opts=None):
if self.opts['usexml']:
return response.text

if self.opts['rawxml']:
return response.content

try:
return response.json()
except ValueError as err:
Expand All @@ -320,7 +327,7 @@ def _set_missing_opts(self):
if not isinstance(self.opts, dict):
self.opts = {}

for option in ('noetag', 'nogzip', 'usexml', 'wrmi', 'wsdl'):
for option in ('noetag', 'nogzip', 'usexml', 'rawxml', 'wrmi', 'wsdl'):
try:
self.opts[option]
except (KeyError, TypeError):
Expand Down Expand Up @@ -368,8 +375,8 @@ def _validate_postdata(self):
raise RuntimeError('usage: postdata must be passed as a dict')

self.logger.debug('The following postdata was included:')
for key in self.postdata:
self.logger.debug('%15s: %s', key, self.postdata[key])
for k, v in self.postdata.items():
self.logger.debug('%15s: %s', k, v)

if not self.opts['wrmi']:
raise RuntimeWarning('wrmi=False')
Expand All @@ -396,7 +403,7 @@ def _create_session(self):
else:
self.session.headers.update({'Accept-Encoding': 'gzip,deflate'})

if self.opts['usexml']:
if self.opts['usexml'] or self.opts['rawxml']:
self.session.headers.update({'Accept': ''})
else:
self.session.headers.update({'Accept': 'application/json'})
Expand Down

0 comments on commit 1a1b698

Please sign in to comment.