Makes HTTP requests with automatic browser cookie synchronization. Cookies from the browser session are sent with API requests, and cookies from API responses are synced back to the browser. Args: driver: The WebDriver instance to sync cookies with. base_url: Optional b
| 508 | |
| 509 | |
| 510 | class APIRequestContext(_BaseRequestContext): |
| 511 | """Makes HTTP requests with automatic browser cookie synchronization. |
| 512 | |
| 513 | Cookies from the browser session are sent with API requests, and cookies |
| 514 | from API responses are synced back to the browser. |
| 515 | |
| 516 | Args: |
| 517 | driver: The WebDriver instance to sync cookies with. |
| 518 | base_url: Optional base URL prepended to relative request paths. |
| 519 | extra_headers: Optional headers included in every request. |
| 520 | timeout: Default request timeout in seconds. |
| 521 | max_redirects: Maximum number of redirects to follow. |
| 522 | fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses. |
| 523 | """ |
| 524 | |
| 525 | def __init__( |
| 526 | self, |
| 527 | driver: "WebDriver", |
| 528 | base_url: str = "", |
| 529 | extra_headers: dict[str, str] | None = None, |
| 530 | timeout: float = 30.0, |
| 531 | max_redirects: int = 10, |
| 532 | fail_on_status_code: bool = False, |
| 533 | ) -> None: |
| 534 | super().__init__( |
| 535 | base_url=base_url, |
| 536 | extra_headers=extra_headers, |
| 537 | timeout=timeout, |
| 538 | max_redirects=max_redirects, |
| 539 | fail_on_status_code=fail_on_status_code, |
| 540 | ) |
| 541 | self._driver = driver |
| 542 | |
| 543 | def new_context( |
| 544 | self, |
| 545 | base_url: str = "", |
| 546 | extra_headers: dict[str, str] | None = None, |
| 547 | storage_state: dict | str | pathlib.Path | None = None, |
| 548 | fail_on_status_code: bool = False, |
| 549 | ) -> "_IsolatedAPIRequestContext": |
| 550 | """Create an isolated API request context that does not sync with the browser. |
| 551 | |
| 552 | Args: |
| 553 | base_url: Optional base URL for this context. |
| 554 | extra_headers: Optional headers for this context. |
| 555 | storage_state: Optional cookies to pre-load, as a dict, JSON file path, or Path. |
| 556 | fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses. |
| 557 | |
| 558 | Returns: |
| 559 | An _IsolatedAPIRequestContext instance. |
| 560 | """ |
| 561 | cookies: list[dict] = [] |
| 562 | if storage_state is not None: |
| 563 | if isinstance(storage_state, (str, pathlib.Path)): |
| 564 | file_path = pathlib.Path(storage_state) |
| 565 | if not file_path.exists(): |
| 566 | raise FileNotFoundError(f"Storage state file not found: {file_path}") |
| 567 | try: |
no outgoing calls