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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::collections::HashMap;
use std::thread::sleep;
use std::time::Duration;

use reqwest::blocking::Client;
use serde::Serialize;

use nature_common::{Instance, NatureError, KeyCondition, Result};

lazy_static! {
    pub static ref CLIENT : Client = Client::new();
}

pub static URL_INPUT: &str = "http://localhost:8080/input";
pub static URL_GET_BY_ID: &str = "http://localhost:8080/get_by_id";

pub fn send_instance(ins: &Instance) -> Result<u128> {
    let response = CLIENT.post(URL_INPUT).json(ins).send();
    let id_s: String = response.unwrap().text().unwrap();
    if id_s.contains("Err") {
        return Err(NatureError::VerifyError(id_s));
    }
    serde_json::from_str(&id_s)?
}

pub fn send_business_object<T>(meta_key: &str, bo: &T) -> Result<u128> where T: Serialize {
    send_business_object_with_sys_context(meta_key, bo, &HashMap::new())
}

pub fn send_business_object_with_sys_context<T>(meta_key: &str, bo: &T, sys_context: &HashMap<String, String>) -> Result<u128> where T: Serialize {
    let mut instance = Instance::new(meta_key).unwrap();
    instance.content = serde_json::to_string(bo).unwrap();
    instance.sys_context = sys_context.clone();

    let response = CLIENT.post(URL_INPUT).json(&instance).send();
    let id_s: String = response.unwrap().text().unwrap();
    if id_s.contains("Err") {
        return Err(NatureError::VerifyError(id_s));
    }
    serde_json::from_str(&id_s)?
}

pub fn get_instance_by_id(id: u128, meta_full: &str) -> Option<Instance> {
    get_state_instance_by_id(id, meta_full, 0)
}

pub fn get_state_instance_by_id(id: u128, meta_full: &str, sta_ver: i32) -> Option<Instance> {
    info!("get state instance by id {}", &id);
    let para = KeyCondition::new(id, meta_full, "", sta_ver);
    let response = CLIENT.post(URL_GET_BY_ID).json(&para).send();
    let msg = response.unwrap().text().unwrap();
    if msg.eq(r#"{"Ok":null}"#) {
        return None;
    }
    match serde_json::from_str::<Result<Instance>>(&msg).unwrap() {
        Ok(x) => Some(x),
        Err(_) => None
    }
}

pub fn wait_for_order_state(order_id: u128, state_ver: i32) -> Instance {
    loop {
        if let Some(ins) = get_state_instance_by_id(order_id, "B:sale/orderState:1", state_ver) {
            return ins;
        } else {
            warn!("not found state instance, will retry");
            sleep(Duration::from_nanos(3000000))
        }
    }
    // panic!("can't find order and state");
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn order_id_test() {
        let rtn = get_instance_by_id(46912184945275581809007620859293488763, "B:sale/order:1");
        dbg!(rtn);
    }

    #[test]
    fn order_state_test() {
        let rtn = get_state_instance_by_id(46912184945275581809007620859293488763, "B:sale/orderState:1", 1);
        dbg!(rtn);
    }
}