mokr.network.manager.chrome

Module Contents

Classes

ChromeNetworkManager

Class to handle requests in a given mokr.browser.Page.

Attributes

LOGGER

mokr.network.manager.chrome.LOGGER
class mokr.network.manager.chrome.ChromeNetworkManager(page: mokr.browser.page.Page, client: mokr.connection.DevtoolsConnection, frame_manager: mokr.frame.FrameManager, interception_callback_chain: list[Callable])

Bases: mokr.network.manager.base.NetworkManager

Class to handle requests in a given mokr.browser.Page.

This class relies on network events to be emitted, and stores events and even requests to handle the various possible chains of events.

The interception_callback_chain this class receives from the parent mokr.browser.Page is shared with spawned mokr.network.Request objects. While the request interception callback chain is run from the Request object, it is initiated during select events from this class.

There are four possible orders that requests will be triggered and thus handled by NetworkManager:

  • _on_request_will_be_sent.

  • _on_request_will_be_sent, _on_request_paused.

  • _on_request_paused, _on_request_will_be_sent.

  • _on_request_paused, _on_request_will_be_sent,

    _on_request_paused, _on_request_will_be_sent, _on_request_paused, _on_request_paused (see crbug.com/1196004).

There are a few different ways requests need to be handled due to this: - For _on_request we need the event from _on_request_will_be_sent and optionally the fetch_request_id (event[“requestId”] also known as “interceptionId”) from _on_request_paused. - If request interception is disabled, call _on_request once per call to _on_request_will_be_sent. - If request interception is enabled, call _on_request once per call to _on_request_paused (once per fetch_request_id). - Events are stored via the mokr.network.NetworkEventManager to allow for subsequent events to call _on_request. - Chains of redirect requests have the same requestId as the original request. This means events can be received in multiple orders for different requests in the same redirect chain such as:

  • _on_request_will_be_sent, _on_request_will_be_sent, …

  • _on_request_will_be_sent, _on_request_paused,

    _on_request_will_be_sent, _on_request_paused, …

  • _on_request_will_be_sent, _on_request_paused,

    _on_request_paused, _on_request_will_be_sent, …

  • _on_request_paused, _on_request_will_be_sent,

    _on_request_paused, _on_request_will_be_sent, _on_request_paused, _on_request_will_be_sent, _on_request_paused, _on_request_paused, … (see crbug.com/1196004)

Parameters:
  • page (Page) – Parent mokr.browser.Page.

  • client (DevtoolsConnection) – A mokr.connection.DevtoolsConnection spawned by the parent mokr.browser.Page.

  • frame_manager (FrameManager) – The mokr.frame.FrameManager from the mokr.browser.Page that spawned this element.

  • interception_callback_chain (list[Callable]) – A list of callbacks to be passed into new mokr.network.Request`s that will be run during "request" event interception, by that object. Inherited from the parent `mokr.browser.Page.

property extra_http_headers: dict[str, str]

Any extra HTTP headers assigned to this class. These are sent with every request this manager handles.

forget_request(request: mokr.network.request.Request, events: bool) None

Drop a request from the mokr.network.NetworkEventManager and remove from attempted authentications listing, if relevant.

Optionally forget all events tied to the remote request networkID, too.

Parameters:
  • request (Request) – mokr.request.Request to forget.

  • events (bool) – If True, drop all events stored for the request, too.

get_in_flight_requests_count() int

Get the number of active requests (not resolved). Wraps mokr.network.NetworkEventManager.get_in_flight_requests_count.

Returns:

Number of active requests.

Return type:

int

async set_credentials(credentials: dict[Literal[username, password], str]) None

Set the credentials to use for HTTP authentication.

Parameters:

credentials (dict[str, str]) – A dictionary with credentials, keyed as “username” and “password”.

async set_extra_http_headers(extra_http_headers: dict[str, str]) None

Set extra headers to be sent with every request.

Parameters:

extra_http_headers (dict[str, str]) – A dictionary of headers.

Raises:

ValueError – Raised if any header value is not string.

async set_request_interception(choice: bool) None

Disable or enabled request interception (enabled by default). See mokr.browser.Page.on for details on using the request interception callback chain.

Parameters:

choice (bool) – False to disable, True to enable.