MCPcopy Index your code
hub / github.com/RustPython/RustPython / fetch

Function fetch

crates/wasm/src/browser_module.rs:57–111  ·  view source on GitHub ↗
(url: PyStrRef, args: FetchArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

55
56 #[pyfunction]
57 fn fetch(url: PyStrRef, args: FetchArgs, vm: &VirtualMachine) -> PyResult {
58 let FetchArgs {
59 response_format,
60 method,
61 headers,
62 body,
63 content_type,
64 } = args;
65
66 let response_format = match response_format {
67 Some(s) => FetchResponseFormat::from_str(vm, s.expect_str())?,
68 None => FetchResponseFormat::Text,
69 };
70
71 let opts = web_sys::RequestInit::new();
72
73 opts.set_method(method.as_ref().map_or("GET", |s| s.expect_str()));
74
75 if let Some(body) = body {
76 opts.set_body(&convert::py_to_js(vm, body));
77 }
78
79 let request = web_sys::Request::new_with_str_and_init(url.expect_str(), &opts)
80 .map_err(|err| convert::js_py_typeerror(vm, err))?;
81
82 if let Some(headers) = headers {
83 let h = request.headers();
84 for (key, value) in headers {
85 let key = key.str(vm)?;
86 let value = value.str(vm)?;
87 h.set(key.expect_str(), value.expect_str())
88 .map_err(|err| convert::js_py_typeerror(vm, err))?;
89 }
90 }
91
92 if let Some(content_type) = content_type {
93 request
94 .headers()
95 .set("Content-Type", content_type.expect_str())
96 .map_err(|err| convert::js_py_typeerror(vm, err))?;
97 }
98
99 let window = window();
100 let request_prom = window.fetch_with_request(&request);
101
102 let future = async move {
103 let val = JsFuture::from(request_prom).await?;
104 let response = val
105 .dyn_into::<web_sys::Response>()
106 .expect("val to be of type Response");
107 JsFuture::from(response_format.get_response(&response)?).await
108 };
109
110 Ok(PyPromise::from_future(future).into_pyobject(vm))
111 }
112
113 #[pyfunction]
114 fn request_animation_frame(func: ArgCallable, vm: &VirtualMachine) -> PyResult {

Callers 3

main.pyFile · 0.90
fetch.pyFile · 0.90
genericFetchFunction · 0.85

Calls 11

newFunction · 0.85
py_to_jsFunction · 0.85
js_py_typeerrorFunction · 0.85
windowFunction · 0.85
expect_strMethod · 0.80
headersMethod · 0.80
get_responseMethod · 0.80
as_refMethod · 0.45
strMethod · 0.45
setMethod · 0.45
into_pyobjectMethod · 0.45

Tested by

no test coverage detected