Skip to content

FastRequests

fastreq.client.FastRequests

Main client for parallel HTTP requests.

The client owns one concurrency gate and one token bucket. A task acquires a rate token before occupying a concurrency slot.

Parameters:

Name Type Description Default
backend BackendName | str

Backend to use ("auto", "niquests", "httpx", "curl_cffi")

'auto'
concurrency int

Maximum number of concurrent requests

20
max_retries int

Maximum retry attempts per request

3
rate_limit float | None

Requests per second (None for no limit)

None
rate_limit_burst int

Burst size for rate limiter

5
http2 bool

Enable HTTP/2 (if supported by backend)

True
impersonate str | None

Browser impersonation target for the curl_cffi backend (e.g. "chrome", "chrome131", "random"; None disables it). When set, user-agent rotation is disabled automatically because curl_cffi supplies the browser-matching User-Agent itself.

None
follow_redirects bool

Follow HTTP redirects

True
verify_ssl bool

Verify SSL certificates

True
timeout float | None

Default timeout per request (seconds)

None
cookies dict[str, str] | None

Initial session cookies

None
random_user_agent bool

Rotate user agents

True
random_proxy bool

Enable proxy rotation (requires proxy config)

False
proxies list[str] | None

List of proxy URLs for rotation

None
proxy_selection ProxySelection | str

Selection strategy ("round_robin" or "random")

ROUND_ROBIN
proxy_cooldown float

Seconds before retrying a failed proxy

60.0
webshare_file str | None

Path to a Webshare proxy text file

None
headers dict[str, str] | None

Default headers applied to all requests

None
debug bool

Enable the fastreq loguru namespace (never touches global sinks)

False
verbose bool

Enable verbose output (stored; progress-bar control)

True
return_none_on_failure bool

Return None instead of raising on failure

