raspbytes 1.1.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.
Files changed (39) hide show
  1. raspbytes/__init__.py +87 -0
  2. raspbytes/_core/__init__.py +1 -0
  3. raspbytes/_core/constants.py +38 -0
  4. raspbytes/_core/http.py +160 -0
  5. raspbytes/_core/transport.py +175 -0
  6. raspbytes/_core/types.py +11 -0
  7. raspbytes/_core/version.py +8 -0
  8. raspbytes/client.py +88 -0
  9. raspbytes/config.py +122 -0
  10. raspbytes/countries.py +278 -0
  11. raspbytes/exceptions.py +115 -0
  12. raspbytes/managed_scrapers.py +4 -0
  13. raspbytes/models.py +89 -0
  14. raspbytes/py.typed +1 -0
  15. raspbytes/resources/__init__.py +17 -0
  16. raspbytes/resources/_base.py +75 -0
  17. raspbytes/resources/_polling.py +73 -0
  18. raspbytes/resources/browser.py +185 -0
  19. raspbytes/resources/proxies.py +49 -0
  20. raspbytes/resources/scraper/__init__.py +45 -0
  21. raspbytes/resources/scraper/_generated/__init__.py +1 -0
  22. raspbytes/resources/scraper/_generated/managed.py +190 -0
  23. raspbytes/resources/scraper/_generated/wire.py +632 -0
  24. raspbytes/resources/scraper/_managed.py +14 -0
  25. raspbytes/resources/scraper/_request.py +128 -0
  26. raspbytes/resources/scraper/builder.py +199 -0
  27. raspbytes/resources/scraper/hub.py +168 -0
  28. raspbytes/resources/scraper/monitoring.py +132 -0
  29. raspbytes/resources/scraper/runs.py +202 -0
  30. raspbytes/resources/scraper/schedules.py +172 -0
  31. raspbytes/resources/scraper/storage.py +155 -0
  32. raspbytes/resources/scraper/tasks.py +134 -0
  33. raspbytes/resources/scraper/types.py +96 -0
  34. raspbytes/resources/unblocker.py +131 -0
  35. raspbytes/scraper_types.py +4 -0
  36. raspbytes-1.1.0.dist-info/METADATA +297 -0
  37. raspbytes-1.1.0.dist-info/RECORD +39 -0
  38. raspbytes-1.1.0.dist-info/WHEEL +4 -0
  39. raspbytes-1.1.0.dist-info/licenses/LICENSE +25 -0
