embedded_wg_display/runtime/host_api/
http.rs1#![doc = "Implementation of the `http` WIT interface.\n\n```wit"]
2#![doc = include_str!("wit/http.wit")]
3#![doc = "```"]
4
5use crate::runtime::WidgetState;
6use crate::runtime::http_sync::HttpRequest;
7use crate::runtime::http_sync::http_request_sync;
8use crate::runtime::widget::widget::http;
9use alloc::string::String;
10use alloc::vec::Vec;
11use defmt::info;
12
13impl http::Host for WidgetState {
14 fn request(
16 &mut self,
17 method: http::Method,
18 url: String,
19 body: Option<Vec<u8>>,
20 ) -> Result<http::Response, ()> {
21 info!("HTTP host function called");
22
23 let request_method = match method {
24 http::Method::Get => reqwless::request::Method::GET,
25 http::Method::Post => reqwless::request::Method::POST,
26 http::Method::Put => reqwless::request::Method::PUT,
27 http::Method::Delete => reqwless::request::Method::DELETE,
28 http::Method::Head => reqwless::request::Method::HEAD,
29 };
30
31 let response = http_request_sync(HttpRequest {
32 method: request_method,
33 url,
34 body,
35 });
36
37 match response {
38 Ok(resp) => {
39 info!(
40 "HTTP request successful: status={}, bytes={}",
41 resp.status,
42 resp.bytes.len()
43 );
44 Ok(resp)
45 }
46 Err(_) => {
47 info!("HTTP request failed");
48 Err(())
49 }
50 }
51 }
52}