pyprocore 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
services/submittals.py ADDED
@@ -0,0 +1,140 @@
1
+ """Submittal service for the Procore SDK."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping, Sequence
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from core import endpoints
10
+ from core.client import ProcoreClient
11
+ from core.exceptions import ValidationError
12
+ from models import Submittal
13
+ from services.files import FileDownloadService
14
+
15
+ DEFAULT_DOWNLOAD_DIR = Path(__file__).resolve().parents[1] / "downloads" / "submittals"
16
+
17
+
18
+ class SubmittalsService:
19
+ """Service for Procore submittal resources."""
20
+
21
+ def __init__(
22
+ self,
23
+ client: ProcoreClient | None = None,
24
+ file_service: FileDownloadService | None = None,
25
+ ) -> None:
26
+ """Initialize the service.
27
+
28
+ Args:
29
+ client: Optional shared Procore HTTP client.
30
+ file_service: Optional shared file download service.
31
+ """
32
+ self._client = client or ProcoreClient()
33
+ self._file_service = file_service or FileDownloadService()
34
+
35
+ def list_submittals(self, project_id: int) -> list[Submittal]:
36
+ """Return submittals for a Procore project."""
37
+ self._validate_positive_id(project_id, "project_id")
38
+
39
+ response = self._client.get_all(endpoints.submittals(project_id))
40
+ if isinstance(response, list):
41
+ return [Submittal.model_validate(submittal) for submittal in response]
42
+ return [Submittal.model_validate(response)]
43
+
44
+ def get_submittal(self, project_id: int, submittal_id: int) -> Submittal:
45
+ """Return a single submittal for a Procore project."""
46
+ self._validate_positive_id(project_id, "project_id")
47
+ self._validate_positive_id(submittal_id, "submittal_id")
48
+
49
+ response = self._client.get(endpoints.submittal(project_id, submittal_id))
50
+ if not isinstance(response, dict):
51
+ raise ValidationError(
52
+ "Expected Procore submittal response to be an object."
53
+ )
54
+ return Submittal.model_validate(response)
55
+
56
+ def download_submittal_attachments(
57
+ self,
58
+ project_id: int,
59
+ submittal_id: int,
60
+ destination_dir: Path | str | None = None,
61
+ ) -> list[Path]:
62
+ """Download all attachments from a submittal.
63
+
64
+ Submittal attachment URLs are read from ``attachments[].url``.
65
+
66
+ Args:
67
+ project_id: Procore project ID.
68
+ submittal_id: Procore submittal ID.
69
+ destination_dir: Optional directory to save files. Defaults to
70
+ ``downloads/submittals/{submittal_id}``.
71
+
72
+ Returns:
73
+ Paths to downloaded files.
74
+ """
75
+ submittal = self.get_submittal(project_id, submittal_id)
76
+ attachments = self._extract_attachments(submittal.model_dump())
77
+ output_dir = (
78
+ Path(destination_dir)
79
+ if destination_dir
80
+ else DEFAULT_DOWNLOAD_DIR / str(submittal_id)
81
+ )
82
+ output_dir.mkdir(parents=True, exist_ok=True)
83
+
84
+ return self._file_service.download_attachments(
85
+ attachments,
86
+ output_dir,
87
+ fallback_prefix=f"submittal-{submittal_id}",
88
+ )
89
+
90
+ @staticmethod
91
+ def _extract_attachments(submittal: Mapping[str, Any]) -> list[dict[str, Any]]:
92
+ """Extract ``attachments[]`` dictionaries from a submittal."""
93
+ attachments = submittal.get("attachments", [])
94
+ if not isinstance(attachments, Sequence) or isinstance(
95
+ attachments, (str, bytes)
96
+ ):
97
+ return []
98
+
99
+ return [
100
+ dict(attachment)
101
+ for attachment in attachments
102
+ if isinstance(attachment, Mapping)
103
+ ]
104
+
105
+ @staticmethod
106
+ def _validate_positive_id(value: int, name: str) -> None:
107
+ """Validate Procore integer identifiers."""
108
+ if value <= 0:
109
+ raise ValidationError(f"{name} must be a positive integer.")
110
+
111
+
112
+ def list_submittals(
113
+ project_id: int,
114
+ client: ProcoreClient | None = None,
115
+ ) -> list[Submittal]:
116
+ """Return submittals for a Procore project."""
117
+ return SubmittalsService(client=client).list_submittals(project_id)
118
+
119
+
120
+ def get_submittal(
121
+ project_id: int,
122
+ submittal_id: int,
123
+ client: ProcoreClient | None = None,
124
+ ) -> Submittal:
125
+ """Return a single submittal for a Procore project."""
126
+ return SubmittalsService(client=client).get_submittal(project_id, submittal_id)
127
+
128
+
129
+ def download_submittal_attachments(
130
+ project_id: int,
131
+ submittal_id: int,
132
+ destination_dir: Path | str | None = None,
133
+ client: ProcoreClient | None = None,
134
+ ) -> list[Path]:
135
+ """Download all attachments from a submittal."""
136
+ return SubmittalsService(client=client).download_submittal_attachments(
137
+ project_id,
138
+ submittal_id,
139
+ destination_dir,
140
+ )