False
Source code in fastreq/client.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
class FastRequests:
    """Main client for parallel HTTP requests.

    The client owns one concurrency gate and one token bucket. A task
    acquires a rate token before occupying a concurrency slot.

    Args:
        backend: Backend to use ("auto", "niquests", "httpx", "curl_cffi")
        concurrency: Maximum number of concurrent requests
        max_retries: Maximum retry attempts per request
        rate_limit: Requests per second (None for no limit)
        rate_limit_burst: Burst size for rate limiter
        http2: Enable HTTP/2 (if supported by backend)
        impersonate: Browser impersonation target for the curl_cffi backend
            (e.g. "chrome", "chrome131", "random"; None disables it).
            When set, user-agent rotation is disabled automatically because
            curl_cffi supplies the browser-matching User-Agent itself.
        follow_redirects: Follow HTTP redirects
        verify_ssl: Verify SSL certificates
        timeout: Default timeout per request (seconds)
        cookies: Initial session cookies
        random_user_agent: Rotate user agents
        random_proxy: Enable proxy rotation (requires proxy config)
        proxies: List of proxy URLs for rotation
        proxy_selection: Selection strategy ("round_robin" or "random")
        proxy_cooldown: Seconds before retrying a failed proxy
        webshare_file: Path to a Webshare proxy text file
        headers: Default headers applied to all requests
        debug: Enable the fastreq loguru namespace (never touches global sinks)
        verbose: Enable verbose output (stored; progress-bar control)
        return_none_on_failure: Return None instead of raising on failure
    """

    def __init__(
        self,
        backend: BackendName | str = "auto",
        *,
        concurrency: int = 20,
        max_retries: int = 3,
        rate_limit: float | None = None,
        rate_limit_burst: int = 5,
        http2: bool = True,
        impersonate: str | None = None,
        follow_redirects: bool = True,
        verify_ssl: bool = True,
        timeout: float | None = None,
        cookies: dict[str, str] | None = None,
        random_user_agent: bool = True,
        random_proxy: bool = False,
        proxies: list[str] | None = None,
        proxy_selection: ProxySelection | str = ProxySelection.ROUND_ROBIN,
        proxy_cooldown: float = 60.0,
        webshare_file: str | None = None,
        headers: dict[str, str] | None = None,
        debug: bool = False,
        verbose: bool = True,
        return_none_on_failure: bool = False,
    ) -> None:
        # Validate free-proxy-related removed parameters
        # (caught early with migration guidance)

        self.backend_name = backend
        self.impersonate = impersonate
        self.concurrency = concurrency
        self.follow_redirects = follow_redirects
        self.verify_ssl = verify_ssl
        self.timeout = timeout
        # Browser impersonation provides its own browser-matching User-Agent;
        # rotating a mismatched UA would contradict the TLS fingerprint.
        self.random_user_agent = random_user_agent if impersonate is None else False
        self.random_proxy = random_proxy
        self.debug = debug
        self.verbose = verbose
        self.return_none_on_failure = return_none_on_failure

        # Library hygiene: a constructor must never reconfigure the host
        # app's global loguru sinks (logger.remove() would destroy them).
        # debug=True only opts the fastreq namespace back in; full sink
        # control belongs to fastreq.utils.logging.configure_logging.
        if debug:
            logger.enable("fastreq")

        self._backend: Backend | None = None
        self._cookies: dict[str, str] = cookies.copy() if cookies else {}
        self._rate_limiter: AsyncRateLimiter | None = None
        self._header_manager = HeaderManager(random_user_agent=self.random_user_agent)
        self._default_headers = headers or {}

        retry_config = RetryConfig(max_retries=max_retries)
        self._retry_strategy = RetryStrategy(retry_config)

        self._concurrency_semaphore = asyncio.Semaphore(concurrency)

        if rate_limit is not None:
            rate_limit_config = RateLimitConfig(
                requests_per_second=rate_limit,
                burst=rate_limit_burst,
            )
            self._rate_limiter = AsyncRateLimiter(rate_limit_config)

        self._http2 = http2

        # Build proxy pool if proxies or random_proxy with proxy list is provided
        self._proxy_pool: ProxyPool | None = None
        pool_proxies: list[str] = []

        # Webshare file import
        if webshare_file:
            from .utils.proxies import load_webshare_from_file

            pool_proxies.extend(load_webshare_from_file(webshare_file))

        # Explicit proxy list
        if proxies:
            pool_proxies.extend(proxies)

        # FASTREQ_PROXIES environment variable
        pool_proxies_from_env = ProxyPool.from_env()
        if pool_proxies_from_env.count() > 0:
            pool_proxies.extend(pool_proxies_from_env.proxies)

        if pool_proxies or random_proxy:
            selection = (
                ProxySelection(proxy_selection)
                if isinstance(proxy_selection, str)
                else proxy_selection
            )
            self._proxy_pool = ProxyPool(
                proxies=pool_proxies,
                config=ProxyPoolConfig(
                    selection=selection,
                    cooldown=proxy_cooldown,
                ),
            )

        self._select_backend()

    def _select_backend(self) -> None:
        """Create the transport using the typed factory."""
        self._backend = _create_backend(
            backend=self.backend_name,  # type: ignore[arg-type]
            http2=self._http2,
            impersonate=self.impersonate,
        )
        logger.info(f"Using backend: {self._backend.name}")

    async def __aenter__(self) -> FastRequests:
        if self._backend:
            await self._backend.__aenter__()
        return self

    async def __aexit__(self, *args: Any) -> None:
        if self._backend:
            await self._backend.__aexit__(*args)

    async def close(self) -> None:
        """Close backend session and cleanup resources."""
        if self._backend:
            await self._backend.close()

    def reset_cookies(self) -> None:
        """Clear all session cookies."""
        self._cookies = {}

    def set_cookies(self, cookies: dict[str, str]) -> None:
        """Add cookies to the session.

        Args:
            cookies: Dictionary of cookies to add (updates existing cookies)
        """
        self._cookies.update(cookies)

    @staticmethod
    @contextlib.asynccontextmanager
    async def _null_context() -> AsyncGenerator[None, None]:
        """A null async context manager to replace rate limiting when disabled."""
        yield

    @overload
    async def request(
        self,
        urls: str,
        *,
        method: str = ...,
        params: dict[str, Any] | None = ...,
        data: Any = ...,
        json: Any = ...,
        headers: dict[str, str] | None = ...,
        timeout: float | None = ...,
        proxy: str | None = ...,
        return_type: ReturnType | str = ...,
        follow_redirects: bool | None = ...,
        verify_ssl: bool | None = ...,
        parse_func: Callable[[Any], T] | None = ...,
        stream_callback: Callable[[bytes], Any] | None = ...,
        progress: ProgressOption = ...,
        progress_callback: ProgressCallback | None = ...,
        keys: None = ...,
    ) -> Any: ...

    @overload
    async def request(
        self,
        urls: list[str],
        *,
        method: str = ...,
        params: dict[str, Any] | None = ...,
        data: Any = ...,
        json: Any = ...,
        headers: dict[str, str] | None = ...,
        timeout: float | None = ...,
        proxy: str | None = ...,
        return_type: ReturnType | str = ...,
        follow_redirects: bool | None = ...,
        verify_ssl: bool | None = ...,
        parse_func: Callable[[Any], T] | None = ...,
        stream_callback: Callable[[bytes], Any] | None = ...,
        progress: ProgressOption = ...,
        progress_callback: ProgressCallback | None = ...,
        keys: list[str] = ...,
    ) -> dict[str, Any]: ...

    @overload
    async def request(
        self,
        urls: list[str],
        *,
        method: str = ...,
        params: dict[str, Any] | None = ...,
        data: Any = ...,
        json: Any = ...,
        headers: dict[str, str] | None = ...,
        timeout: float | None = ...,
        proxy: str | None = ...,
        return_type: ReturnType | str = ...,
        follow_redirects: bool | None = ...,
        verify_ssl: bool | None = ...,
        parse_func: Callable[[Any], T] | None = ...,
        stream_callback: Callable[[bytes], Any] | None = ...,
        progress: ProgressOption = ...,
        progress_callback: ProgressCallback | None = ...,
        keys: None = ...,
    ) -> list[Any]: ...

    @overload
    async def request(
        self,
        urls: str | list[str],
        *,
        method: str = ...,
        params: dict[str, Any] | None = ...,
        data: Any = ...,
        json: Any = ...,
        headers: dict[str, str] | None = ...,
        timeout: float | None = ...,
        proxy: str | None = ...,
        return_type: ReturnType | str = ...,
        follow_redirects: bool | None = ...,
        verify_ssl: bool | None = ...,
        parse_func: Callable[[Any], T] | None = ...,
        stream_callback: Callable[[bytes], Any] | None = ...,
        progress: ProgressOption = ...,
        progress_callback: ProgressCallback | None = ...,
        keys: list[str] | None = ...,
    ) -> Any: ...

    async def request(
        self,
        urls: str | list[str],
        *,
        method: str = "GET",
        params: dict[str, Any] | None = None,
        data: Any = None,
        json: Any = None,
        headers: dict[str, str] | None = None,
        timeout: float | None = None,
        proxy: str | None = None,
        return_type: ReturnType | str = ReturnType.JSON,
        follow_redirects: bool | None = None,
        verify_ssl: bool | None = None,
        parse_func: Callable[[Any], Any] | None = None,
        stream_callback: Callable[[bytes], Any] | None = None,
        progress: ProgressOption = None,
        progress_callback: ProgressCallback | None = None,
        keys: list[str] | None = None,
    ) -> Any:
        """Make parallel HTTP requests.

        Args:
            urls: Single URL or list of URLs to request.
            method: HTTP method (GET, POST, etc.).
            params: Query parameters.
            data: Request body data.
            json: JSON body (serialized automatically).
            headers: Request headers.
            timeout: Per-request timeout in seconds.
            proxy: Proxy URL (overrides proxy rotation).
            return_type: How to parse the response.
            follow_redirects: Override default follow_redirects setting.
            verify_ssl: Override default verify_ssl setting.
            parse_func: Custom function to parse each response.
            stream_callback: Callback for streaming responses (receives chunks).
            progress: Optional ``"rich"``/``"tqdm"`` progress bar, or ``True``
                to auto-select an installed optional backend.
            progress_callback: Optional callback receiving ``(completed, total)``.
            keys: Keys for dict return (must match urls length).

        Returns:
            Single URL → single result
            List of URLs → list of results
            List of URLs with keys → dict mapping keys to results
        """
        if not self._backend:
            raise ConfigurationError("Backend not initialized")

        if isinstance(return_type, str):
            return_type = ReturnType(return_type)

        effective_follow_redirects = (
            follow_redirects if follow_redirects is not None else self.follow_redirects
        )
        effective_verify_ssl = verify_ssl if verify_ssl is not None else self.verify_ssl
        effective_timeout = timeout if timeout is not None else self.timeout

        if isinstance(urls, str):
            single_url = True
            url_list: list[str] = [urls]
        else:
            single_url = False
            url_list = list(urls)

        if keys is not None and len(keys) != len(url_list):
            raise ConfigurationError(
                f"Number of keys ({len(keys)}) must match number of URLs ({len(url_list)})"
            )

        request_options = [
            RequestOptions(
                url=u,
                method=method,
                params=params,
                data=data,
                json=json,
                headers=headers,
                timeout=effective_timeout,
                proxy=proxy,
                return_type=return_type,
                stream_callback=stream_callback,
            )
            for u in url_list
        ]

        tasks = [
            self._execute_request(
                req,
                follow_redirects=effective_follow_redirects,
                verify_ssl=effective_verify_ssl,
            )
            for req in request_options
        ]

        results = await gather_with_progress(
            tasks,
            mode=progress,
            callback=progress_callback,
            description="HTTP requests",
        )

        failures: dict[str, FailureDetails] = {}
        processed_results: list[Any] = []

        for idx, result in enumerate(results):
            current_url = url_list[idx]

            if isinstance(result, Exception):
                if self.return_none_on_failure:
                    processed_results.append(None)
                else:
                    failures[current_url] = FailureDetails(url=current_url, error=result)
                    processed_results.append(result)
            else:
                if parse_func is not None:
                    result = parse_func(result)
                processed_results.append(result)

        if failures and not self.return_none_on_failure:
            raise PartialFailureError(
                f"Partial failure: {len(failures)} of {len(url_list)} requests failed",
                failures=failures,
                successes=len([r for r in results if not isinstance(r, Exception)]),
                total=len(url_list),
            )

        if single_url:
            return processed_results[0]
        elif keys is not None:
            return dict(zip(keys, processed_results, strict=True))
        else:
            return processed_results

    async def _execute_request(
        self,
        req: RequestOptions,
        *,
        follow_redirects: bool,
        verify_ssl: bool,
    ) -> Any:
        """Execute a single request with retry, rate limiting, and concurrency.

        Rate token is acquired BEFORE the concurrency slot is occupied,
        so that rate-limit-waiting requests don't block slots that could
        serve other requests.
        """
        if not self._backend:
            raise ConfigurationError("Backend not initialized")

        backend = self._backend

        async def make_request() -> NormalizedResponse:
            # Acquire rate token FIRST (before concurrency slot)
            if self._rate_limiter:
                await self._rate_limiter.acquire()

            # Then acquire concurrency slot
            async with self._concurrency_semaphore:
                # Select proxy: explicit per-request proxy > pool rotation
                selected_proxy = req.proxy
                if selected_proxy is None and self._proxy_pool:
                    selected_proxy = await self._proxy_pool.acquire()
                    if selected_proxy is None and self.random_proxy:
                        selected_proxy = None  # pool exhausted, try direct

                # Build headers with user-agent rotation
                request_headers = self._header_manager.get_headers(
                    {**self._default_headers, **(req.headers or {})}
                )

                try:
                    config = RequestConfig(
                        url=req.url,
                        method=req.method,
                        params=req.params,
                        data=req.data,
                        json=req.json,
                        headers=request_headers,
                        cookies={**self._cookies},
                        timeout=req.timeout,
                        proxy=selected_proxy,
                        http2=self._http2,
                        stream=req.return_type == ReturnType.STREAM,
                        follow_redirects=follow_redirects,
                        verify_ssl=verify_ssl,
                    )
                    response = await backend.request(
                        config,
                        stream_callback=req.stream_callback,
                    )

                    # Check for retryable status codes
                    if response.status_code in DEFAULT_RETRYABLE_STATUSES:
                        retry_after_raw = response.headers.get("retry-after", "")
                        retry_after = _parse_retry_after(retry_after_raw)
                        raise RetryableResponse(
                            f"Retryable status {response.status_code} from {req.url}",
                            status_code=response.status_code,
                            retry_after=retry_after,
                            url=req.url,
                        )

                    # Mark proxy success after a good request
                    if selected_proxy and self._proxy_pool:
                        await self._proxy_pool.mark_success(selected_proxy)

                    return response

                except RetryableResponse:
                    # Mark proxy as failed on retryable response
                    if selected_proxy and self._proxy_pool:
                        await self._proxy_pool.mark_failed(selected_proxy)
                    raise
                except Exception:
                    # Mark proxy as failed on transport error
                    if selected_proxy and self._proxy_pool:
                        await self._proxy_pool.mark_failed(selected_proxy)
                    raise

        response = await self._retry_strategy.execute(make_request)
        return self._parse_response(response, req)

    def _parse_response(self, response: NormalizedResponse, req: RequestOptions) -> Any:
        logger.debug(f"Request completed: {req.url} - Status: {response.status_code}")
        match req.return_type:
            case ReturnType.JSON:
                return response.json_data if response.is_json else None
            case ReturnType.TEXT:
                return response.text
            case ReturnType.CONTENT:
                return response.content
            case ReturnType.RESPONSE:
                return response
            case ReturnType.STREAM:
                # Chunks were already delivered to stream_callback during transport.
                # No additional callback call needed here.
                return None