@@ -0,0 +1,128 @@
1
+ """Shared request and pagination helpers for the Scraper API."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from collections.abc import Mapping
7
+ from http import HTTPMethod
8
+ from typing import TypeVar, cast, get_origin
9
+ from urllib.parse import parse_qsl, urlsplit
10
+
11
+ from ..._core.constants import RequestField
12
+ from ..._core.types import Query, ResponseData
13
+ from ...countries import Country, country_code
14
+ from ...exceptions import ConfigurationError
15
+ from .._base import AsyncResource, SyncResource
16
+ from ._generated.wire import ScrapeJobResponse
17
+
18
+ _Response = TypeVar("_Response")
19
+ _IDEMPOTENCY_KEY = "idempotency_key"
20
+ _EXECUTION_CONFIGURATION = "configuration"
21
+ _RESOURCE_ID = re.compile(r"[A-Za-z0-9_-]+")
22
+
23
+
24
+ def _path_id(value: str) -> str:
25
+ if not _RESOURCE_ID.fullmatch(value):
26
+ raise ConfigurationError("Scraper resource IDs must contain only letters, digits, _ or -")
27
+ return value
28
+
29
+
30
+ def _check_response_shape(data: ResponseData, shape: type[_Response]) -> _Response:
31
+ expected = list if get_origin(shape) is list else dict
32
+ if not isinstance(data, expected):
33
+ raise ConfigurationError(f"expected a {expected.__name__} response from the Scraper API")
34
+ return cast(_Response, data)
35
+
36
+
37
+ def _with_origin_country(options: Mapping[str, object]) -> dict[str, object]:
38
+ result = dict(options)
39
+ country = result.get(RequestField.COUNTRY)
40
+ if country is not None:
41
+ if not isinstance(country, (str, Country)):
42
+ raise ConfigurationError("country must be a two-letter country code")
43
+ result[RequestField.COUNTRY] = country_code(country)
44
+ return result
45
+
46
+
47
+ def _run_payload(params: Mapping[str, object]) -> tuple[dict[str, object], bool]:
48
+ body = _with_origin_country(params)
49
+ key = body.get(_IDEMPOTENCY_KEY)
50
+ if key is not None and (not isinstance(key, str) or not key.strip() or len(key) > 128):
51
+ raise ConfigurationError("idempotency_key must contain between 1 and 128 characters")
52
+ configuration = body.get(_EXECUTION_CONFIGURATION)
53
+ if isinstance(configuration, Mapping):
54
+ body[_EXECUTION_CONFIGURATION] = _with_origin_country(configuration)
55
+ return body, key is not None
56
+
57
+
58
+ def _continuation(
59
+ next_link: str,
60
+ seen: set[tuple[tuple[str, str], ...]],
61
+ ) -> dict[str, str]:
62
+ # Only reuse query values. All requests go to the configured endpoint.
63
+ query = dict(parse_qsl(urlsplit(next_link).query, keep_blank_values=True))
64
+ signature = tuple(sorted(query.items()))
65
+ if not query or signature in seen:
66
+ raise ConfigurationError("Invalid or repeated Scraper continuation")
67
+ seen.add(signature)
68
+ return query
69
+
70
+
71
+ class _SyncScraperEndpoints(SyncResource):
72
+ def _submit_run(self, path: str, request: Mapping[str, object]) -> ScrapeJobResponse:
73
+ body, idempotent = _run_payload(request)
74
+ return self._request(
75
+ HTTPMethod.POST, path, ScrapeJobResponse, body=body, idempotent=idempotent
76
+ )
77
+
78
+ def _request(
79
+ self,
80
+ method: str,
81
+ path: str,
82
+ shape: type[_Response],
83
+ *,
84
+ params: Query | None = None,
85
+ body: Mapping[str, object] | None = None,
86
+ idempotent: bool = False,
87
+ ) -> _Response:
88
+ return _check_response_shape(
89
+ self._transport.request(
90
+ method,
91
+ path,
92
+ params=params,
93
+ json=body,
94
+ config=self._request_config,
95
+ retry_response=method == HTTPMethod.GET or idempotent,
96
+ ),
97
+ shape,
98
+ )
99
+
100
+
101
+ class _AsyncScraperEndpoints(AsyncResource):
102
+ async def _submit_run(self, path: str, request: Mapping[str, object]) -> ScrapeJobResponse:
103
+ body, idempotent = _run_payload(request)
104
+ return await self._request(
105
+ HTTPMethod.POST, path, ScrapeJobResponse, body=body, idempotent=idempotent
106
+ )
107
+
108
+ async def _request(
109
+ self,
110
+ method: str,
111
+ path: str,
112
+ shape: type[_Response],
113
+ *,
114
+ params: Query | None = None,
115
+ body: Mapping[str, object] | None = None,
116
+ idempotent: bool = False,
117
+ ) -> _Response:
118
+ return _check_response_shape(
119
+ await self._transport.request(
120
+ method,
121
+ path,
122
+ params=params,
123
+ json=body,
124
+ config=self._request_config,
125
+ retry_response=method == HTTPMethod.GET or idempotent,
126
+ ),
127
+ shape,
128
+ )
@@ -0,0 +1,199 @@
1
+ """Account-owned scrapers, immutable revisions, sharing, and publication."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import builtins
6
+ from http import HTTPMethod
7
+
8
+ from ._generated.wire import ScraperRoute
9
+ from ._request import (
10
+ _AsyncScraperEndpoints,
11
+ _path_id,
12
+ _SyncScraperEndpoints,
13
+ _with_origin_country,
14
+ )
15
+ from .types import (
16
+ CreateOwnedScraperParams,
17
+ CreateOwnedScraperRevisionParams,
18
+ OwnedScraper,
19
+ OwnedScraperRevision,
20
+ OwnedScraperShare,
21
+ ShareOwnedScraperParams,
22
+ UpdateOwnedScraperParams,
23
+ )
24
+
25
+
26
+ def _revision_body(request: CreateOwnedScraperRevisionParams) -> dict[str, object]:
27
+ return {**request, "configuration": _with_origin_country(request["configuration"])}
28
+
29
+
30
+ class ScraperBuilder(_SyncScraperEndpoints):
31
+ """Account-owned scrapers, immutable revisions, sharing, and publication."""
32
+
33
+ def list(self) -> builtins.list[OwnedScraper]:
34
+ return self._request(HTTPMethod.GET, ScraperRoute.CREATOR_SCRAPERS_LIST, list[OwnedScraper])
35
+
36
+ def get(self, scraper_id: str) -> OwnedScraper:
37
+ return self._request(
38
+ HTTPMethod.GET,
39
+ ScraperRoute.CREATOR_SCRAPERS_RETRIEVE.format(scraper_id=_path_id(scraper_id)),
40
+ OwnedScraper,
41
+ )
42
+
43
+ def create(self, request: CreateOwnedScraperParams) -> OwnedScraper:
44
+ body: dict[str, object] = dict(request)
45
+ if "revision" in request:
46
+ body["revision"] = _revision_body(request["revision"])
47
+ return self._request(
48
+ HTTPMethod.POST, ScraperRoute.CREATOR_SCRAPERS_CREATE, OwnedScraper, body=body
49
+ )
50
+
51
+ def update(self, scraper_id: str, request: UpdateOwnedScraperParams) -> OwnedScraper:
52
+ return self._request(
53
+ HTTPMethod.PATCH,
54
+ ScraperRoute.CREATOR_SCRAPERS_UPDATE.format(scraper_id=_path_id(scraper_id)),
55
+ OwnedScraper,
56
+ body=request,
57
+ )
58
+
59
+ def archive(self, scraper_id: str) -> OwnedScraper:
60
+ return self._request(
61
+ HTTPMethod.DELETE,
62
+ ScraperRoute.CREATOR_SCRAPERS_ARCHIVE.format(scraper_id=_path_id(scraper_id)),
63
+ OwnedScraper,
64
+ )
65
+
66
+ def list_revisions(self, scraper_id: str) -> builtins.list[OwnedScraperRevision]:
67
+ return self._request(
68
+ HTTPMethod.GET,
69
+ ScraperRoute.CREATOR_SCRAPER_REVISIONS_LIST.format(scraper_id=_path_id(scraper_id)),
70
+ list[OwnedScraperRevision],
71
+ )
72
+
73
+ def create_revision(
74
+ self, scraper_id: str, request: CreateOwnedScraperRevisionParams
75
+ ) -> OwnedScraperRevision:
76
+ return self._request(
77
+ HTTPMethod.POST,
78
+ ScraperRoute.CREATOR_SCRAPER_REVISIONS_CREATE.format(scraper_id=_path_id(scraper_id)),
79
+ OwnedScraperRevision,
80
+ body=_revision_body(request),
81
+ )
82
+
83
+ def share(self, scraper_id: str, request: ShareOwnedScraperParams) -> OwnedScraperShare:
84
+ return self._request(
85
+ HTTPMethod.POST,
86
+ ScraperRoute.CREATOR_SCRAPER_SHARES_CREATE.format(scraper_id=_path_id(scraper_id)),
87
+ OwnedScraperShare,
88
+ body=request,
89
+ )
90
+
91
+ def revoke_share(self, scraper_id: str, share_id: str) -> OwnedScraper:
92
+ return self._request(
93
+ HTTPMethod.DELETE,
94
+ ScraperRoute.CREATOR_SCRAPER_SHARES_REVOKE.format(
95
+ scraper_id=_path_id(scraper_id), share_id=_path_id(share_id)
96
+ ),
97
+ OwnedScraper,
98
+ )
99
+
100
+ def publish(self, scraper_id: str) -> OwnedScraper:
101
+ return self._request(
102
+ HTTPMethod.POST,
103
+ ScraperRoute.CREATOR_SCRAPERS_PUBLISH.format(scraper_id=_path_id(scraper_id)),
104
+ OwnedScraper,
105
+ )
106
+
107
+ def unpublish(self, scraper_id: str) -> OwnedScraper:
108
+ return self._request(
109
+ HTTPMethod.DELETE,
110
+ ScraperRoute.CREATOR_SCRAPERS_UNPUBLISH.format(scraper_id=_path_id(scraper_id)),
111
+ OwnedScraper,
112
+ )
113
+
114
+
115
+ class AsyncScraperBuilder(_AsyncScraperEndpoints):
116
+ """Account-owned scrapers, immutable revisions, sharing, and publication."""
117
+
118
+ async def list(self) -> builtins.list[OwnedScraper]:
119
+ return await self._request(
120
+ HTTPMethod.GET, ScraperRoute.CREATOR_SCRAPERS_LIST, list[OwnedScraper]
121
+ )
122
+
123
+ async def get(self, scraper_id: str) -> OwnedScraper:
124
+ return await self._request(
125
+ HTTPMethod.GET,
126
+ ScraperRoute.CREATOR_SCRAPERS_RETRIEVE.format(scraper_id=_path_id(scraper_id)),
127
+ OwnedScraper,
128
+ )
129
+
130
+ async def create(self, request: CreateOwnedScraperParams) -> OwnedScraper:
131
+ body: dict[str, object] = dict(request)
132
+ if "revision" in request:
133
+ body["revision"] = _revision_body(request["revision"])
134
+ return await self._request(
135
+ HTTPMethod.POST, ScraperRoute.CREATOR_SCRAPERS_CREATE, OwnedScraper, body=body
136
+ )
137
+
138
+ async def update(self, scraper_id: str, request: UpdateOwnedScraperParams) -> OwnedScraper:
139
+ return await self._request(
140
+ HTTPMethod.PATCH,
141
+ ScraperRoute.CREATOR_SCRAPERS_UPDATE.format(scraper_id=_path_id(scraper_id)),
142
+ OwnedScraper,
143
+ body=request,
144
+ )
145
+
146
+ async def archive(self, scraper_id: str) -> OwnedScraper:
147
+ return await self._request(
148
+ HTTPMethod.DELETE,
149
+ ScraperRoute.CREATOR_SCRAPERS_ARCHIVE.format(scraper_id=_path_id(scraper_id)),
150
+ OwnedScraper,
151
+ )
152
+
153
+ async def list_revisions(self, scraper_id: str) -> builtins.list[OwnedScraperRevision]:
154
+ return await self._request(
155
+ HTTPMethod.GET,
156
+ ScraperRoute.CREATOR_SCRAPER_REVISIONS_LIST.format(scraper_id=_path_id(scraper_id)),
157
+ list[OwnedScraperRevision],
158
+ )
159
+
160
+ async def create_revision(
161
+ self, scraper_id: str, request: CreateOwnedScraperRevisionParams
162
+ ) -> OwnedScraperRevision:
163
+ return await self._request(
164
+ HTTPMethod.POST,
165
+ ScraperRoute.CREATOR_SCRAPER_REVISIONS_CREATE.format(scraper_id=_path_id(scraper_id)),
166
+ OwnedScraperRevision,
167
+ body=_revision_body(request),
168
+ )
169
+
170
+ async def share(self, scraper_id: str, request: ShareOwnedScraperParams) -> OwnedScraperShare:
171
+ return await self._request(
172
+ HTTPMethod.POST,
173
+ ScraperRoute.CREATOR_SCRAPER_SHARES_CREATE.format(scraper_id=_path_id(scraper_id)),
174
+ OwnedScraperShare,
175
+ body=request,
176
+ )
177
+
178
+ async def revoke_share(self, scraper_id: str, share_id: str) -> OwnedScraper:
179
+ return await self._request(
180
+ HTTPMethod.DELETE,
181
+ ScraperRoute.CREATOR_SCRAPER_SHARES_REVOKE.format(
182
+ scraper_id=_path_id(scraper_id), share_id=_path_id(share_id)
183
+ ),
184
+ OwnedScraper,
185
+ )
186
+
187
+ async def publish(self, scraper_id: str) -> OwnedScraper:
188
+ return await self._request(
189
+ HTTPMethod.POST,
190
+ ScraperRoute.CREATOR_SCRAPERS_PUBLISH.format(scraper_id=_path_id(scraper_id)),
191
+ OwnedScraper,
192
+ )
193
+
194
+ async def unpublish(self, scraper_id: str) -> OwnedScraper:
195
+ return await self._request(
196
+ HTTPMethod.DELETE,
197
+ ScraperRoute.CREATOR_SCRAPERS_UNPUBLISH.format(scraper_id=_path_id(scraper_id)),
198
+ OwnedScraper,
199
+ )
@@ -0,0 +1,168 @@
1
+ """Scraper discovery, managed inputs, and Hub run workflows."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping
6
+ from http import HTTPMethod
7
+ from typing import Literal, TypeVar, cast
8
+
9
+ from ..._core.transport import AsyncTransport, SyncTransport
10
+ from ...config import ScraperConfig
11
+ from .._polling import ObjectResponse
12
+ from ._generated.wire import (
13
+ ScrapeJobResponse,
14
+ ScraperHubItemResponse,
15
+ ScraperHubResponse,
16
+ ScraperHubRun,
17
+ ScraperHubRunConfiguration,
18
+ ScraperRoute,
19
+ )
20
+ from ._managed import ManagedScraper
21
+ from ._request import (
22
+ _IDEMPOTENCY_KEY,
23
+ _AsyncScraperEndpoints,
24
+ _path_id,
25
+ _SyncScraperEndpoints,
26
+ )
27
+ from .runs import AsyncScraperRuns, ScraperRuns, _run_done
28
+
29
+ HubScope = Literal["all", "raspbytes", "mine", "private"]
30
+ _Inputs = TypeVar("_Inputs", bound=Mapping[str, object])
31
+
32
+
33
+ def _managed_params(
34
+ scraper: ManagedScraper[_Inputs],
35
+ inputs: _Inputs,
36
+ configuration: ScraperHubRunConfiguration | None,
37
+ idempotency_key: str | None,
38
+ url: str | None,
39
+ ) -> dict[str, object]:
40
+ return {
41
+ key: value
42
+ for key, value in {
43
+ "revision": scraper.revision,
44
+ "inputs": inputs,
45
+ "configuration": configuration,
46
+ _IDEMPOTENCY_KEY: idempotency_key,
47
+ "url": url,
48
+ }.items()
49
+ if value is not None
50
+ }
51
+
52
+
53
+ class ScraperHub(_SyncScraperEndpoints):
54
+ def __init__(self, transport: SyncTransport, config: ScraperConfig, runs: ScraperRuns) -> None:
55
+ super().__init__(transport, config)
56
+ self._runs = runs
57
+
58
+ def list(
59
+ self,
60
+ *,
61
+ scope: HubScope = "all",
62
+ category: str | None = None,
63
+ search: str | None = None,
64
+ ) -> ScraperHubResponse:
65
+ return self._request(
66
+ HTTPMethod.GET,
67
+ ScraperRoute.SCRAPER_HUB_LIST,
68
+ ScraperHubResponse,
69
+ params={"scope": scope, "category": category, "search": search},
70
+ )
71
+
72
+ def get(self, scraper_id: str) -> ScraperHubItemResponse:
73
+ path = ScraperRoute.SCRAPER_HUB_RETRIEVE.format(scraper_id=_path_id(scraper_id))
74
+ return self._request(HTTPMethod.GET, path, ScraperHubItemResponse)
75
+
76
+ def run(self, scraper_id: str, request: ScraperHubRun | None = None) -> ScrapeJobResponse:
77
+ return self._submit_run(
78
+ ScraperRoute.SCRAPER_HUB_RUN_CREATE.format(scraper_id=_path_id(scraper_id)),
79
+ request or {},
80
+ )
81
+
82
+ def run_managed(
83
+ self,
84
+ scraper: ManagedScraper[_Inputs],
85
+ inputs: _Inputs,
86
+ *,
87
+ configuration: ScraperHubRunConfiguration | None = None,
88
+ idempotency_key: str | None = None,
89
+ url: str | None = None,
90
+ ) -> ScrapeJobResponse:
91
+ """Submit typed inputs with the revision pinned by the installed catalog."""
92
+ return self._submit_run(
93
+ ScraperRoute.SCRAPER_HUB_RUN_CREATE.format(scraper_id=_path_id(scraper.id)),
94
+ _managed_params(scraper, inputs, configuration, idempotency_key, url),
95
+ )
96
+
97
+ def run_and_wait(
98
+ self,
99
+ scraper_id: str,
100
+ request: ScraperHubRun | None = None,
101
+ *,
102
+ timeout: float | None = None,
103
+ poll_interval: float | None = None,
104
+ ) -> ScrapeJobResponse:
105
+ run = self.run(scraper_id, request)
106
+ if _run_done(cast(ObjectResponse, run)):
107
+ return run
108
+ return self._runs.wait(run["id"], timeout=timeout, poll_interval=poll_interval)
109
+
110
+
111
+ class AsyncScraperHub(_AsyncScraperEndpoints):
112
+ def __init__(
113
+ self, transport: AsyncTransport, config: ScraperConfig, runs: AsyncScraperRuns
114
+ ) -> None:
115
+ super().__init__(transport, config)
116
+ self._runs = runs
117
+
118
+ async def list(
119
+ self,
120
+ *,
121
+ scope: HubScope = "all",
122
+ category: str | None = None,
123
+ search: str | None = None,
124
+ ) -> ScraperHubResponse:
125
+ return await self._request(
126
+ HTTPMethod.GET,
127
+ ScraperRoute.SCRAPER_HUB_LIST,
128
+ ScraperHubResponse,
129
+ params={"scope": scope, "category": category, "search": search},
130
+ )
131
+
132
+ async def get(self, scraper_id: str) -> ScraperHubItemResponse:
133
+ path = ScraperRoute.SCRAPER_HUB_RETRIEVE.format(scraper_id=_path_id(scraper_id))
134
+ return await self._request(HTTPMethod.GET, path, ScraperHubItemResponse)
135
+
136
+ async def run(self, scraper_id: str, request: ScraperHubRun | None = None) -> ScrapeJobResponse:
137
+ return await self._submit_run(
138
+ ScraperRoute.SCRAPER_HUB_RUN_CREATE.format(scraper_id=_path_id(scraper_id)),
139
+ request or {},
140
+ )
141
+
142
+ async def run_managed(
143
+ self,
144
+ scraper: ManagedScraper[_Inputs],
145
+ inputs: _Inputs,
146
+ *,
147
+ configuration: ScraperHubRunConfiguration | None = None,
148
+ idempotency_key: str | None = None,
149
+ url: str | None = None,
150
+ ) -> ScrapeJobResponse:
151
+ """Submit typed inputs with the revision pinned by the installed catalog."""
152
+ return await self._submit_run(
153
+ ScraperRoute.SCRAPER_HUB_RUN_CREATE.format(scraper_id=_path_id(scraper.id)),
154
+ _managed_params(scraper, inputs, configuration, idempotency_key, url),
155
+ )
156
+
157
+ async def run_and_wait(
158
+ self,
159
+ scraper_id: str,
160
+ request: ScraperHubRun | None = None,
161
+ *,
162
+ timeout: float | None = None,
163
+ poll_interval: float | None = None,
164
+ ) -> ScrapeJobResponse:
165
+ run = await self.run(scraper_id, request)
166
+ if _run_done(cast(ObjectResponse, run)):
167
+ return run
168
+ return await self._runs.wait(run["id"], timeout=timeout, poll_interval=poll_interval)
@@ -0,0 +1,132 @@
1
+ """Scraper workload health, monitoring scopes, and alert policies."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from http import HTTPMethod
6
+
7
+ from ._generated.wire import ScraperRoute
8
+ from ._request import _AsyncScraperEndpoints, _SyncScraperEndpoints
9
+ from .types import (
10
+ ScraperMonitoring,
11
+ ScraperMonitoringScopes,
12
+ ScraperMonitoringScopeType,
13
+ ScraperMonitoringWindow,
14
+ ScraperMonitorPolicy,
15
+ UpdateScraperMonitorPolicyParams,
16
+ )
17
+
18
+
19
+ class ScraperMonitoringResource(_SyncScraperEndpoints):
20
+ """Scraper workload health, monitoring scopes, and alert policies."""
21
+
22
+ def get(
23
+ self,
24
+ *,
25
+ scope_type: ScraperMonitoringScopeType | None = None,
26
+ scope_id: str | None = None,
27
+ window: ScraperMonitoringWindow | None = None,
28
+ ) -> ScraperMonitoring:
29
+ return self._request(
30
+ HTTPMethod.GET,
31
+ ScraperRoute.SCRAPER_MONITORING_RETRIEVE,
32
+ ScraperMonitoring,
33
+ params={"scope_type": scope_type, "scope_id": scope_id, "window": window},
34
+ )
35
+
36
+ def list_scopes(self) -> ScraperMonitoringScopes:
37
+ return self._request(
38
+ HTTPMethod.GET, ScraperRoute.SCRAPER_MONITORING_SCOPES_LIST, ScraperMonitoringScopes
39
+ )
40
+
41
+ def get_policy(
42
+ self, *, scope_type: ScraperMonitoringScopeType | None = None, scope_id: str | None = None
43
+ ) -> ScraperMonitorPolicy:
44
+ return self._request(
45
+ HTTPMethod.GET,
46
+ ScraperRoute.SCRAPER_MONITOR_POLICY_RETRIEVE,
47
+ ScraperMonitorPolicy,
48
+ params={"scope_type": scope_type, "scope_id": scope_id},
49
+ )
50
+
51
+ def update_policy(
52
+ self,
53
+ request: UpdateScraperMonitorPolicyParams,
54
+ *,
55
+ scope_type: ScraperMonitoringScopeType | None = None,
56
+ scope_id: str | None = None,
57
+ ) -> ScraperMonitorPolicy:
58
+ return self._request(
59
+ HTTPMethod.PATCH,
60
+ ScraperRoute.SCRAPER_MONITOR_POLICY_UPDATE,
61
+ ScraperMonitorPolicy,
62
+ body=request,
63
+ params={"scope_type": scope_type, "scope_id": scope_id},
64
+ )
65
+
66
+ def evaluate_policy(
67
+ self, *, scope_type: ScraperMonitoringScopeType | None = None, scope_id: str | None = None
68
+ ) -> ScraperMonitorPolicy:
69
+ return self._request(
70
+ HTTPMethod.POST,
71
+ ScraperRoute.SCRAPER_MONITOR_POLICY_EVALUATE,
72
+ ScraperMonitorPolicy,
73
+ params={"scope_type": scope_type, "scope_id": scope_id},
74
+ )
75
+
76
+
77
+ class AsyncScraperMonitoringResource(_AsyncScraperEndpoints):
78
+ """Scraper workload health, monitoring scopes, and alert policies."""
79
+
80
+ async def get(
81
+ self,
82
+ *,
83
+ scope_type: ScraperMonitoringScopeType | None = None,
84
+ scope_id: str | None = None,
85
+ window: ScraperMonitoringWindow | None = None,
86
+ ) -> ScraperMonitoring:
87
+ return await self._request(
88
+ HTTPMethod.GET,
89
+ ScraperRoute.SCRAPER_MONITORING_RETRIEVE,
90
+ ScraperMonitoring,
91
+ params={"scope_type": scope_type, "scope_id": scope_id, "window": window},
92
+ )
93
+
94
+ async def list_scopes(self) -> ScraperMonitoringScopes:
95
+ return await self._request(
96
+ HTTPMethod.GET, ScraperRoute.SCRAPER_MONITORING_SCOPES_LIST, ScraperMonitoringScopes
97
+ )
98
+
99
+ async def get_policy(
100
+ self, *, scope_type: ScraperMonitoringScopeType | None = None, scope_id: str | None = None
101
+ ) -> ScraperMonitorPolicy:
102
+ return await self._request(
103
+ HTTPMethod.GET,
104
+ ScraperRoute.SCRAPER_MONITOR_POLICY_RETRIEVE,
105
+ ScraperMonitorPolicy,
106
+ params={"scope_type": scope_type, "scope_id": scope_id},
107
+ )
108
+
109
+ async def update_policy(
110
+ self,
111
+ request: UpdateScraperMonitorPolicyParams,
112
+ *,
113
+ scope_type: ScraperMonitoringScopeType | None = None,
114
+ scope_id: str | None = None,
115
+ ) -> ScraperMonitorPolicy:
116
+ return await self._request(
117
+ HTTPMethod.PATCH,
118
+ ScraperRoute.SCRAPER_MONITOR_POLICY_UPDATE,
119
+ ScraperMonitorPolicy,
120
+ body=request,
121
+ params={"scope_type": scope_type, "scope_id": scope_id},
122
+ )
123
+
124
+ async def evaluate_policy(
125
+ self, *, scope_type: ScraperMonitoringScopeType | None = None, scope_id: str | None = None
126
+ ) -> ScraperMonitorPolicy:
127
+ return await self._request(
128
+ HTTPMethod.POST,
129
+ ScraperRoute.SCRAPER_MONITOR_POLICY_EVALUATE,
130
+ ScraperMonitorPolicy,
131
+ params={"scope_type": scope_type, "scope_id": scope_id},
132
+ )