Skip to content

Commit

Permalink
CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-moore-97 committed Jan 5, 2024
1 parent 48611af commit 0446d55
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
10 changes: 4 additions & 6 deletions qualtrix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ async def get_response(request: ResponseModel):
@router.post("/redirect")
async def get_redirect(request: RedirectModel):
try:
return client.get_redirect(
request.surveyId,
request.targetSurveyId,
request.directoryId,
request.responseId,
)
email = client.get_email(request.surveyId, request.responseId)
contact = client.get_contact(request.directoryId, email)
distribution = client.get_distribution(request.directoryId, contact["id"])
return client.get_link(request.targetSurveyId, distribution["distributionId"])
except error.QualtricsError as e:
raise HTTPException(status_code=400, detail=e.args)

Expand Down
47 changes: 33 additions & 14 deletions qualtrix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __eq__(self, other):
return self.value == other


def get_redirect(survey_id, target_survey_id, directory_id, response_id):
def get_email(survey_id: str, response_id: str):
header = copy.deepcopy(auth_header)
header["Accept"] = "application/json"

Expand All @@ -80,36 +80,48 @@ def get_redirect(survey_id, target_survey_id, directory_id, response_id):
"Email could not be found, and redirect link could not be generated"
)

return email


def get_contact(directory_id: str, email: str):
header = copy.deepcopy(auth_header)
header["Accept"] = "application/json"

# Email -> Contact ID
email_to_contact_id_payload = {
email_to_contact_payload = {
"filter": {"filterType": "email", "comparison": "eq", "value": email}
}

r = requests.post(
settings.BASE_URL + f"/directories/{directory_id}/contacts/search",
headers=auth_header,
headers=header,
params={"includeEmbedded": "true"},
json=email_to_contact_id_payload,
json=email_to_contact_payload,
timeout=settings.TIMEOUT,
)

email_to_contact_id_resp = r.json()
email_to_contact_resp = r.json()

if "error" in email_to_contact_id_resp["meta"]:
raise error.QualtricsError(email_to_contact_id_resp["meta"]["error"])
if "error" in email_to_contact_resp["meta"]:
raise error.QualtricsError(email_to_contact_resp["meta"]["error"])

contact_id = next(
iter(x for x in email_to_contact_id_resp["result"]["elements"]), None
)
if contact_id is None:
contact = next(iter(x for x in email_to_contact_resp["result"]["elements"]), None)
if contact is None:
raise error.QualtricsError(
"Contact ID could not be found, and redirect link could not be generated"
)

return contact


def get_distribution(directory_id: str, contact_id: str):
header = copy.deepcopy(auth_header)
header["Accept"] = "application/json"

# Contact ID -> Distribution ID https://api.qualtrics.com/f30cf65c90b7a-get-directory-contact-history
r = requests.get(
settings.BASE_URL
+ f"/directories/{directory_id}/contacts/{contact_id['id']}/history",
+ f"/directories/{directory_id}/contacts/{contact_id}/history",
headers=header,
params={"type": "email"},
timeout=settings.TIMEOUT,
Expand All @@ -136,9 +148,16 @@ def get_redirect(survey_id, target_survey_id, directory_id, response_id):
"Distribution ID could not be found, and redirect link could not be generated"
)

return distribution


def get_link(target_survey_id: str, distribution_id: str):
header = copy.deepcopy(auth_header)
header["Accept"] = "application/json"

# Distribution ID -> Link https://api.qualtrics.com/437447486af95-list-distribution-links
r = requests.get(
settings.BASE_URL + f"/distributions/{distribution['distributionId']}/links",
settings.BASE_URL + f"/distributions/{distribution_id}/links",
headers=header,
params={"surveyId": target_survey_id},
timeout=settings.TIMEOUT,
Expand Down Expand Up @@ -197,7 +216,7 @@ def get_response(survey_id: str, response_id: str):

survey_answers["response"] = answer

return response
return survey_answers


def get_survey_schema(survey_id: str):
Expand Down

0 comments on commit 0446d55

Please sign in to comment.