close async

close() -> None

Close backend session and cleanup resources.

Source code in fastreq/client.py
async def close(self) -> None:
    """Close backend session and cleanup resources."""
    if self._backend:
        await self._backend.close()

reset_cookies

reset_cookies() -> None

Clear all session cookies.

Source code in fastreq/client.py
def reset_cookies(self) -> None:
    """Clear all session cookies."""
    self._cookies = {}

set_cookies

set_cookies(cookies: dict[str, str]) -> None

Add cookies to the session.

Parameters:

Name Type Description Default
cookies dict[str, str]

Dictionary of cookies to add (updates existing cookies)

required
Source code in fastreq/client.py
def set_cookies(self, cookies: dict[str, str]) -> None:
    """Add cookies to the session.

    Args:
        cookies: Dictionary of cookies to add (updates existing cookies)
    """
    self._cookies.update(cookies)

request async

request(
    urls: str,
    *,
    method: str = ...,
    params: dict[str, Any] | None = ...,
    data: Any = ...,
    json: Any = ...,
    headers: dict[str, str] | None = ...,
    timeout: float | None = ...,
    proxy: str | None = ...,
    return_type: ReturnType | str = ...,
    follow_redirects: bool | None = ...,
    verify_ssl: bool | None = ...,
    parse_func: Callable[[Any], T] | None = ...,
    stream_callback: Callable[[bytes], Any] | None = ...,
    progress: ProgressOption = ...,
    progress_callback: ProgressCallback | None = ...,
    keys: None = ...,
) -> Any
request(
    urls: list[str],
    *,
    method: str = ...,
    params: dict[str, Any] | None = ...,
    data: Any = ...,
    json: Any = ...,
    headers: dict[str, str] | None = ...,
    timeout: float | None = ...,
    proxy: str | None = ...,
    return_type: ReturnType | str = ...,
    follow_redirects: bool | None = ...,
    verify_ssl: bool | None = ...,
    parse_func: Callable[[Any], T] | None = ...,
    stream_callback: Callable[[bytes], Any] | None = ...,
    progress: ProgressOption = ...,
    progress_callback: ProgressCallback | None = ...,
    keys: list[str] = ...,
) -> dict[str, Any]
request(
    urls: list[str],
    *,
    method: str = ...,
    params: dict[str, Any] | None = ...,
    data: Any = ...,
    json: Any = ...,
    headers: dict[str, str] | None = ...,
    timeout: float | None = ...,
    proxy: str | None = ...,
    return_type: ReturnType | str = ...,
    follow_redirects: bool | None = ...,
    verify_ssl: bool | None = ...,
    parse_func: Callable[[Any], T] | None = ...,
    stream_callback: Callable[[bytes], Any] | None = ...,
    progress: ProgressOption = ...,
    progress_callback: ProgressCallback | None = ...,
    keys: None = ...,
) -> list[Any]
request(
    urls: str | list[str],
    *,
    method: str = ...,
    params: dict[str, Any] | None = ...,
    data: Any = ...,
    json: Any = ...,
    headers: dict[str, str] | None = ...,
    timeout: float | None = ...,
    proxy: str | None = ...,
    return_type: ReturnType | str = ...,
    follow_redirects: bool | None = ...,
    verify_ssl: bool | None = ...,
    parse_func: Callable[[Any], T] | None = ...,
    stream_callback: Callable[[bytes], Any] | None = ...,
    progress: ProgressOption = ...,
    progress_callback: ProgressCallback | None = ...,
    keys: list[str] | None = ...,
) -> Any
request(
    urls: str | list[str],
    *,
    method: str = "GET",
    params: dict[str, Any] | None = None,
    data: Any = None,
    json: Any = None,
    headers: dict[str, str] | None = None,
    timeout: float | None = None,
    proxy: str | None = None,
    return_type: ReturnType | str = ReturnType.JSON,
    follow_redirects: bool | None = None,
    verify_ssl: bool | None = None,
    parse_func: Callable[[Any], Any] | None = None,
    stream_callback: Callable[[bytes], Any] | None = None,
    progress: ProgressOption = None,
    progress_callback: ProgressCallback | None = None,
    keys: list[str] | None = None,
) -> Any

