|
| 1 | +from sift_client import SiftClient |
| 2 | + |
1 | 3 | # Contributing |
2 | 4 |
|
3 | 5 | ## Style Guidelines |
@@ -159,3 +161,67 @@ The `ResourceBase` class provides helper methods to build consistent CEL filter |
159 | 161 | To generate a sync API from an async API, add a `generate_sync_api` function call in `sift_client/resources/sync_stubs/__init__.py` and |
160 | 162 | run the `dev gen-stubs` script to generate the associated type stubs. |
161 | 163 |
|
| 164 | +#### Complex Sift Resources |
| 165 | + |
| 166 | +For more complex Sift objects, such as `Reports` and `ReportTemplates`, |
| 167 | +where the relationship is tightly coupled and there is a clear hierarchy, |
| 168 | +the high-level API should implement a nested resource. |
| 169 | + |
| 170 | +This is to: |
| 171 | + |
| 172 | +* Avoid too many top-level resources by logically grouping based on the domain |
| 173 | +* Ensure consistent CRUD method signatures - users should not need to look up the docs to know what methods are available |
| 174 | + |
| 175 | +For example, `Reports` should be implemented as a nested resource of `ReportTemplates`: |
| 176 | + |
| 177 | +```python |
| 178 | +# resources/reports.py |
| 179 | + |
| 180 | +class ReportTemplates(ResourceBase): |
| 181 | + def __init__(self, client: SiftClient): |
| 182 | + super().__init__(sift_client) |
| 183 | + self._low_level_client = ReportTemplatesLowLevelClient(grpc_client=self.client.grpc_client) |
| 184 | + |
| 185 | + def get(self, report_template_id: str) -> ReportTemplate: |
| 186 | + ... |
| 187 | + |
| 188 | + def list_(self, ...) -> List[ReportTemplate]: |
| 189 | + ... |
| 190 | + |
| 191 | + def create(self, ...) -> ReportTemplate: |
| 192 | + ... |
| 193 | + |
| 194 | + def update(self, ...) -> ReportTemplate: |
| 195 | + ... |
| 196 | + |
| 197 | +class Reports(ResourceBase): |
| 198 | + |
| 199 | + def __init__(self, client: SiftClient): |
| 200 | + super().__init__(sift_client) |
| 201 | + self._low_level_client = ReportsLowLevelClient(grpc_client=self.client.grpc_client) |
| 202 | + self.report_templates = ReportTemplates(client=self.client) |
| 203 | + |
| 204 | + def get(self, report_template_id: str) -> Report: |
| 205 | + ... |
| 206 | + |
| 207 | + def list_(self, ...) -> List[Report]: |
| 208 | + ... |
| 209 | + |
| 210 | + def create(self, ...) -> Report: |
| 211 | + ... |
| 212 | + |
| 213 | + def update(self, ...) -> Report: |
| 214 | + ... |
| 215 | + |
| 216 | +# Sift client access |
| 217 | + |
| 218 | +sc = SiftClient() |
| 219 | + |
| 220 | +# Get a specific report |
| 221 | +report = sc.reports.get(report_id) |
| 222 | + |
| 223 | +report_template = sc.reports.report_templates.get(report_template_id) |
| 224 | +``` |
| 225 | + |
| 226 | + |
| 227 | + |
0 commit comments