Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#13619 closed Patch - Feature (fixed)

Python services-api enhancements

Reported by: rcrdnalor Owned by: Bill Meek
Priority: minor Milestone: 31.1
Component: Bindings - Python Version: Master Head
Severity: medium Keywords: services-api
Cc: Ticket locked: no

Description (last modified by Bill Meek)

1) Python's lxml module works best if fed with raw data from a 'Request.session.get' response.

A new option provided by this patch allows returning data as raw data i.e.: returning the recieved 'response.content'. This can be easily handled by the lxml.etree.fromstring() method.

An example how to use this feature:

$ python3
>>> from MythTV.services_api import send as API
>>> from lxml import etree
>>> host = "localhost"
>>> mythrequest = API.Send(host)
>>> endpoint = "/Dvr/GetRecordedList"
>>> restdata = "Descending=True&Count=3"
>>> opts     = {'rawxml': True}                ### < --- new option
>>> response = mythrequest.send(endpoint=endpoint, rest=restdata, opts=opts)
>>> eroot = etree.fromstring(response)
>>> print(etree.tostring(eroot, pretty_print=True, encoding='unicode')) 

Verified also with python2.

Attached a patch adding this additional option.

2) The mythservices-api requires the 'post' data to be passed as dictionary. For classes derived from dictionary (dict), it is not mandatory to provide an attribute 'keys', but the methods 'keys()', 'values()', 'items()' must always be implemented.

Using the latter methods, allows logging of a 'post' operation for those classes derived from 'dict'.

Otherwise, on get a traceback showing an 'AttributeError' or a 'KeyError'. The class 'OrdDict' from the python bindings is such an example.

Attached a patch that improves logging for 'post' operations.

Attachments (2)

01_improve_logging_of_post_operations.patch (733 bytes) - added by rcrdnalor 4 months ago.
Improve logging of 'post' operations
02_add_option_to_return_raw_content.patch (1.7 KB) - added by rcrdnalor 4 months ago.
Add option to return raw data

Download all attachments as: .zip

Change History (5)

Changed 4 months ago by rcrdnalor

Improve logging of 'post' operations

Changed 4 months ago by rcrdnalor

Add option to return raw data

comment:1 Changed 4 months ago by Bill Meek

Description: modified (diff)
Milestone: needs_triage31.1

comment:2 Changed 4 months ago by Roland Ernst <rcrernst@…>

Resolution: fixed
Status: newclosed

In 1ca7a4b09/mythtv:

Python Bindings: Services API, logging & XML enhancements

  • 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@…>
(cherry picked from commit 1a1b69836515a5ec77b09c4559a3bb729af9cd7b)

comment:3 Changed 4 months ago by Roland Ernst <rcrernst@…>

In 1ca7a4b09/mythtv:

Python Bindings: Services API, logging & XML enhancements

  • 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@…>
(cherry picked from commit 1a1b69836515a5ec77b09c4559a3bb729af9cd7b)

Note: See TracTickets for help on using tickets.