MCPcopy Create free account
hub / github.com/apify/crawlee-python / _get_client

Method _get_client

src/crawlee/http_clients/_httpx.py:283–323  ·  view source on GitHub ↗

Retrieve or create an HTTP client for the given proxy URL. If a client for the specified proxy URL does not exist, create and store a new one.

(self, proxy_url: str | None)

Source from the content-addressed store, hash-verified

281 )
282
283 def _get_client(self, proxy_url: str | None) -> httpx.AsyncClient:
284 """Retrieve or create an HTTP client for the given proxy URL.
285
286 If a client for the specified proxy URL does not exist, create and store a new one.
287 """
288 if not self._transport:
289 # Configure connection pool limits and keep-alive connections for transport
290 limits = self._async_client_kwargs.get(
291 'limits', httpx.Limits(max_connections=1000, max_keepalive_connections=200)
292 )
293
294 self._transport = _HttpxTransport(
295 http1=self._http1,
296 http2=self._http2,
297 verify=self._ssl_context,
298 limits=limits,
299 )
300
301 if proxy_url not in self._client_by_proxy_url:
302 # Prepare a default kwargs for the new client.
303 kwargs: dict[str, Any] = {
304 'proxy': proxy_url,
305 'http1': self._http1,
306 'http2': self._http2,
307 'follow_redirects': True,
308 }
309
310 # Update the default kwargs with any additional user-provided kwargs.
311 kwargs.update(self._async_client_kwargs)
312
313 kwargs.update(
314 {
315 'transport': self._transport,
316 'verify': self._ssl_context,
317 }
318 )
319
320 client = httpx.AsyncClient(**kwargs)
321 self._client_by_proxy_url[proxy_url] = client
322
323 return self._client_by_proxy_url[proxy_url]
324
325 def _combine_headers(self, explicit_headers: HttpHeaders | None) -> HttpHeaders | None:
326 """Merge default headers with explicit headers for an HTTP request.

Callers 3

crawlMethod · 0.95
send_requestMethod · 0.95
streamMethod · 0.95

Calls 1

_HttpxTransportClass · 0.85

Tested by

no test coverage detected