use serde::Deserialize;
use crate::error::OkxError;
#[derive(Debug, Deserialize)]
pub(crate) struct OkxResponse<T> {
pub code: String,
pub msg: String,
pub data: T,
}
impl<T> OkxResponse<T> {
pub fn into_result(self) -> Result<T, OkxError> {
if self.code == "0" {
Ok(self.data)
} else {
Err(OkxError::Api {
code: self.code,
msg: self.msg,
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_success_response() {
let json = r#"{"code":"0","msg":"","data":[{"balance":"100.5"}]}"#;
let resp: OkxResponse<Vec<serde_json::Value>> = serde_json::from_str(json).unwrap();
let data = resp.into_result().unwrap();
assert_eq!(data.len(), 1);
}
#[test]
fn test_error_response() {
let json = r#"{"code":"51008","msg":"Order failed. Insufficient balance.","data":[]}"#;
let resp: OkxResponse<Vec<serde_json::Value>> = serde_json::from_str(json).unwrap();
let err = resp.into_result().unwrap_err();
match err {
OkxError::Api { code, msg } => {
assert_eq!(code, "51008");
assert!(msg.contains("Insufficient"));
}
_ => panic!("Expected Api error"),
}
}
}