Send a request using WebAssembly JavaScript Promise Integration to wrap the asynchronous JavaScript fetch api (experimental). :param request: Request to send :param streaming: Whether to stream the response :return: The response object :rtype: EmscriptenRe
(
request: EmscriptenRequest, streaming: bool
)
| 546 | |
| 547 | |
| 548 | def send_jspi_request( |
| 549 | request: EmscriptenRequest, streaming: bool |
| 550 | ) -> EmscriptenResponse: |
| 551 | """ |
| 552 | Send a request using WebAssembly JavaScript Promise Integration |
| 553 | to wrap the asynchronous JavaScript fetch api (experimental). |
| 554 | |
| 555 | :param request: |
| 556 | Request to send |
| 557 | |
| 558 | :param streaming: |
| 559 | Whether to stream the response |
| 560 | |
| 561 | :return: The response object |
| 562 | :rtype: EmscriptenResponse |
| 563 | """ |
| 564 | timeout = request.timeout |
| 565 | js_abort_controller = js.AbortController.new() |
| 566 | headers = {k: v for k, v in request.headers.items() if k not in HEADERS_TO_IGNORE} |
| 567 | req_body = request.body |
| 568 | fetch_data = { |
| 569 | "headers": headers, |
| 570 | "body": to_js(req_body), |
| 571 | "method": request.method, |
| 572 | "signal": js_abort_controller.signal, |
| 573 | } |
| 574 | # Node.js returns the whole response (unlike opaqueredirect in browsers), |
| 575 | # so urllib3 can set `redirect: manual` to control redirects itself. |
| 576 | # https://stackoverflow.com/a/78524615 |
| 577 | if _is_node_js(): |
| 578 | fetch_data["redirect"] = "manual" |
| 579 | # Call JavaScript fetch (async api, returns a promise) |
| 580 | fetcher_promise_js = js.fetch(request.url, _obj_from_dict(fetch_data)) |
| 581 | # Now suspend WebAssembly until we resolve that promise |
| 582 | # or time out. |
| 583 | response_js = _run_sync_with_timeout( |
| 584 | fetcher_promise_js, |
| 585 | timeout, |
| 586 | js_abort_controller, |
| 587 | request=request, |
| 588 | response=None, |
| 589 | ) |
| 590 | headers = {} |
| 591 | header_iter = response_js.headers.entries() |
| 592 | while True: |
| 593 | iter_value_js = header_iter.next() |
| 594 | if getattr(iter_value_js, "done", False): |
| 595 | break |
| 596 | else: |
| 597 | headers[str(iter_value_js.value[0])] = str(iter_value_js.value[1]) |
| 598 | status_code = response_js.status |
| 599 | body: bytes | io.RawIOBase = b"" |
| 600 | |
| 601 | response = EmscriptenResponse( |
| 602 | status_code=status_code, headers=headers, body=b"", request=request |
| 603 | ) |
| 604 | if streaming: |
| 605 | # get via inputstream |
no test coverage detected