forked from brightdata/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_unblocker.py
More file actions
216 lines (181 loc) · 7.49 KB
/
async_unblocker.py
File metadata and controls
216 lines (181 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Async unblocker client for non-blocking requests.
This client handles Bright Data's async unblocker endpoints which support
both SERP and Web Unlocker services in non-blocking mode.
Endpoints:
- POST /unblocker/req → Triggers async request, returns x-response-id header
- GET /unblocker/get_result → Polls for results (202 pending, 200 ready)
Key Design Decisions:
- customer_id is OPTIONAL for both SERP and Web Unlocker (derived from bearer token)
- Uses AsyncEngine for all HTTP operations (reuses auth, rate limiting)
- Simple status model: "ready", "pending", or "error"
- Minimal abstraction - just wraps the two endpoints
Performance Note:
- SERP async: ~3 seconds response time
- Web Unlocker async: ~145 seconds response time (sync mode is faster!)
- See devdocs/web_unlocker_async_inspection.md for details
"""
from typing import Optional, Any
from ..core.engine import AsyncEngine
from ..exceptions import APIError
class AsyncUnblockerClient:
"""
Client for async unblocker endpoints.
Supports both SERP and Web Unlocker async modes using:
- POST /unblocker/req → returns x-response-id header
- GET /unblocker/get_result → polls for results
Example:
>>> async with AsyncEngine(token) as engine:
... client = AsyncUnblockerClient(engine)
...
... # Trigger async request
... response_id = await client.trigger(
... zone="my_zone",
... url="https://example.com"
... )
...
... # Poll until ready
... while True:
... status = await client.get_status(zone="my_zone", response_id=response_id)
... if status == "ready":
... data = await client.fetch_result(zone="my_zone", response_id=response_id)
... break
... elif status == "error":
... break
... await asyncio.sleep(2)
"""
TRIGGER_ENDPOINT = "/unblocker/req"
FETCH_ENDPOINT = "/unblocker/get_result"
def __init__(self, engine: AsyncEngine):
"""
Initialize async unblocker client.
Args:
engine: AsyncEngine instance with bearer token auth
"""
self.engine = engine
async def trigger(
self,
zone: str,
url: str,
customer: Optional[str] = None,
**kwargs, # Additional params like country, format, etc.
) -> Optional[str]:
"""
Trigger async unblocker request.
Args:
zone: Zone name (e.g., "serp_api4", "unblocker_zone")
url: Target URL to scrape/search
customer: Customer ID (optional, derived from bearer token if not provided)
**kwargs: Additional request parameters (e.g., country, format, method)
Returns:
response_id from x-response-id header, or None if trigger failed
Note:
customer_id is optional for both SERP and Web Unlocker.
Example:
>>> response_id = await client.trigger(
... zone="my_serp_zone",
... url="https://www.google.com/search?q=test&brd_json=1"
... )
"""
params = {"zone": zone}
# Add customer to query params if provided
if customer:
params["customer"] = customer
payload = {"url": url}
# Merge additional params into payload
payload.update(kwargs)
async with self.engine.post_to_url(
f"{self.engine.BASE_URL}{self.TRIGGER_ENDPOINT}", params=params, json_data=payload
) as response:
# Extract response_id from x-response-id header
# Note: This is different from datasets API which returns snapshot_id in body
response_id = response.headers.get("x-response-id")
return response_id
async def get_status(self, zone: str, response_id: str, customer: Optional[str] = None) -> str:
"""
Check if response is ready.
Args:
zone: Zone name
response_id: Response ID from trigger()
customer: Customer ID (optional, derived from bearer token if not provided)
Returns:
- "ready" if HTTP 200 (results available)
- "pending" if HTTP 202 (still processing)
- "error" for any other status
Example:
>>> status = await client.get_status(
... zone="my_zone",
... response_id="s4w7t1767082074477rtu2rth43mk8",
... customer="hl_67e5ed38"
... )
>>> if status == "ready":
... # Fetch results
"""
params = {"zone": zone, "response_id": response_id}
# Add customer to query params if provided
if customer:
params["customer"] = customer
async with self.engine.get_from_url(
f"{self.engine.BASE_URL}{self.FETCH_ENDPOINT}", params=params
) as response:
if response.status == 200:
return "ready"
elif response.status == 202:
return "pending"
else:
# Any other status (4xx, 5xx) is treated as error
return "error"
async def fetch_result(
self,
zone: str,
response_id: str,
response_format: str = "json",
customer: Optional[str] = None,
) -> Any:
"""
Fetch results when ready.
Important: Only call this when get_status() returns "ready".
If called while still pending, will raise APIError.
Args:
zone: Zone name
response_id: Response ID from trigger()
response_format: How to parse response - "json" or "raw" (default: "json")
customer: Customer ID (optional, derived from bearer token if not provided)
Returns:
Response data (parsed JSON dict/list or raw text string)
Raises:
APIError: If response not ready (HTTP 202) or fetch fails
Example:
>>> # SERP results (JSON)
>>> data = await client.fetch_result(
... zone="my_serp_zone",
... response_id="s4w7t1767082074477rtu2rth43mk8",
... response_format="json"
... )
>>> # Web Unlocker HTML (raw text)
>>> html = await client.fetch_result(
... zone="my_web_zone",
... response_id="s4w7t1767082074477rtu2rth43mk8",
... response_format="raw",
... customer="hl_67e5ed38"
... )
"""
params = {"zone": zone, "response_id": response_id}
# Add customer to query params if provided
if customer:
params["customer"] = customer
async with self.engine.get_from_url(
f"{self.engine.BASE_URL}{self.FETCH_ENDPOINT}", params=params
) as response:
if response.status == 200:
# Success - parse based on format
if response_format == "json":
return await response.json()
else:
return await response.text()
elif response.status == 202:
# Still pending - caller should have checked status first
raise APIError("Response not ready yet (HTTP 202). Check status before fetching.")
else:
# Error occurred
error_text = await response.text()
raise APIError(f"Fetch failed (HTTP {response.status}): {error_text}")