-
Notifications
You must be signed in to change notification settings - Fork 0
Description
PyCap does not directly provide two queries (available in API playground) that we needed.
These are easily patched into PyCap's base classes (which lets us preserve all of PyCap's response handling) by changing the payload (which is a dictionary created with self._initialize_payload() and optionally expanded by adding keys), as done for:
- Export Project XML:
datalad-redcap/datalad_redcap/export_project_xml.py
Lines 41 to 72 in 6057002
def export_project_xml( self, metadata_only: bool = False, files: bool = False, survey_fields: bool = False, dags: bool = False, ): """Export Project XML This function is a patch for PyCap ProjectInfo class """ format_type = "xml" payload = self._initialize_payload( content="project_xml", format_type=format_type, ) payload["returnMetadataOnly"] = metadata_only payload["exportFiles"] = files payload["exportSurveyFields"] = survey_fields payload["exportDataAccessGroups"] = dags return_type = self._lookup_return_type(format_type, request_type="export") response = self._call_api(payload, return_type) return self._return_data( response=response, content="instrument", format_type=format_type, df_kwargs=None, ) - Query:
datalad-redcap/datalad_redcap/query.py
Lines 27 to 54 in f62d02e
class MyInstruments(Instruments): """An extension of PyCap's Instruments class Contains an additional method to export instruments names and labels """ def export_instruments(self): """Export instruments names and labels PyCap's Instruments class has a field_names property, but lacks a method to return matching labels (human-readable). This method does that in a simplified way (only supports json format, does not support arms). """ format_type = "json" # constant, for now payload = self._initialize_payload( content="instrument", format_type=format_type, ) return_type = self._lookup_return_type(format_type, request_type="export") response = self._call_api(payload, return_type) return self._return_data( response=response, content="instrument", format_type=format_type, df_kwargs=None, )
(side note - I use monkey-patching for one and subclassing for another, would be nice to make it consistent)
I should probably open an issue in PyCap asking if they would consider a PR - needs a little bit on top of what was done here.