|
| 1 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The SFC licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import typing |
| 19 | +from dataclasses import dataclass |
| 20 | + |
| 21 | +from selenium.webdriver.common.bidi.cdp import import_devtools |
| 22 | + |
| 23 | +from . import browsing_context |
| 24 | +from . import script |
| 25 | +from .bidi import BidiCommand |
| 26 | +from .bidi import BidiEvent |
| 27 | +from .bidi import BidiObject |
| 28 | + |
| 29 | +devtools = import_devtools("") |
| 30 | +event_class = devtools.util.event_class |
| 31 | + |
| 32 | +InterceptPhase = typing.Literal["beforeRequestSent", "responseStarted", "authRequired"] |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class UrlPatternPattern(BidiObject): |
| 37 | + type_: typing.Literal["pattern"] = "pattern" |
| 38 | + protocol: typing.Optional[str] = None |
| 39 | + hostname: typing.Optional[str] = None |
| 40 | + port: typing.Optional[str] = None |
| 41 | + pathname: typing.Optional[str] = None |
| 42 | + search: typing.Optional[str] = None |
| 43 | + |
| 44 | + |
| 45 | +@dataclass |
| 46 | +class UrlPatternString(BidiObject): |
| 47 | + pattern: str |
| 48 | + type_: typing.Literal["string"] = "string" |
| 49 | + |
| 50 | + |
| 51 | +UrlPattern = typing.Union[UrlPatternPattern, UrlPatternString] |
| 52 | + |
| 53 | + |
| 54 | +@dataclass |
| 55 | +class AddInterceptParameters(BidiObject): |
| 56 | + phases: typing.List[InterceptPhase] |
| 57 | + contexts: typing.Optional[typing.List[browsing_context.BrowsingContext]] = None |
| 58 | + urlPatterns: typing.Optional[typing.List[UrlPattern]] = None |
| 59 | + |
| 60 | + |
| 61 | +@dataclass |
| 62 | +class AddIntercept(BidiCommand): |
| 63 | + params: AddInterceptParameters |
| 64 | + method: typing.Literal["network.addIntercept"] = "network.addIntercept" |
| 65 | + |
| 66 | + |
| 67 | +Request = str |
| 68 | + |
| 69 | + |
| 70 | +@dataclass |
| 71 | +class StringValue(BidiObject): |
| 72 | + value: str |
| 73 | + type_: typing.Literal["string"] = "string" |
| 74 | + |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class Base64Value(BidiObject): |
| 78 | + value: str |
| 79 | + type_: typing.Literal["base64"] = "base64" |
| 80 | + |
| 81 | + |
| 82 | +BytesValue = typing.Union[StringValue, Base64Value] |
| 83 | + |
| 84 | + |
| 85 | +@dataclass |
| 86 | +class CookieHeader(BidiObject): |
| 87 | + name: str |
| 88 | + value: BytesValue |
| 89 | + |
| 90 | + |
| 91 | +@dataclass |
| 92 | +class Header(BidiObject): |
| 93 | + name: str |
| 94 | + value: BytesValue |
| 95 | + |
| 96 | + |
| 97 | +@dataclass |
| 98 | +class ContinueRequestParameters(BidiObject): |
| 99 | + request: Request |
| 100 | + body: typing.Optional[BytesValue] = None |
| 101 | + cookies: typing.Optional[typing.List[CookieHeader]] = None |
| 102 | + headers: typing.Optional[typing.List[Header]] = None |
| 103 | + method: typing.Optional[str] = None |
| 104 | + url: typing.Optional[str] = None |
| 105 | + |
| 106 | + |
| 107 | +@dataclass |
| 108 | +class ContinueRequest(BidiCommand): |
| 109 | + params: ContinueRequestParameters |
| 110 | + method: typing.Literal["network.continueRequest"] = "network.continueRequest" |
| 111 | + |
| 112 | + |
| 113 | +Intercept = str |
| 114 | + |
| 115 | + |
| 116 | +@dataclass |
| 117 | +class RemoveInterceptParameters(BidiObject): |
| 118 | + intercept: Intercept |
| 119 | + |
| 120 | + |
| 121 | +@dataclass |
| 122 | +class RemoveIntercept(BidiCommand): |
| 123 | + params: RemoveInterceptParameters |
| 124 | + method: typing.Literal["network.removeIntercept"] = "network.removeIntercept" |
| 125 | + |
| 126 | + |
| 127 | +SameSite = typing.Literal["strict", "lax", "none"] |
| 128 | + |
| 129 | + |
| 130 | +@dataclass |
| 131 | +class Cookie(BidiObject): |
| 132 | + name: str |
| 133 | + value: BytesValue |
| 134 | + domain: str |
| 135 | + path: str |
| 136 | + size: int |
| 137 | + httpOnly: bool |
| 138 | + secure: bool |
| 139 | + sameSite: SameSite |
| 140 | + expiry: typing.Optional[int] = None |
| 141 | + |
| 142 | + |
| 143 | +@dataclass |
| 144 | +class FetchTimingInfo(BidiObject): |
| 145 | + timeOrigin: float |
| 146 | + requestTime: float |
| 147 | + redirectStart: float |
| 148 | + redirectEnd: float |
| 149 | + fetchStart: float |
| 150 | + dnsStart: float |
| 151 | + dnsEnd: float |
| 152 | + connectStart: float |
| 153 | + connectEnd: float |
| 154 | + tlsStart: float |
| 155 | + requestStart: float |
| 156 | + responseStart: float |
| 157 | + responseEnd: float |
| 158 | + |
| 159 | + |
| 160 | +@dataclass |
| 161 | +class RequestData(BidiObject): |
| 162 | + request: Request |
| 163 | + url: str |
| 164 | + method: str |
| 165 | + headersSize: int |
| 166 | + timings: FetchTimingInfo |
| 167 | + headers: typing.Optional[typing.List[Header]] = None |
| 168 | + cookies: typing.Optional[typing.List[Cookie]] = None |
| 169 | + bodySize: typing.Optional[int] = None |
| 170 | + |
| 171 | + |
| 172 | +@dataclass |
| 173 | +class Initiator(BidiObject): |
| 174 | + type_: typing.Literal["parser", "script", "preflight", "other"] |
| 175 | + columnNumber: typing.Optional[int] = None |
| 176 | + lineNumber: typing.Optional[int] = None |
| 177 | + stackTrace: typing.Optional[script.StackTrace] = None |
| 178 | + request: typing.Optional[Request] = None |
| 179 | + |
| 180 | + |
| 181 | +@dataclass |
| 182 | +class BeforeRequestSentParameters(BidiObject): |
| 183 | + isBlocked: bool |
| 184 | + redirectCount: int |
| 185 | + request: RequestData |
| 186 | + timestamp: int |
| 187 | + initiator: Initiator |
| 188 | + context: typing.Optional[browsing_context.BrowsingContext] = None |
| 189 | + navigation: typing.Optional[browsing_context.Navigation] = None |
| 190 | + intercepts: typing.Optional[typing.List[Intercept]] = None |
| 191 | + |
| 192 | + |
| 193 | +@dataclass |
| 194 | +@event_class("network.beforeRequestSent") |
| 195 | +class BeforeRequestSent(BidiEvent): |
| 196 | + params: BeforeRequestSentParameters |
| 197 | + method: typing.Literal["network.beforeRequestSent"] = "network.beforeRequestSent" |
| 198 | + |
| 199 | + |
| 200 | +class Network: |
| 201 | + def __init__(self, conn): |
| 202 | + self.conn = conn |
| 203 | + |
| 204 | + async def add_intercept(self, params: AddInterceptParameters): |
| 205 | + result = await self.conn.execute(AddIntercept(params).cmd()) |
| 206 | + return result |
| 207 | + |
| 208 | + async def continue_request(self, params: ContinueRequestParameters): |
| 209 | + result = await self.conn.execute(ContinueRequest(params).cmd()) |
| 210 | + return result |
| 211 | + |
| 212 | + async def remove_intercept(self, params: RemoveInterceptParameters): |
| 213 | + await self.conn.execute(RemoveIntercept(params).cmd()) |
0 commit comments