"""
This file is generated by generator/run.py based on the Chrome DevTools Protocol.

Network domain allows tracking network activities of the page. It exposes information about http,
file, data and other requests and responses, their headers, bodies, timing, etc.
https://chromedevtools.github.io/devtools-protocol/tot/Network/

***************************************************
                    Events
***************************************************
"""
from __future__ import annotations

from typing import Literal

from cdpkit.protocol._types import (
    Network,
    Page,
)
from cdpkit.protocol.base import CDPEvent


class DataReceived(CDPEvent):
    """ Fired when data chunk was received over the network. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    dataLength: int
    encodedDataLength: int
    data: str | None = None  # experimental


class EventSourceMessageReceived(CDPEvent):
    """ Fired when EventSource message is received. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    eventName: str
    eventId: str
    data: str


class LoadingFailed(CDPEvent):
    """ Fired when HTTP request has failed to load. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    type: Network.ResourceType
    errorText: str
    canceled: bool | None = None
    blockedReason: Network.BlockedReason | None = None
    corsErrorStatus: Network.CorsErrorStatus | None = None


class LoadingFinished(CDPEvent):
    """ Fired when HTTP request has finished loading. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    encodedDataLength: float


class RequestIntercepted(CDPEvent):
    """ Details of an intercepted HTTP request, which must be either allowed, blocked, modified or
    mocked.
    Deprecated, use Fetch.requestPaused instead. """

    interceptionId: Network.InterceptionId
    request: Network.Request
    frameId: Page.FrameId
    resourceType: Network.ResourceType
    isNavigationRequest: bool
    isDownload: bool | None = None
    redirectUrl: str | None = None
    authChallenge: Network.AuthChallenge | None = None
    responseErrorReason: Network.ErrorReason | None = None
    responseStatusCode: int | None = None
    responseHeaders: Network.Headers | None = None
    requestId: Network.RequestId | None = None


class RequestServedFromCache(CDPEvent):
    """ Fired if request ended up loading from cache. """

    requestId: Network.RequestId


class RequestWillBeSent(CDPEvent):
    """ Fired when page is about to send HTTP request. """

    requestId: Network.RequestId
    loaderId: Network.LoaderId
    documentURL: str
    request: Network.Request
    timestamp: Network.MonotonicTime
    wallTime: Network.TimeSinceEpoch
    initiator: Network.Initiator
    redirectHasExtraInfo: bool | None = None  # experimental
    redirectResponse: Network.Response | None = None
    type: Network.ResourceType | None = None
    frameId: Page.FrameId | None = None
    hasUserGesture: bool | None = None
    renderBlockingBehavior: Network.RenderBlockingBehavior | None = None  # experimental


class ResourceChangedPriority(CDPEvent):
    """ Fired when resource loading priority is changed """

    requestId: Network.RequestId
    newPriority: Network.ResourcePriority
    timestamp: Network.MonotonicTime


class SignedExchangeReceived(CDPEvent):
    """ Fired when a signed exchange was received over the network """

    requestId: Network.RequestId
    info: Network.SignedExchangeInfo


class ResponseReceived(CDPEvent):
    """ Fired when HTTP response is available. """

    requestId: Network.RequestId
    loaderId: Network.LoaderId
    timestamp: Network.MonotonicTime
    type: Network.ResourceType
    response: Network.Response
    hasExtraInfo: bool | None = None  # experimental
    frameId: Page.FrameId | None = None


class WebSocketClosed(CDPEvent):
    """ Fired when WebSocket is closed. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime


class WebSocketCreated(CDPEvent):
    """ Fired upon WebSocket creation. """

    requestId: Network.RequestId
    url: str
    initiator: Network.Initiator | None = None


class WebSocketFrameError(CDPEvent):
    """ Fired when WebSocket message error occurs. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    errorMessage: str


class WebSocketFrameReceived(CDPEvent):
    """ Fired when WebSocket message is received. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    response: Network.WebSocketFrame


class WebSocketFrameSent(CDPEvent):
    """ Fired when WebSocket message is sent. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    response: Network.WebSocketFrame


