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
pub mod protobufs {
    include!(concat!(env!("OUT_DIR"), "/oracle_job.rs"));
}

pub mod protos {
    pub use crate::protobufs::{oracle_job, OracleJob};
    pub use prost::Message;

    pub use crate::protobufs::oracle_job::*;

    pub use crate::task::*;

    pub use crate::runner::*;
}

pub mod task;
pub use task::*;

pub use jsonpath_rust as jsonpath;
pub use reqwest;

pub mod runner;
pub use runner::*;

pub mod amm;
pub use amm::*;

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

    use serde_json::Value;
    use std::str::FromStr;

    #[tokio::test]
    async fn test_http_chain() {
        let json_string = r#"{"symbol":"SOLUSDT","price":"25.36000000"}"#;
        let json = Value::from_str(json_string).unwrap();

        let mut server = mockito::Server::new();
        let url = &format!("{}/test", server.url());

        // Create a mock
        let mock = server
            .mock("GET", "/test")
            .with_status(201)
            .with_header("content-type", "application/json")
            .with_body(json_string)
            .create();

        let http_result = http_task(url, None).await.unwrap();
        assert_eq!(http_result, json);
        mock.assert();

        let path = "$.price";

        let value = json_parse_task(http_result, path).unwrap();
        let result = f64::from_str(value.as_str().unwrap()).unwrap();
        assert_eq!(result, 25.36);
        assert_eq!(value.as_str().unwrap(), "25.36000000");
    }

    #[tokio::test]
    async fn test_simple_http_chain() {
        let json_string = r#"{"symbol":"SOLUSDT","price":"25.36000000"}"#;
        let json = Value::from_str(json_string).unwrap();

        let mut server = mockito::Server::new();
        let url = &format!("{}/test", server.url());

        // Create a mock
        let mock = server
            .mock("GET", "/test")
            .with_status(201)
            .with_header("content-type", "application/json")
            .with_body(json_string)
            .create();

        let value = http_task(url, Some("$.price")).await.unwrap();
        mock.assert();

        let result = f64::from_str(value.as_str().unwrap()).unwrap();
        assert_eq!(result, 25.36);
        assert_eq!(value.as_str().unwrap(), "25.36000000");
    }
}