Survey Collaboration Management

Overview

The **Survey Collaboration Management** module provides programmatic capabilities to manage collaborators on Qualtrics surveys. The core purpose of this module is to streamline survey access control by automating tasks such as adding collaborators to surveys and accepting collaboration invitations via collaboration codes. This eliminates the need for manual interaction with the Qualtrics UI for managing sharing permissions, facilitating bulk or automated workflows.

This module is encapsulated primarily in the `CollaborationClient` class, which exposes methods to add collaborators to a survey and to accept collaboration codes programmatically. These methods interact directly with the Qualtrics Collaboration API endpoints using authenticated HTTP requests, leveraging an existing authenticated session established via automated login.


Key Functionalities and Workflows

Adding Collaborators to a Survey

The method `add_collaborator(survey_id: str, collaboration_username: str)` is designed to add a collaborator by their Qualtrics username to a specified survey.

The permissions included in the payload cover a broad spectrum of actions such as viewing, editing, translating surveys, managing quotas, exporting data, and activating surveys, ensuring the collaborator receives comprehensive access.

**Example snippet illustrating payload assembly:**

payload = {
    'shareStatus': f'{{"{collaboration_username}":"added"}}',
    'shareType': f'{{"{collaboration_username}":"Invitation"}}',
    'inviteDetails': f'{{"{collaboration_username}":{{}}}}',
    'inviteMessages': '{}',
    'permissions': f'{{"{collaboration_username}":{{"viewSurveys":true,"editSurveys":true,"activateSurveys":true, ...}}}}',
    'SurveyID': survey_id
}

This method abstracts the complexity of the collaboration API, allowing easy addition of collaborators with a single function call.


Accepting Collaboration Codes

The method `enter_collaboration_code(code: str)` allows users to programmatically accept collaboration invitations via codes.

This functionality supports workflows where multiple collaboration codes need to be processed, facilitating bulk joining of shared surveys.


Interaction with Other System Components

Relationship to Login and Session Management

The collaboration management methods depend on an authenticated session managed by the `CollaborationClient`. The authentication state (cookies and tokens) is established during the automated login process handled by Selenium and then transferred to the requests session for API calls.

Header and Cookie Management

Collaboration requests require specific HTTP headers to be accepted by Qualtrics’ API:

This tight coupling between session state and request headers ensures that API calls are authenticated and authorized.


Important Concepts and Design Patterns

1. Abstraction of Collaboration API

The `CollaborationClient` abstracts the complexity of HTTP request formatting and authentication details, exposing simple methods (`add_collaborator` and `enter_collaboration_code`) that directly map to collaboration management actions.

2. Session Synchronization

After Selenium completes the login flow, including handling potential multi-factor authentication and browser trust prompts, it extracts cookies from the browser session and seeds the `requests.Session` cookie jar. This design allows seamless transition from browser-driven authentication to lightweight API calls.

3. Unique Request Identification

Each collaboration API request includes unique `x-request-id` and `x-transaction-id` headers generated using UUIDs. This likely supports Qualtrics’ internal request tracking and prevents replay attacks or duplicate processing.


Module Workflow Visualization

The following flowchart illustrates the typical workflow for adding a collaborator or accepting a collaboration code using the `CollaborationClient`:

flowchart TD
    Start[Instantiate CollaborationClient] --> Login[Call login()]
    Login -->|Authenticated Session Established| ChooseAction{Choose Action}
    ChooseAction -->|Add Collaborator| AddCollab[Call add_collaborator(survey_id, username)]
    ChooseAction -->|Accept Collaboration Code| AcceptCode[Call enter_collaboration_code(code)]
    AddCollab --> PrepareReq[Generate Headers & Payload]
    AcceptCode --> PrepareReq
    PrepareReq --> SendReq[Send POST Request to Collaboration API]
    SendReq --> Response{API Response}
    Response -->|Success| Success[Operation Successful]
    Response -->|Failure| Failure[Handle Error]

Summary of Relevant Methods in CollaborationClient

Method

Description

`add_collaborator`

Adds a collaborator by username to a specified survey with full permissions.

`enter_collaboration_code`

Accepts a shared survey invitation using a collaboration code token.

`_generate_qualtrics_headers`

Constructs authenticated headers including tokens and unique IDs for API requests.

`cookies` (property)

Exposes current session cookies used for authentication.


This module plays a critical role in enabling automated, scalable management of survey collaborations within the system architecture, bridging authenticated login with API-level control over survey access permissions.