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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use uniui_core::Slot;
use crate::{
to_path,
CallError,
Kind,
Response,
};
pub trait Service {
type In: 'static + serde::de::DeserializeOwned + serde::Serialize;
type Out: 'static + serde::de::DeserializeOwned + serde::Serialize;
fn path(&self) -> &str;
fn kind(&self) -> Kind;
fn from_response(&self) -> fn(response: Response) -> Result<Self::Out, Response>;
fn exec(
&self,
parameter: &Self::In,
slot: &dyn Slot<Result<Self::Out, Response>>,
) -> Result<(), CallError> {
let p = parameter;
let s = slot.proxy();
let converter = self.from_response();
let cpa = move |code, body: String| {
let result = Response {
code,
body: Some(body.into()),
};
let data = converter(result);
s.exec_for(data);
};
return match self.kind() {
Kind::Get => {
let path = to_path(self.path(), p)?;
uni_net::get(&path, cpa)?;
Ok(())
},
Kind::Post => {
let data = serde_json::to_string(p)?;
uni_net::post_json(self.path(), &data, cpa)?;
Ok(())
},
};
}
}