Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for exportFieldNames call #125

Merged
40 changes: 40 additions & 0 deletions redcap/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,46 @@ def export_fem(self, arms=None, format='json', df_kwargs=None):

return self.read_csv(StringIO(response), **df_kwargs)

def export_field_names(self, field=None, format='json', df_kwargs=None):
"""
Export the project's export field names

Parameters
----------
fields : str
Limit exported field name to this field (only single field supported).
When not provided, all fields returned.
format : (``'json'``), ``'csv'``, ``'xml'``, ``'df'``
Return the metadata in native objects, csv or xml.
``'df'`` will return a ``pandas.DataFrame``.
df_kwargs : dict
Passed to ``pandas.read_csv`` to control construction of
returned DataFrame.
by default ``{'index_col': 'original_field_name'}``

Returns
-------
metadata : list, str, ``pandas.DataFrame``
metadata structure for the project.
"""
ret_format = format
if format == 'df':
ret_format = 'csv'

pl = self.__basepl('exportFieldNames', format=ret_format)

if field:
pl['field'] = field

response, _ = self._call_api(pl, 'exp_field_names')

if format in ('json', 'csv', 'xml'):
return response
elif format == 'df':
if not df_kwargs:
df_kwargs = {'index_col': 'original_field_name'}
return self.read_csv(StringIO(response), **df_kwargs)

def export_metadata(self, fields=None, forms=None, format='json',
df_kwargs=None):
"""
Expand Down
2 changes: 2 additions & 0 deletions redcap/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def validate(self):
valid_data = {
'exp_record': (['type', 'format'], 'record',
'Exporting record but content is not record'),
'exp_field_names': (['format'], 'exportFieldNames',
'Exporting field names, but content is not exportFieldNames'),
'del_record': (['format'], 'record',
'Deleting record but content is not record'),
'imp_record': (['type', 'overwriteBehavior', 'data', 'format'],
Expand Down