crypto_markets/exchanges/binance/
utils.rs

1use super::super::utils::http_get;
2use crate::error::{Error, Result};
3
4use serde_json::Value;
5use std::collections::HashMap;
6
7fn check_code_in_body(resp: String) -> Result<String> {
8    let obj = serde_json::from_str::<HashMap<String, Value>>(&resp);
9    if obj.is_err() {
10        return Ok(resp);
11    }
12
13    match obj.unwrap().get("code") {
14        Some(code) => {
15            if code.as_i64().unwrap() != 0 {
16                Err(Error(resp))
17            } else {
18                Ok(resp)
19            }
20        }
21        None => Ok(resp),
22    }
23}
24
25pub(super) fn binance_http_get(url: &str) -> Result<String> {
26    let ret = http_get(url, None);
27    match ret {
28        Ok(resp) => check_code_in_body(resp),
29        Err(_) => ret,
30    }
31}
32
33pub(super) fn parse_filter<'a>(
34    filters: &'a [HashMap<String, Value>],
35    filter_type: &'a str,
36    field: &'static str,
37) -> &'a str {
38    filters.iter().find(|x| x["filterType"] == filter_type).unwrap()[field].as_str().unwrap()
39}