_fetch.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. # Copyright (c) Microsoft Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import base64
  15. import json
  16. import pathlib
  17. import typing
  18. from pathlib import Path
  19. from typing import Any, Dict, List, Optional, Union, cast
  20. import playwright._impl._network as network
  21. from playwright._impl._api_structures import (
  22. ClientCertificate,
  23. FilePayload,
  24. FormField,
  25. Headers,
  26. HttpCredentials,
  27. ProxySettings,
  28. ServerFilePayload,
  29. StorageState,
  30. )
  31. from playwright._impl._connection import ChannelOwner, from_channel
  32. from playwright._impl._errors import is_target_closed_error
  33. from playwright._impl._helper import (
  34. Error,
  35. NameValue,
  36. TargetClosedError,
  37. TimeoutSettings,
  38. async_readfile,
  39. async_writefile,
  40. is_file_payload,
  41. locals_to_params,
  42. object_to_array,
  43. to_impl,
  44. )
  45. from playwright._impl._network import serialize_headers, to_client_certificates_protocol
  46. from playwright._impl._tracing import Tracing
  47. if typing.TYPE_CHECKING:
  48. from playwright._impl._playwright import Playwright
  49. FormType = Dict[str, Union[bool, float, str]]
  50. DataType = Union[Any, bytes, str]
  51. MultipartType = Dict[str, Union[bytes, bool, float, str, FilePayload]]
  52. ParamsType = Union[Dict[str, Union[bool, float, str]], str]
  53. class APIRequest:
  54. def __init__(self, playwright: "Playwright") -> None:
  55. self.playwright = playwright
  56. self._loop = playwright._loop
  57. self._dispatcher_fiber = playwright._connection._dispatcher_fiber
  58. async def new_context(
  59. self,
  60. baseURL: str = None,
  61. extraHTTPHeaders: Dict[str, str] = None,
  62. httpCredentials: HttpCredentials = None,
  63. ignoreHTTPSErrors: bool = None,
  64. proxy: ProxySettings = None,
  65. userAgent: str = None,
  66. timeout: float = None,
  67. storageState: Union[StorageState, str, Path] = None,
  68. clientCertificates: List[ClientCertificate] = None,
  69. failOnStatusCode: bool = None,
  70. maxRedirects: int = None,
  71. ) -> "APIRequestContext":
  72. params = locals_to_params(locals())
  73. if "storageState" in params:
  74. storage_state = params["storageState"]
  75. if not isinstance(storage_state, dict) and storage_state:
  76. params["storageState"] = json.loads(
  77. (await async_readfile(storage_state)).decode()
  78. )
  79. if "extraHTTPHeaders" in params:
  80. params["extraHTTPHeaders"] = serialize_headers(params["extraHTTPHeaders"])
  81. params["clientCertificates"] = await to_client_certificates_protocol(
  82. params.get("clientCertificates")
  83. )
  84. context = cast(
  85. APIRequestContext,
  86. from_channel(
  87. await self.playwright._channel.send("newRequest", None, params)
  88. ),
  89. )
  90. context._timeout_settings.set_default_timeout(timeout)
  91. return context
  92. class APIRequestContext(ChannelOwner):
  93. def __init__(
  94. self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
  95. ) -> None:
  96. super().__init__(parent, type, guid, initializer)
  97. self._tracing: Tracing = from_channel(initializer["tracing"])
  98. self._close_reason: Optional[str] = None
  99. self._timeout_settings = TimeoutSettings(None)
  100. async def dispose(self, reason: str = None) -> None:
  101. self._close_reason = reason
  102. try:
  103. await self._channel.send("dispose", None, {"reason": reason})
  104. except Error as e:
  105. if is_target_closed_error(e):
  106. return
  107. raise e
  108. self._tracing._reset_stack_counter()
  109. async def delete(
  110. self,
  111. url: str,
  112. params: ParamsType = None,
  113. headers: Headers = None,
  114. data: DataType = None,
  115. form: FormType = None,
  116. multipart: MultipartType = None,
  117. timeout: float = None,
  118. failOnStatusCode: bool = None,
  119. ignoreHTTPSErrors: bool = None,
  120. maxRedirects: int = None,
  121. maxRetries: int = None,
  122. ) -> "APIResponse":
  123. return await self.fetch(
  124. url,
  125. method="DELETE",
  126. params=params,
  127. headers=headers,
  128. data=data,
  129. form=form,
  130. multipart=multipart,
  131. timeout=timeout,
  132. failOnStatusCode=failOnStatusCode,
  133. ignoreHTTPSErrors=ignoreHTTPSErrors,
  134. maxRedirects=maxRedirects,
  135. maxRetries=maxRetries,
  136. )
  137. async def head(
  138. self,
  139. url: str,
  140. params: ParamsType = None,
  141. headers: Headers = None,
  142. data: DataType = None,
  143. form: FormType = None,
  144. multipart: MultipartType = None,
  145. timeout: float = None,
  146. failOnStatusCode: bool = None,
  147. ignoreHTTPSErrors: bool = None,
  148. maxRedirects: int = None,
  149. maxRetries: int = None,
  150. ) -> "APIResponse":
  151. return await self.fetch(
  152. url,
  153. method="HEAD",
  154. params=params,
  155. headers=headers,
  156. data=data,
  157. form=form,
  158. multipart=multipart,
  159. timeout=timeout,
  160. failOnStatusCode=failOnStatusCode,
  161. ignoreHTTPSErrors=ignoreHTTPSErrors,
  162. maxRedirects=maxRedirects,
  163. maxRetries=maxRetries,
  164. )
  165. async def get(
  166. self,
  167. url: str,
  168. params: ParamsType = None,
  169. headers: Headers = None,
  170. data: DataType = None,
  171. form: FormType = None,
  172. multipart: MultipartType = None,
  173. timeout: float = None,
  174. failOnStatusCode: bool = None,
  175. ignoreHTTPSErrors: bool = None,
  176. maxRedirects: int = None,
  177. maxRetries: int = None,
  178. ) -> "APIResponse":
  179. return await self.fetch(
  180. url,
  181. method="GET",
  182. params=params,
  183. headers=headers,
  184. data=data,
  185. form=form,
  186. multipart=multipart,
  187. timeout=timeout,
  188. failOnStatusCode=failOnStatusCode,
  189. ignoreHTTPSErrors=ignoreHTTPSErrors,
  190. maxRedirects=maxRedirects,
  191. maxRetries=maxRetries,
  192. )
  193. async def patch(
  194. self,
  195. url: str,
  196. params: ParamsType = None,
  197. headers: Headers = None,
  198. data: DataType = None,
  199. form: FormType = None,
  200. multipart: Dict[str, Union[bytes, bool, float, str, FilePayload]] = None,
  201. timeout: float = None,
  202. failOnStatusCode: bool = None,
  203. ignoreHTTPSErrors: bool = None,
  204. maxRedirects: int = None,
  205. maxRetries: int = None,
  206. ) -> "APIResponse":
  207. return await self.fetch(
  208. url,
  209. method="PATCH",
  210. params=params,
  211. headers=headers,
  212. data=data,
  213. form=form,
  214. multipart=multipart,
  215. timeout=timeout,
  216. failOnStatusCode=failOnStatusCode,
  217. ignoreHTTPSErrors=ignoreHTTPSErrors,
  218. maxRedirects=maxRedirects,
  219. maxRetries=maxRetries,
  220. )
  221. async def put(
  222. self,
  223. url: str,
  224. params: ParamsType = None,
  225. headers: Headers = None,
  226. data: DataType = None,
  227. form: FormType = None,
  228. multipart: Dict[str, Union[bytes, bool, float, str, FilePayload]] = None,
  229. timeout: float = None,
  230. failOnStatusCode: bool = None,
  231. ignoreHTTPSErrors: bool = None,
  232. maxRedirects: int = None,
  233. maxRetries: int = None,
  234. ) -> "APIResponse":
  235. return await self.fetch(
  236. url,
  237. method="PUT",
  238. params=params,
  239. headers=headers,
  240. data=data,
  241. form=form,
  242. multipart=multipart,
  243. timeout=timeout,
  244. failOnStatusCode=failOnStatusCode,
  245. ignoreHTTPSErrors=ignoreHTTPSErrors,
  246. maxRedirects=maxRedirects,
  247. maxRetries=maxRetries,
  248. )
  249. async def post(
  250. self,
  251. url: str,
  252. params: ParamsType = None,
  253. headers: Headers = None,
  254. data: DataType = None,
  255. form: FormType = None,
  256. multipart: Dict[str, Union[bytes, bool, float, str, FilePayload]] = None,
  257. timeout: float = None,
  258. failOnStatusCode: bool = None,
  259. ignoreHTTPSErrors: bool = None,
  260. maxRedirects: int = None,
  261. maxRetries: int = None,
  262. ) -> "APIResponse":
  263. return await self.fetch(
  264. url,
  265. method="POST",
  266. params=params,
  267. headers=headers,
  268. data=data,
  269. form=form,
  270. multipart=multipart,
  271. timeout=timeout,
  272. failOnStatusCode=failOnStatusCode,
  273. ignoreHTTPSErrors=ignoreHTTPSErrors,
  274. maxRedirects=maxRedirects,
  275. maxRetries=maxRetries,
  276. )
  277. async def fetch(
  278. self,
  279. urlOrRequest: Union[str, network.Request],
  280. params: ParamsType = None,
  281. method: str = None,
  282. headers: Headers = None,
  283. data: DataType = None,
  284. form: FormType = None,
  285. multipart: Dict[str, Union[bytes, bool, float, str, FilePayload]] = None,
  286. timeout: float = None,
  287. failOnStatusCode: bool = None,
  288. ignoreHTTPSErrors: bool = None,
  289. maxRedirects: int = None,
  290. maxRetries: int = None,
  291. ) -> "APIResponse":
  292. url = urlOrRequest if isinstance(urlOrRequest, str) else None
  293. request = (
  294. cast(network.Request, to_impl(urlOrRequest))
  295. if isinstance(to_impl(urlOrRequest), network.Request)
  296. else None
  297. )
  298. assert request or isinstance(
  299. urlOrRequest, str
  300. ), "First argument must be either URL string or Request"
  301. return await self._inner_fetch(
  302. request,
  303. url,
  304. method,
  305. headers,
  306. data,
  307. params,
  308. form,
  309. multipart,
  310. timeout,
  311. failOnStatusCode,
  312. ignoreHTTPSErrors,
  313. maxRedirects,
  314. maxRetries,
  315. )
  316. async def _inner_fetch(
  317. self,
  318. request: Optional[network.Request],
  319. url: Optional[str],
  320. method: str = None,
  321. headers: Headers = None,
  322. data: DataType = None,
  323. params: ParamsType = None,
  324. form: FormType = None,
  325. multipart: Dict[str, Union[bytes, bool, float, str, FilePayload]] = None,
  326. timeout: float = None,
  327. failOnStatusCode: bool = None,
  328. ignoreHTTPSErrors: bool = None,
  329. maxRedirects: int = None,
  330. maxRetries: int = None,
  331. ) -> "APIResponse":
  332. if self._close_reason:
  333. raise TargetClosedError(self._close_reason)
  334. assert (
  335. (1 if data else 0) + (1 if form else 0) + (1 if multipart else 0)
  336. ) <= 1, "Only one of 'data', 'form' or 'multipart' can be specified"
  337. assert (
  338. maxRedirects is None or maxRedirects >= 0
  339. ), "'max_redirects' must be greater than or equal to '0'"
  340. assert (
  341. maxRetries is None or maxRetries >= 0
  342. ), "'max_retries' must be greater than or equal to '0'"
  343. url = url or (request.url if request else url)
  344. method = method or (request.method if request else "GET")
  345. # Cannot call allHeaders() here as the request may be paused inside route handler.
  346. headers_obj = headers or (request.headers if request else None)
  347. serialized_headers = serialize_headers(headers_obj) if headers_obj else None
  348. json_data: Any = None
  349. form_data: Optional[List[NameValue]] = None
  350. multipart_data: Optional[List[FormField]] = None
  351. post_data_buffer: Optional[bytes] = None
  352. if data is not None:
  353. if isinstance(data, str):
  354. if is_json_content_type(serialized_headers):
  355. json_data = data if is_json_parsable(data) else json.dumps(data)
  356. else:
  357. post_data_buffer = data.encode()
  358. elif isinstance(data, bytes):
  359. post_data_buffer = data
  360. elif isinstance(data, (dict, list, int, bool)):
  361. json_data = json.dumps(data)
  362. else:
  363. raise Error(f"Unsupported 'data' type: {type(data)}")
  364. elif form:
  365. form_data = object_to_array(form)
  366. elif multipart:
  367. multipart_data = []
  368. # Convert file-like values to ServerFilePayload structs.
  369. for name, value in multipart.items():
  370. if is_file_payload(value):
  371. payload = cast(FilePayload, value)
  372. assert isinstance(
  373. payload["buffer"], bytes
  374. ), f"Unexpected buffer type of 'data.{name}'"
  375. multipart_data.append(
  376. FormField(name=name, file=file_payload_to_json(payload))
  377. )
  378. elif isinstance(value, str):
  379. multipart_data.append(FormField(name=name, value=value))
  380. if (
  381. post_data_buffer is None
  382. and json_data is None
  383. and form_data is None
  384. and multipart_data is None
  385. ):
  386. post_data_buffer = request.post_data_buffer if request else None
  387. post_data = (
  388. base64.b64encode(post_data_buffer).decode() if post_data_buffer else None
  389. )
  390. response = await self._channel.send(
  391. "fetch",
  392. self._timeout_settings.timeout,
  393. {
  394. "url": url,
  395. "timeout": timeout,
  396. "params": object_to_array(params) if isinstance(params, dict) else None,
  397. "encodedParams": params if isinstance(params, str) else None,
  398. "method": method,
  399. "headers": serialized_headers,
  400. "postData": post_data,
  401. "jsonData": json_data,
  402. "formData": form_data,
  403. "multipartData": multipart_data,
  404. "failOnStatusCode": failOnStatusCode,
  405. "ignoreHTTPSErrors": ignoreHTTPSErrors,
  406. "maxRedirects": maxRedirects,
  407. "maxRetries": maxRetries,
  408. },
  409. )
  410. return APIResponse(self, response)
  411. async def storage_state(
  412. self,
  413. path: Union[pathlib.Path, str] = None,
  414. indexedDB: bool = None,
  415. ) -> StorageState:
  416. result = await self._channel.send_return_as_dict(
  417. "storageState", None, {"indexedDB": indexedDB}
  418. )
  419. if path:
  420. await async_writefile(path, json.dumps(result))
  421. return result
  422. def file_payload_to_json(payload: FilePayload) -> ServerFilePayload:
  423. return ServerFilePayload(
  424. name=payload["name"],
  425. mimeType=payload["mimeType"],
  426. buffer=base64.b64encode(payload["buffer"]).decode(),
  427. )
  428. class APIResponse:
  429. def __init__(self, context: APIRequestContext, initializer: Dict) -> None:
  430. self._loop = context._loop
  431. self._dispatcher_fiber = context._connection._dispatcher_fiber
  432. self._request = context
  433. self._initializer = initializer
  434. self._headers = network.RawHeaders(initializer["headers"])
  435. def __repr__(self) -> str:
  436. return f"<APIResponse url={self.url!r} status={self.status!r} status_text={self.status_text!r}>"
  437. @property
  438. def ok(self) -> bool:
  439. return self.status >= 200 and self.status <= 299
  440. @property
  441. def url(self) -> str:
  442. return self._initializer["url"]
  443. @property
  444. def status(self) -> int:
  445. return self._initializer["status"]
  446. @property
  447. def status_text(self) -> str:
  448. return self._initializer["statusText"]
  449. @property
  450. def headers(self) -> Headers:
  451. return self._headers.headers()
  452. @property
  453. def headers_array(self) -> network.HeadersArray:
  454. return self._headers.headers_array()
  455. async def body(self) -> bytes:
  456. try:
  457. result = await self._request._connection.wrap_api_call(
  458. lambda: self._request._channel.send_return_as_dict(
  459. "fetchResponseBody",
  460. None,
  461. {
  462. "fetchUid": self._fetch_uid,
  463. },
  464. ),
  465. True,
  466. )
  467. if result is None:
  468. raise Error("Response has been disposed")
  469. return base64.b64decode(result["binary"])
  470. except Error as exc:
  471. if is_target_closed_error(exc):
  472. raise Error("Response has been disposed")
  473. raise exc
  474. async def text(self) -> str:
  475. content = await self.body()
  476. return content.decode()
  477. async def json(self) -> Any:
  478. content = await self.text()
  479. return json.loads(content)
  480. async def dispose(self) -> None:
  481. await self._request._channel.send(
  482. "disposeAPIResponse",
  483. None,
  484. {
  485. "fetchUid": self._fetch_uid,
  486. },
  487. )
  488. @property
  489. def _fetch_uid(self) -> str:
  490. return self._initializer["fetchUid"]
  491. async def _fetch_log(self) -> List[str]:
  492. return await self._request._channel.send(
  493. "fetchLog",
  494. None,
  495. {
  496. "fetchUid": self._fetch_uid,
  497. },
  498. )
  499. def is_json_content_type(headers: network.HeadersArray = None) -> bool:
  500. if not headers:
  501. return False
  502. for header in headers:
  503. if header["name"] == "Content-Type":
  504. return header["value"].startswith("application/json")
  505. return False
  506. def is_json_parsable(value: Any) -> bool:
  507. if not isinstance(value, str):
  508. return False
  509. try:
  510. json.loads(value)
  511. return True
  512. except json.JSONDecodeError:
  513. return False