-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync_native_type.rs
49 lines (42 loc) · 1.05 KB
/
async_native_type.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use hebi::prelude::*;
#[tokio::main]
async fn main() {
struct Client {
inner: reqwest::Client,
}
impl Client {
fn new() -> Self {
Self {
inner: reqwest::Client::new(),
}
}
async fn get(scope: Scope<'_>, this: This<'_, Client>) -> hebi::Result<String> {
let url = scope.param::<Str>(0)?;
let request = this.inner.get(url.as_str());
let response = request.send().await.map_err(hebi::Error::user)?;
let bytes = response.bytes().await.map_err(hebi::Error::user)?.to_vec();
let str = String::from_utf8(bytes).map_err(hebi::Error::user)?;
Ok(str)
}
}
let module = NativeModule::builder("http")
.class::<Client>("Client", |class| {
class
.init(|_| Ok(Client::new()))
.async_method("get", Client::get)
.finish()
})
.finish();
let mut hebi = Hebi::new();
hebi.register(&module);
hebi
.eval_async(
r#"
import http
c := http.Client()
print c.get("https://jsonplaceholder.typicode.com/todos/1")
"#,
)
.await
.unwrap();
}