class WebSocketHandshakeResponseReceived(CDPEvent):
    """ Fired when WebSocket handshake response becomes available. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    response: Network.WebSocketResponse


class WebSocketWillSendHandshakeRequest(CDPEvent):
    """ Fired when WebSocket is about to initiate handshake. """

    requestId: Network.RequestId
    timestamp: Network.MonotonicTime
    wallTime: Network.TimeSinceEpoch
    request: Network.WebSocketRequest


class WebTransportCreated(CDPEvent):
    """ Fired upon WebTransport creation. """

    transportId: Network.RequestId
    url: str
    timestamp: Network.MonotonicTime
    initiator: Network.Initiator | None = None


class WebTransportConnectionEstablished(CDPEvent):
    """ Fired when WebTransport handshake is finished. """

    transportId: Network.RequestId
    timestamp: Network.MonotonicTime


class WebTransportClosed(CDPEvent):
    """ Fired when WebTransport is disposed. """

    transportId: Network.RequestId
    timestamp: Network.MonotonicTime


class DirectTCPSocketCreated(CDPEvent):
    """ Fired upon direct_socket.TCPSocket creation. """

    identifier: Network.RequestId
    remoteAddr: str
    remotePort: int
    options: Network.DirectTCPSocketOptions
    timestamp: Network.MonotonicTime
    initiator: Network.Initiator | None = None


class DirectTCPSocketOpened(CDPEvent):
    """ Fired when direct_socket.TCPSocket connection is opened. """

    identifier: Network.RequestId
    remoteAddr: str
    remotePort: int
    timestamp: Network.MonotonicTime
    localAddr: str | None = None
    localPort: int | None = None


class DirectTCPSocketAborted(CDPEvent):
    """ Fired when direct_socket.TCPSocket is aborted. """

    identifier: Network.RequestId
    errorMessage: str
    timestamp: Network.MonotonicTime


class DirectTCPSocketClosed(CDPEvent):
    """ Fired when direct_socket.TCPSocket is closed. """

    identifier: Network.RequestId
    timestamp: Network.MonotonicTime


class DirectTCPSocketChunkSent(CDPEvent):
    """ Fired when data is sent to tcp direct socket stream. """

    identifier: Network.RequestId
    data: str
    timestamp: Network.MonotonicTime


class DirectTCPSocketChunkReceived(CDPEvent):
    """ Fired when data is received from tcp direct socket stream. """

    identifier: Network.RequestId
    data: str
    timestamp: Network.MonotonicTime


class DirectUDPSocketJoinedMulticastGroup(CDPEvent):

    identifier: Network.RequestId
    IPAddress: str


class DirectUDPSocketLeftMulticastGroup(CDPEvent):

    identifier: Network.RequestId
    IPAddress: str


class DirectUDPSocketCreated(CDPEvent):
    """ Fired upon direct_socket.UDPSocket creation. """

    identifier: Network.RequestId
    options: Network.DirectUDPSocketOptions
    timestamp: Network.MonotonicTime
    initiator: Network.Initiator | None = None


class DirectUDPSocketOpened(CDPEvent):
    """ Fired when direct_socket.UDPSocket connection is opened. """

    identifier: Network.RequestId
    localAddr: str
    localPort: int
    timestamp: Network.MonotonicTime
    remoteAddr: str | None = None
    remotePort: int | None = None


class DirectUDPSocketAborted(CDPEvent):
    """ Fired when direct_socket.UDPSocket is aborted. """

    identifier: Network.RequestId
    errorMessage: str
    timestamp: Network.MonotonicTime


class DirectUDPSocketClosed(CDPEvent):
    """ Fired when direct_socket.UDPSocket is closed. """

    identifier: Network.RequestId
    timestamp: Network.MonotonicTime


class DirectUDPSocketChunkSent(CDPEvent):
    """ Fired when message is sent to udp direct socket stream. """

    identifier: Network.RequestId
    message: Network.DirectUDPMessage
    timestamp: Network.MonotonicTime


class DirectUDPSocketChunkReceived(CDPEvent):
    """ Fired when message is received from udp direct socket stream. """

    identifier: Network.RequestId
    message: Network.DirectUDPMessage
    timestamp: Network.MonotonicTime


class RequestWillBeSentExtraInfo(CDPEvent):
    """ Fired when additional information about a requestWillBeSent event is available from the
    network stack. Not every requestWillBeSent event will have an additional
    requestWillBeSentExtraInfo fired for it, and there is no guarantee whether requestWillBeSent
    or requestWillBeSentExtraInfo will be fired first for the same request. """

    requestId: Network.RequestId
    associatedCookies: list[Network.AssociatedCookie]
    headers: Network.Headers
    connectTiming: Network.ConnectTiming | None = None  # experimental
    clientSecurityState: Network.ClientSecurityState | None = None
    siteHasCookieInOtherPartition: bool | None = None
    appliedNetworkConditionsId: str | None = None


class ResponseReceivedExtraInfo(CDPEvent):
    """ Fired when additional information about a responseReceived event is available from the network
    stack. Not every responseReceived event will have an additional responseReceivedExtraInfo for
    it, and responseReceivedExtraInfo may be fired before or after responseReceived. """

    requestId: Network.RequestId
    blockedCookies: list[Network.BlockedSetCookieWithReason]
    headers: Network.Headers
    resourceIPAddressSpace: Network.IPAddressSpace
    statusCode: int
    headersText: str | None = None
    cookiePartitionKey: Network.CookiePartitionKey | None = None  # experimental
    cookiePartitionKeyOpaque: bool | None = None
    exemptedCookies: list[Network.ExemptedSetCookieWithReason] | None = None


class ResponseReceivedEarlyHints(CDPEvent):
    """ Fired when 103 Early Hints headers is received in addition to the common response.
    Not every responseReceived event will have an responseReceivedEarlyHints fired.
    Only one responseReceivedEarlyHints may be fired for eached responseReceived event. """

    requestId: Network.RequestId
    headers: Network.Headers


class TrustTokenOperationDone(CDPEvent):
    """ Fired exactly once for each Trust Token operation. Depending on
    the type of the operation and whether the operation succeeded or
    failed, the event is fired before the corresponding request was sent
    or after the response was received. """

    status: Literal['Ok', 'InvalidArgument', 'MissingIssuerKeys', 'FailedPrecondition', 'ResourceExhausted', 'AlreadyExists', 'ResourceLimited', 'Unauthorized', 'BadResponse', 'InternalError', 'UnknownError', 'FulfilledLocally', 'SiteIssuerLimit']
    type: Network.TrustTokenOperationType
    requestId: Network.RequestId
    topLevelOrigin: str | None = None
    issuerOrigin: str | None = None
    issuedTokenCount: int | None = None


class PolicyUpdated(CDPEvent):
    """ Fired once security policy has been updated. """

    ...


class ReportingApiReportAdded(CDPEvent):
    """ Is sent whenever a new report is added.
    And after 'enableReportingApi' for all existing reports. """

    report: Network.ReportingApiReport


class ReportingApiReportUpdated(CDPEvent):

    report: Network.ReportingApiReport


class ReportingApiEndpointsChangedForOrigin(CDPEvent):

    origin: str
    endpoints: list[Network.ReportingApiEndpoint]
