Skip to content

Commit 12f5cb2

Browse files
committed
update to wstd to remove Reactor
1 parent cf8b84b commit 12f5cb2

File tree

5 files changed

+68
-72
lines changed

5 files changed

+68
-72
lines changed

.gitmodules

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
[submodule "lib/WAVS"]
55
path = lib/WAVS
66
url = https://github.com/Lay3rLabs/WAVS
7-
tag = v0.3.0-alpha4
7+
# tag = v0.3.0-alpha4
8+
commit = af1ac23629b7b1e41b28a7f367224395af2b9d55

Cargo.lock

Lines changed: 25 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ rust-version = "1.80.0"
1818
[workspace.dependencies]
1919
bindings = { path = "crates/bindings" }
2020
wit-bindgen-rt = {version = "0.37.0", features = ["bitflags"]}
21-
wstd = "0.4.0"
21+
wstd = "0.5.1"
22+
wasi = "0.14.0"
2223
serde = { version = "1.0.211", features = ["derive"] }
2324
serde_json = "1.0.127"
2425
# layer WAVS imports
@@ -43,6 +44,5 @@ tendermint = { version = "0.40.1", default-features = false}
4344
tendermint-rpc = "0.40.1"
4445
tower-service = "0.3.3"
4546
url = "2.5.3"
46-
wasi = "0.13.3"
4747
wasmtime-wasi = { version = "24.0.0", default-features = true }
4848
wit-bindgen = "0.37.0"

components/eth-trigger-weather/src/lib.rs

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ mod trigger;
22
use layer_wasi::{
33
bindings::world::{Guest, TriggerAction},
44
export_layer_trigger_world,
5-
wasi::{Request, WasiPollable},
65
};
76
use serde::{Deserialize, Serialize};
87
use trigger::{decode_trigger_event, encode_trigger_output};
9-
use wstd::runtime::{block_on, Reactor};
8+
use wstd::{
9+
http::{Client, Request},
10+
io::{empty, AsyncRead},
11+
runtime::block_on,
12+
};
1013

1114
struct Component;
1215

@@ -24,16 +27,15 @@ impl Guest for Component {
2427
let api_key = std::env::var("WAVS_ENV_OPEN_WEATHER_API_KEY")
2528
.or(Err("missing env var `WAVS_ENV_OPEN_WEATHER_API_KEY`".to_string()))?;
2629

27-
let res = block_on(move |reactor| async move {
28-
let loc: Result<LocDataNested, String> =
29-
get_location(&reactor, api_key.clone(), input).await;
30+
let res = block_on(async move {
31+
let loc: Result<LocDataNested, String> = get_location(api_key.clone(), input).await;
3032

3133
let location = match loc {
3234
Ok(data) => data,
3335
Err(e) => return Err(e),
3436
};
3537

36-
let weather_data = get_weather(&reactor, location, api_key).await;
38+
let weather_data = get_weather(location, api_key).await;
3739

3840
match weather_data {
3941
Ok(data) => {
@@ -51,47 +53,37 @@ impl Guest for Component {
5153
}
5254
}
5355

54-
async fn get_location(
55-
reactor: &Reactor,
56-
app_key: String,
57-
loc_input: &str,
58-
) -> Result<LocDataNested, String> {
56+
async fn get_location(app_key: String, loc_input: &str) -> Result<LocDataNested, String> {
5957
let url: &str = "http://api.openweathermap.org/geo/1.0/direct";
6058
let loc_input_formatted = format!("{},US", loc_input);
6159
let params = [("q", loc_input_formatted.as_str()), ("appid", app_key.as_str())];
6260

6361
let url_with_params = reqwest::Url::parse_with_params(url, &params).unwrap();
64-
let mut req = Request::get(url_with_params.as_str())?;
65-
req.headers = vec![
66-
("Accept".to_string(), "application/json".to_string()),
67-
("Content-Type".to_string(), "application/json".to_string()),
68-
];
62+
let req = Request::get(url_with_params.as_str())
63+
.header("Accept", "application/json")
64+
.header("Content-Type", "application/json")
65+
.body(empty())
66+
.unwrap();
6967

70-
let response = reactor.send(req).await;
68+
let response = Client::new().send(req).await;
7169

7270
match response {
73-
Ok(response) => {
74-
let finalresp = response.json::<LocationData>().map_err(|e| {
75-
let resp_body = response.body;
76-
let resp_str = String::from_utf8_lossy(&resp_body);
77-
format!(
78-
"Error debugging location response to JSON. Error: {:?}. had response: {:?} | using URL: {:?}",
79-
e, resp_str, url_with_params,
80-
)
81-
})?;
82-
return Ok(finalresp[0].clone());
71+
Ok(mut response) => {
72+
let mut body_buf = Vec::new();
73+
response.body_mut().read_to_end(&mut body_buf).await.unwrap();
74+
75+
let resp = String::from_utf8_lossy(&body_buf);
76+
let json: LocationData = serde_json::from_str(&resp).unwrap();
77+
78+
return Ok(json[0].clone());
8379
}
8480
Err(e) => {
8581
return Err(e.to_string());
8682
}
8783
}
8884
}
8985

90-
async fn get_weather(
91-
reactor: &Reactor,
92-
location: LocDataNested,
93-
app_key: String,
94-
) -> Result<WeatherResponse, String> {
86+
async fn get_weather(location: LocDataNested, app_key: String) -> Result<WeatherResponse, String> {
9587
let url: &str = "https://api.openweathermap.org/data/2.5/weather";
9688
let params = [
9789
("lat", location.lat.to_string()),
@@ -101,25 +93,23 @@ async fn get_weather(
10193
];
10294

10395
let url_with_params = reqwest::Url::parse_with_params(url, &params).unwrap();
104-
let mut req = Request::get(url_with_params.as_str())?;
105-
req.headers = vec![
106-
("Accept".to_string(), "application/json".to_string()),
107-
("Content-Type".to_string(), "application/json".to_string()),
108-
];
96+
let req = Request::get(url_with_params.as_str())
97+
.header("Accept", "application/json")
98+
.header("Content-Type", "application/json")
99+
.body(empty())
100+
.unwrap();
109101

110-
let response = reactor.send(req).await;
102+
let response = Client::new().send(req).await;
111103

112104
match response {
113-
Ok(response) => {
114-
let finalresp = response.json::<WeatherResponse>().map_err(|e| {
115-
let resp_body = response.body;
116-
let resp_str = String::from_utf8_lossy(&resp_body);
117-
format!(
118-
"Error debugging weather response to JSON. Error: {:?}. had response: {:?} | using URL: {:?}",
119-
e, resp_str, url_with_params,
120-
)
121-
})?;
122-
return Ok(finalresp);
105+
Ok(mut response) => {
106+
let mut body_buf = Vec::new();
107+
response.body_mut().read_to_end(&mut body_buf).await.unwrap();
108+
109+
let resp = String::from_utf8_lossy(&body_buf);
110+
let json: WeatherResponse = serde_json::from_str(&resp).unwrap();
111+
112+
return Ok(json);
123113
}
124114
Err(e) => {
125115
return Err(e.to_string());

0 commit comments

Comments
 (0)