eoka_server/
browser_fetch.rs1use eoka::{BoundedFetchRequest, Page};
2use eoka_protocol::{ErrorDetail, FetchArgs, Response};
3
4pub async fn fetch(page: &Page, args: FetchArgs) -> Response {
5 let mut request = BoundedFetchRequest::get(args.url);
6 request.body = args.body;
7 if let Some(method) = args.method {
8 request.method = method;
9 }
10 if let Some(redirect) = args.redirect {
11 request.redirect = redirect;
12 }
13 if let Some(max_bytes) = args.max_body {
14 request.max_bytes = max_bytes;
15 }
16 if let Some(headers) = args.headers {
17 match serde_json::from_value(headers) {
18 Ok(headers) => request.headers = headers,
19 Err(_) => {
20 return Response::err_detail(ErrorDetail::new(
21 "invalid_input",
22 "Headers must be an object of string values",
23 ))
24 }
25 }
26 }
27 let result = match page.fetch_bounded(&request).await {
28 Ok(result) => result,
29 Err(error) => {
30 return Response::err_detail(
31 ErrorDetail::new("fetch_failed", error.to_string()).retryable(false),
32 )
33 }
34 };
35 if args.body_only {
36 if result.body_omitted.as_deref() == Some("binary") {
37 return Response::err_detail(ErrorDetail::new(
38 "fetch_binary_body",
39 "Binary body omitted; use eoka download --url or --click with --out",
40 ));
41 }
42 if !result.truncated {
43 return Response::ok_text(result.body);
44 }
45 }
46 let value = match serde_json::to_value(result) {
47 Ok(value) => value,
48 Err(error) => return Response::err(error.to_string()),
49 };
50 if args.body_only {
51 let mut response = Response::err_detail(ErrorDetail::new(
52 "fetch_body_truncated",
53 "Body-only output exceeded max_body; increase the bounded limit or inspect JSON output",
54 ));
55 response.data = Some(value);
56 return response;
57 }
58 Response::ok(value)
59}