|
| 1 | +# dapi/projects.py |
| 2 | +import requests |
| 3 | +from tapipy.tapis import Tapis |
| 4 | +from tapipy.errors import BaseTapyException |
| 5 | +from .exceptions import FileOperationError |
| 6 | +from typing import Dict, List, Optional |
| 7 | + |
| 8 | + |
| 9 | +_DS_PROJECTS_API = "https://designsafe-ci.org/api/projects/v2/" |
| 10 | + |
| 11 | + |
| 12 | +def _get_auth_headers(t: Tapis) -> Dict[str, str]: |
| 13 | + """Build authentication headers from a Tapis client.""" |
| 14 | + token = t.access_token.access_token |
| 15 | + return {"X-Tapis-Token": token, "Authorization": f"Bearer {token}"} |
| 16 | + |
| 17 | + |
| 18 | +def list_projects(t: Tapis, limit: int = 100, offset: int = 0) -> List[Dict]: |
| 19 | + """List DesignSafe projects the authenticated user has access to. |
| 20 | +
|
| 21 | + Args: |
| 22 | + t (Tapis): Authenticated Tapis client instance. |
| 23 | + limit (int, optional): Maximum number of projects to return. Defaults to 100. |
| 24 | + offset (int, optional): Number of projects to skip. Defaults to 0. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + List[Dict]: List of project dictionaries with keys: |
| 28 | + - uuid (str): Project UUID |
| 29 | + - projectId (str): Project ID (e.g., "PRJ-1305") |
| 30 | + - title (str): Project title |
| 31 | + - pi (dict): Principal investigator info (username, fname, lname) |
| 32 | + - created (str): Creation timestamp |
| 33 | + - lastUpdated (str): Last update timestamp |
| 34 | +
|
| 35 | + Raises: |
| 36 | + FileOperationError: If the API request fails. |
| 37 | + """ |
| 38 | + headers = _get_auth_headers(t) |
| 39 | + try: |
| 40 | + resp = requests.get( |
| 41 | + _DS_PROJECTS_API, |
| 42 | + headers=headers, |
| 43 | + params={"limit": limit, "offset": offset}, |
| 44 | + timeout=30, |
| 45 | + ) |
| 46 | + resp.raise_for_status() |
| 47 | + except requests.RequestException as e: |
| 48 | + raise FileOperationError(f"Failed to list projects: {e}") from e |
| 49 | + |
| 50 | + data = resp.json() |
| 51 | + projects = [] |
| 52 | + for p in data.get("result", []): |
| 53 | + val = p.get("value", {}) |
| 54 | + users = val.get("users", []) |
| 55 | + pi = next((u for u in users if u.get("role") == "pi"), None) |
| 56 | + projects.append( |
| 57 | + { |
| 58 | + "uuid": p.get("uuid"), |
| 59 | + "projectId": val.get("projectId"), |
| 60 | + "title": val.get("title"), |
| 61 | + "pi": pi, |
| 62 | + "created": p.get("created"), |
| 63 | + "lastUpdated": p.get("lastUpdated"), |
| 64 | + } |
| 65 | + ) |
| 66 | + return projects |
| 67 | + |
| 68 | + |
| 69 | +def get_project(t: Tapis, project_id: str) -> Dict: |
| 70 | + """Get detailed metadata for a DesignSafe project. |
| 71 | +
|
| 72 | + Args: |
| 73 | + t (Tapis): Authenticated Tapis client instance. |
| 74 | + project_id (str): Project ID (e.g., "PRJ-1305") or project UUID. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + Dict: Project metadata with keys: |
| 78 | + - uuid (str): Project UUID |
| 79 | + - projectId (str): Project ID |
| 80 | + - title (str): Project title |
| 81 | + - description (str): Project description |
| 82 | + - pi (dict): Principal investigator info |
| 83 | + - coPis (list): Co-PIs |
| 84 | + - teamMembers (list): Team members |
| 85 | + - awardNumbers (list): Award/grant numbers |
| 86 | + - keywords (list): Keywords |
| 87 | + - dois (list): Associated DOIs |
| 88 | + - projectType (str): Project type |
| 89 | + - created (str): Creation timestamp |
| 90 | + - lastUpdated (str): Last update timestamp |
| 91 | + - systemId (str): Tapis system ID for file access |
| 92 | +
|
| 93 | + Raises: |
| 94 | + FileOperationError: If the project is not found or the API request fails. |
| 95 | + """ |
| 96 | + headers = _get_auth_headers(t) |
| 97 | + try: |
| 98 | + resp = requests.get( |
| 99 | + f"{_DS_PROJECTS_API}{project_id}/", |
| 100 | + headers=headers, |
| 101 | + timeout=30, |
| 102 | + ) |
| 103 | + resp.raise_for_status() |
| 104 | + except requests.RequestException as e: |
| 105 | + raise FileOperationError(f"Failed to get project '{project_id}': {e}") from e |
| 106 | + |
| 107 | + data = resp.json() |
| 108 | + bp = data.get("baseProject", {}) |
| 109 | + val = bp.get("value", {}) |
| 110 | + users = val.get("users", []) |
| 111 | + pi = next((u for u in users if u.get("role") == "pi"), None) |
| 112 | + uuid = bp.get("uuid", "") |
| 113 | + |
| 114 | + return { |
| 115 | + "uuid": uuid, |
| 116 | + "projectId": val.get("projectId"), |
| 117 | + "title": val.get("title"), |
| 118 | + "description": val.get("description"), |
| 119 | + "pi": pi, |
| 120 | + "coPis": val.get("coPis", []), |
| 121 | + "teamMembers": val.get("teamMembers", []), |
| 122 | + "awardNumbers": val.get("awardNumbers", []), |
| 123 | + "keywords": val.get("keywords", []), |
| 124 | + "dois": val.get("dois", []), |
| 125 | + "projectType": val.get("projectType"), |
| 126 | + "created": bp.get("created"), |
| 127 | + "lastUpdated": bp.get("lastUpdated"), |
| 128 | + "systemId": f"project-{uuid}" if uuid else None, |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | +def list_project_files( |
| 133 | + t: Tapis, project_id: str, path: str = "/", limit: int = 100 |
| 134 | +) -> List: |
| 135 | + """List files in a DesignSafe project. |
| 136 | +
|
| 137 | + Resolves the project ID to a Tapis system and lists files at the given path. |
| 138 | +
|
| 139 | + Args: |
| 140 | + t (Tapis): Authenticated Tapis client instance. |
| 141 | + project_id (str): Project ID (e.g., "PRJ-1305"). |
| 142 | + path (str, optional): Path within the project. Defaults to "/". |
| 143 | + limit (int, optional): Maximum number of items to return. Defaults to 100. |
| 144 | +
|
| 145 | + Returns: |
| 146 | + List: List of Tapis file objects with name, type, size, etc. |
| 147 | +
|
| 148 | + Raises: |
| 149 | + FileOperationError: If the project is not found or file listing fails. |
| 150 | + """ |
| 151 | + project = get_project(t, project_id) |
| 152 | + system_id = project["systemId"] |
| 153 | + if not system_id: |
| 154 | + raise FileOperationError( |
| 155 | + f"Could not determine Tapis system ID for project '{project_id}'." |
| 156 | + ) |
| 157 | + |
| 158 | + if not path: |
| 159 | + path = "/" |
| 160 | + |
| 161 | + try: |
| 162 | + results = t.files.listFiles(systemId=system_id, path=path, limit=limit) |
| 163 | + return results |
| 164 | + except BaseTapyException as e: |
| 165 | + raise FileOperationError( |
| 166 | + f"Failed to list files in project '{project_id}' at path '{path}': {e}" |
| 167 | + ) from e |
| 168 | + |
| 169 | + |
| 170 | +def resolve_project_uuid(t: Tapis, project_id: str) -> str: |
| 171 | + """Resolve a DesignSafe project ID (e.g., PRJ-1305) to its Tapis system ID. |
| 172 | +
|
| 173 | + Args: |
| 174 | + t (Tapis): Authenticated Tapis client instance. |
| 175 | + project_id (str): The DesignSafe project ID (e.g., "PRJ-1305"). |
| 176 | +
|
| 177 | + Returns: |
| 178 | + str: The Tapis system ID (e.g., "project-7997906542076432871-242ac11c-0001-012"). |
| 179 | +
|
| 180 | + Raises: |
| 181 | + FileOperationError: If the project cannot be found. |
| 182 | + """ |
| 183 | + headers = _get_auth_headers(t) |
| 184 | + try: |
| 185 | + resp = requests.get( |
| 186 | + _DS_PROJECTS_API, |
| 187 | + headers=headers, |
| 188 | + params={"limit": 100}, |
| 189 | + timeout=30, |
| 190 | + ) |
| 191 | + resp.raise_for_status() |
| 192 | + projects = resp.json().get("result", []) |
| 193 | + for p in projects: |
| 194 | + val = p.get("value", {}) |
| 195 | + if val.get("projectId", "") == project_id: |
| 196 | + uuid = p["uuid"] |
| 197 | + return f"project-{uuid}" |
| 198 | + except requests.RequestException as e: |
| 199 | + raise FileOperationError( |
| 200 | + f"Failed to query DesignSafe projects API for '{project_id}': {e}" |
| 201 | + ) from e |
| 202 | + |
| 203 | + raise FileOperationError( |
| 204 | + f"Project '{project_id}' not found. Ensure you have access to this project." |
| 205 | + ) |
0 commit comments