Skip to content

Add remaining Survey API methods #298

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

Merged
merged 8 commits into from
May 2, 2025
171 changes: 151 additions & 20 deletions redcap/methods/surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,186 @@ class Surveys(Base):

def export_survey_link(
self,
instrument: str,
record: str,
instrument: str,
event: Optional[str] = None,
repeat_instance: int = 1,
):
) -> str:
"""
Export one survey link

Note:
The passed instrument must be set up as a survey instrument.

Args:
record:
Name of the record
instrument:
Name of instrument as seen in the Data Dictionary (metadata).
event:
Unique event name, only used in longitudinal projects
repeat_instance:
only for projects with repeating instruments/events)
The repeat instance number of the repeating event (if longitudinal)
or the repeating instrument (if classic or longitudinal).
Default value is '1'.

Returns:
URL of survey link requested

Examples:
>>> proj.export_survey_link(record="1", instrument="form_1", event="event_1_arm_1")
'https://redcapdemo.vumc.org/surveys/?s=...'
"""
payload = self._initialize_payload(
content="surveyLink",
# Hard-coded due to the nature of the response
return_format_type="csv",
)

payload["record"] = record
payload["instrument"] = instrument
payload["repeat_instance"] = repeat_instance

if event:
payload["event"] = event

return cast(str, self._call_api(payload, return_type="str"))

def export_survey_queue_link(
self,
record: str,
) -> str:
"""
Export one survey queue link

Note:
The passed instrument must be set up as a survey instrument. The
survey queue must be enabled for the project.

Args:
record:
Name of the record

Returns:
URL of survey queue link requested

Examples:
>>> proj.export_survey_queue_link(record="1")
'https://redcapdemo.vumc.org/surveys/?sq=...'
"""
payload = self._initialize_payload(
content="surveyQueueLink",
# Hard-coded due to the nature of the response
return_format_type="csv",
)

payload["record"] = record

return cast(str, self._call_api(payload, return_type="str"))

def export_survey_access_code(
self,
record: str,
instrument: str,
event: Optional[str] = None,
repeat_instance: int = 1,
) -> str:
# pylint: disable=line-too-long
"""
Export a Survey Access Code for a Participant

Note:
The passed instrument must be set up as a survey instrument.

Args:
record:
Name of the record
instrument:
Name of instrument as seen in the Data Dictionary (metadata).
event:
Unique event name, only used in longitudinal projects
repeat_instance:
only for projects with repeating instruments/events)
The repeat instance number of the repeating event (if longitudinal)
or the repeating instrument (if classic or longitudinal).
only for projects with repeating instruments/events)
The repeat instance number of the repeating event (if longitudinal)
or the repeating instrument (if classic or longitudinal).
Default value is '1'.

Returns:
Str:
URL of survey link requested
A survey access code for a specified record and data collection
instrument

Examples:
>>> proj.export_survey_link(instrument="form_1", record="5", event="event_1_arm_1")
https://redcap.mytld.com/surveys/?s=6B2zFAEWPVSrXnnx
"""
>>> proj.export_survey_access_code(record="1", instrument="form_1", event="event_1_arm_1")
'...'
"""
# pylint: enable=line-too-long
payload = self._initialize_payload(
content="surveyLink"
content="surveyAccessCode",
# Hard-coded due to the nature of the response
return_format_type="csv",
)
payload["instrument"] = instrument

payload["record"] = record
payload["instrument"] = instrument
payload["repeat_instance"] = repeat_instance

if event:
payload["event"] = event
payload["repeat_instance"] = repeat_instance

return_type = "str"
response = cast(Union[Json, str], self._call_api(payload, return_type))
return cast(str, self._call_api(payload, return_type="str"))

return self._return_data(
response=response,
content="surveyLink",
format_type=return_type,
def export_survey_return_code(
self,
record: str,
instrument: str,
event: Optional[str] = None,
repeat_instance: int = 1,
) -> str:
# pylint: disable=line-too-long
"""
Export a Survey Return Code for a Participant

Note:
The passed instrument must be set up as a survey instrument, which has return codes enabled.

Args:
record:
Name of the record
instrument:
Name of instrument as seen in the Data Dictionary (metadata).
event:
Unique event name, only used in longitudinal projects
repeat_instance:
only for projects with repeating instruments/events)
The repeat instance number of the repeating event (if longitudinal)
or the repeating instrument (if classic or longitudinal).
Default value is '1'.

Returns:
A survey return code for a specified record and data collection
instrument

Examples:
>>> proj.export_survey_return_code(record="1", instrument="form_1", event="event_1_arm_1")
'...'
"""
# pylint: enable=line-too-long
payload = self._initialize_payload(
content="surveyReturnCode",
# Hard-coded due to the nature of the response
return_format_type="csv",
)

payload["record"] = record
payload["instrument"] = instrument
payload["repeat_instance"] = repeat_instance

if event:
payload["event"] = event

return cast(str, self._call_api(payload, return_type="str"))

def export_survey_participant_list(
self,
instrument: str,
Expand Down Expand Up @@ -103,7 +233,8 @@ def export_survey_participant_list(
'survey_access_code': ...}]
"""
payload = self._initialize_payload(
content="participantList", format_type=format_type
content="participantList",
format_type=format_type,
)
payload["instrument"] = instrument
if event:
Expand Down
Loading