collaboration_client.py


Overview

`collaboration_client.py` defines a **`CollaborationClient`** class that automates login and collaboration management tasks for Qualtrics surveys, specifically tailored for the NYU Qualtrics subdomain. Its primary purpose is to:

This file encapsulates complex UI interactions and API communication, enabling users to manage survey collaborations programmatically with minimal manual intervention.


Classes and Methods

Class: CollaborationClient

Encapsulates authentication and collaboration operations with Qualtrics.


__init__(self)

**Purpose:** Initializes the client by setting up a `requests.Session` for API communication, configuring the user-agent string, and defining the Qualtrics subdomain.

**Details:**

**Parameters:** None

**Returns:** None


login(self) -> None

**Purpose:** Initiates the login process by prompting the user for their NYU username and password, then performing an automated Selenium-driven login.

**Details:**

**Parameters:** None

**Returns:** None

**Usage Example:**

client = CollaborationClient()
client.login()

add_collaborator(self, survey_id: str, collaboration_username: str) -> dict[str, str]

**Purpose:** Adds a collaborator to a specified Qualtrics survey, granting them full editing and viewing permissions.

**Details:**

**Parameters:**

**Returns:**

**Usage Example:**

response = client.add_collaborator("SV_abc123", "[email protected]")
print(response)

enter_collaboration_code(self, code: str) -> dict[str, str]

**Purpose:** Accepts a collaboration invitation on Qualtrics by submitting a collaboration code token programmatically.

**Details:**

**Parameters:**

**Returns:**

**Usage Example:**

response = client.enter_collaboration_code("abc123token")
print(response)

cookies(self) -> dict[str, str] (Property)

**Purpose:** Provides easy access to the current session cookies as a dictionary.

**Details:**

**Returns:**


_generate_qualtrics_headers(self, content_type: str) -> dict

**Purpose:** Generates the HTTP headers required for Qualtrics API requests, including security tokens and unique request identifiers.

**Details:**

**Parameters:**

**Returns:**


_selenium_login(self, nyu_username: str, nyu_password: str) -> None

**Purpose:** Automates login to Qualtrics using Selenium WebDriver, handling username/password entry, 2FA, and browser trust prompts.

**Details:**

**Parameters:**

**Returns:** None


human_sleep() -> None (Static Method)

**Purpose:** Introduces a randomized pause to simulate human-like waiting times on web pages.

**Details:**

**Returns:** None

**Usage:** Called between key UI interactions to mimic natural user delays.


_human_type(element: WebElement, string: str) -> None (Static Method)

**Purpose:** Simulates human typing by sending characters one-by-one to a web element with varying delays.

**Details:**

**Parameters:**

**Returns:** None


_at_qualtrics(driver: webdriver) -> bool (Static Method)

**Purpose:** Checks whether the Selenium-controlled browser has successfully reached the authenticated Qualtrics landing page.

**Details:**

**Parameters:**

**Returns:**


_check_for_trust_browser_page(driver: webdriver) -> bool (Static Method)

**Purpose:** Detects whether the browser trust prompt page is currently shown.

**Details:**

**Parameters:**

**Returns:**


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Class Structure and Key Methods

classDiagram
    class CollaborationClient {
        -session: requests.Session
        -user_agent: str
        -subdomain: str
        +login()
        +add_collaborator(survey_id: str, collaboration_username: str) dict[str, str]
        +enter_collaboration_code(code: str) dict[str, str]
        +cookies() dict[str, str]
        -_generate_qualtrics_headers(content_type: str) dict
        -_selenium_login(nyu_username: str, nyu_password: str) None
        +human_sleep() None
        -_human_type(element: WebElement, string: str) None
        -_at_qualtrics(driver: webdriver) bool
        -_check_for_trust_browser_page(driver: webdriver) bool
    }

Summary


If you require further details on any method or integration with other parts of the system, please let me know!