Make parallel HTTP requests.

Parameters:

Name Type Description Default
urls str | list[str]

Single URL or list of URLs to request.

required
method str

HTTP method (GET, POST, etc.).

'GET'
params dict[str, Any] | None

Query parameters.

None
data Any

Request body data.

None
json Any

JSON body (serialized automatically).

None
headers dict[str, str] | None

Request headers.

None
timeout float | None

Per-request timeout in seconds.

None
proxy str | None

Proxy URL (overrides proxy rotation).

None
return_type ReturnType | str

How to parse the response.

JSON
follow_redirects bool | None

Override default follow_redirects setting.

None
verify_ssl bool | None

Override default verify_ssl setting.

None
parse_func Callable[[Any], Any] | None

Custom function to parse each response.

None
stream_callback Callable[[bytes], Any] | None

Callback for streaming responses (receives chunks).

None
progress ProgressOption

Optional "rich"/"tqdm" progress bar, or True to auto-select an installed optional backend.

None
progress_callback ProgressCallback | None

Optional callback receiving (completed, total).

None
keys list[str] | None

Keys for dict return (must match urls length).

None

Returns:

Type Description
Any

Single URL → single result

Any

List of URLs → list of results

Any

List of URLs with keys → dict mapping keys to results

Source code in fastreq/client.py
async def request(
    self,
    urls: str | list[str],
    *,
    method: str = "GET",
    params: dict[str, Any] | None = None,
    data: Any = None,
    json: Any = None,
    headers: dict[str, str] | None = None,
    timeout: float | None = None,
    proxy: str | None = None,
    return_type: ReturnType | str = ReturnType.JSON,
    follow_redirects: bool | None = None,
    verify_ssl: bool | None = None,
    parse_func: Callable[[Any], Any] | None = None,
    stream_callback: Callable[[bytes], Any] | None = None,
    progress: ProgressOption = None,
    progress_callback: ProgressCallback | None = None,
    keys: list[str] | None = None,
) -> Any:
    """Make parallel HTTP requests.

    Args:
        urls: Single URL or list of URLs to request.
        method: HTTP method (GET, POST, etc.).
        params: Query parameters.
        data: Request body data.
        json: JSON body (serialized automatically).
        headers: Request headers.
        timeout: Per-request timeout in seconds.
        proxy: Proxy URL (overrides proxy rotation).
        return_type: How to parse the response.
        follow_redirects: Override default follow_redirects setting.
        verify_ssl: Override default verify_ssl setting.
        parse_func: Custom function to parse each response.
        stream_callback: Callback for streaming responses (receives chunks).
        progress: Optional ``"rich"``/``"tqdm"`` progress bar, or ``True``
            to auto-select an installed optional backend.
        progress_callback: Optional callback receiving ``(completed, total)``.
        keys: Keys for dict return (must match urls length).

    Returns:
        Single URL → single result
        List of URLs → list of results
        List of URLs with keys → dict mapping keys to results
    """
    if not self._backend:
        raise ConfigurationError("Backend not initialized")

    if isinstance(return_type, str):
        return_type = ReturnType(return_type)

    effective_follow_redirects = (
        follow_redirects if follow_redirects is not None else self.follow_redirects
    )
    effective_verify_ssl = verify_ssl if verify_ssl is not None else self.verify_ssl
    effective_timeout = timeout if timeout is not None else self.timeout

    if isinstance(urls, str):
        single_url = True
        url_list: list[str] = [urls]
    else:
        single_url = False
        url_list = list(urls)

    if keys is not None and len(keys) != len(url_list):
        raise ConfigurationError(
            f"Number of keys ({len(keys)}) must match number of URLs ({len(url_list)})"
        )

    request_options = [
        RequestOptions(
            url=u,
            method=method,
            params=params,
            data=data,
            json=json,
            headers=headers,
            timeout=effective_timeout,
            proxy=proxy,
            return_type=return_type,
            stream_callback=stream_callback,
        )
        for u in url_list
    ]

    tasks = [
        self._execute_request(
            req,
            follow_redirects=effective_follow_redirects,
            verify_ssl=effective_verify_ssl,
        )
        for req in request_options
    ]

    results = await gather_with_progress(
        tasks,
        mode=progress,
        callback=progress_callback,
        description="HTTP requests",
    )

    failures: dict[str, FailureDetails] = {}
    processed_results: list[Any] = []

    for idx, result in enumerate(results):
        current_url = url_list[idx]

        if isinstance(result, Exception):
            if self.return_none_on_failure:
                processed_results.append(None)
            else:
                failures[current_url] = FailureDetails(url=current_url, error=result)
                processed_results.append(result)
        else:
            if parse_func is not None:
                result = parse_func(result)
            processed_results.append(result)

    if failures and not self.return_none_on_failure:
        raise PartialFailureError(
            f"Partial failure: {len(failures)} of {len(url_list)} requests failed",
            failures=failures,
            successes=len([r for r in results if not isinstance(r, Exception)]),
            total=len(url_list),
        )

    if single_url:
        return processed_results[0]
    elif keys is not None:
        return dict(zip(keys, processed_results, strict=True))
    else:
        return processed_results