Skip to content

Commit

Permalink
get responseids from dist string (#81)
Browse files Browse the repository at this point in the history
* get responseids from dist string

* fix logs
  • Loading branch information
dzaslavskiy authored Jan 24, 2024
1 parent fdc797b commit aa992b3
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
10 changes: 10 additions & 0 deletions qualtrix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ async def session(request: SessionModel):
return client.delete_session(request.surveyId, request.sessionId)
except error.QualtricsError as e:
raise HTTPException(status_code=400, detail=e.args)


@router.get("/contact/{contactId}/responseIds")
async def dist(contactId: str):
return client.get_responseIds_by_contact(contactId)


@router.get("/dist/{distId}/responseIds")
async def dist(distId: str):
return client.get_responseIds_by_dist(distId)
99 changes: 99 additions & 0 deletions qualtrix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,112 @@ def get_response(survey_id: str, response_id: str):
return survey_answers


def get_contact(contact_id: str):
logging.info(f"get_contact {contact_id}")

r = requests.get(
settings.BASE_URL
+ f"/directories/{settings.DIRECTORY_ID}/contacts/{contact_id}",
headers=auth_header,
timeout=settings.TIMEOUT,
)

logging.info(f"get_contact {contact_id} {r.status_code}")
logging.debug(f"get_contact {contact_id} {r.text}")

return r.json()


def get_contact_history(contact_id: str):
logging.info(f"get_contact_history {contact_id}")

r = requests.get(
settings.BASE_URL
+ f"/directories/{settings.DIRECTORY_ID}/contacts/{contact_id}/history",
headers=auth_header,
timeout=settings.TIMEOUT,
params={"type": "response"},
)

logging.info(f"get_contact_history {contact_id} {r.status_code}")
logging.debug(f"get_contact_history {contact_id} {r.text}")

return r.json()


def get_distribution_history(distributionId: str):
logging.info(f"get_distribution_history {distributionId}")

url = settings.BASE_URL + f"/distributions/{distributionId}/history"
r = requests.get(url, headers=auth_header, timeout=settings.TIMEOUT)

logging.info(f"get_distribution_history {distributionId} {r.status_code}")
logging.debug(f"get_distribution_history {distributionId} {r.text}")

data = r.json()

return data


def get_responseIds_by_dist(dist_string: str):
"""
returns a list of responseIds starting from a dist string. A dist string has three parts.
The first part is a distribution id that can be used to get the contactId.
"""

logging.info(f"get_responseIds_by_dist {dist_string}")

dist_parts = dist_string.split("_")
distributionId = "EMD_" + dist_parts[0]

data = get_distribution_history(distributionId)
contactId = data["result"]["elements"][0]["contactId"]

return get_responseIds_by_contact(contactId)


def get_responseIds_by_contact(contactId: str):
"""
get list of responeIds from contact history
"""

logging.info(f"get_responseIds_by_contact {contactId}")

contacthist = get_contact_history(contactId)
dist_Id_list = list(
filter(
lambda y: y != None,
map(lambda x: x["distributionId"], contacthist["result"]["elements"]),
)
)

response_ids = []

for id in dist_Id_list:
data = get_distribution_history(id)

response_ids.extend(
map(
lambda x: "R_" + x["surveySessionId"].split("_")[1],
data["result"]["elements"],
)
)

return response_ids


def get_survey_schema(survey_id: str):
logging.info(f"get_survey_schema {survey_id}")

r = requests.get(
settings.BASE_URL + f"/surveys/{survey_id}/response-schema",
headers=auth_header,
timeout=settings.TIMEOUT,
)

logging.info(f"get_survey_schema {survey_id} {r.status_code}")
logging.debug(f"get_survey_schema {survey_id} {r.text}")

return r.json()


Expand Down

0 comments on commit aa992b3

Please sign in to comment.