use serde::Deserialize;
use serde_json::Value;
use crate::{client::Client, EasyBit, Error};
#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
pub struct Summary {
pub id: String,
pub send: String,
pub receive: String,
pub sendNetwork: String,
pub receiveNetwork: String,
pub sendAmount: String,
pub receiveAmount: String,
pub estimatedSendAmount: String,
pub estimatedReceiveAmount: String,
pub sendAddress: String,
pub sendTag: Option<String>,
pub receiveAddress: String,
pub receiveTag: Option<String>,
pub refundAddress: Option<String>,
pub refundTag: Option<String>,
pub vpm: String,
pub status: String,
pub hashIn: Option<String>,
pub hashOut: Option<String>,
pub networkFee: String,
pub earned: String,
pub validationStatus: Option<String>,
pub createdAt: i128,
pub updatedAt: i128,
}
pub async fn all_orders(
client: &Client,
id: Option<String>,
limit: Option<String>,
date_from: Option<String>,
date_to: Option<String>,
sort_direction: Option<String>,
status: Option<String>,
) -> Result<Vec<Summary>, Error> {
let path = "/orders";
let request = reqwest::Client::new()
.get(format!("{}{}", client.get_url(), path))
.header("API-KEY", client.get_api_key())
.query(&[
("id", id),
("limit", limit),
("dateFrom", date_from),
("dateTo", date_to),
("sortDirection", sort_direction),
("status", status),
])
.send()
.await?;
let json: Value = request.json().await?;
match json.get("data") {
Some(data) => {
log::info!("Raw status: {:?}", data);
let orders: Vec<Summary> = serde_json::from_value(data.clone())?;
Ok(orders)
}
None => {
let error: EasyBit = serde_json::from_value(json)?;
log::error!("{:?}", error);
Err(Error::ApiError(error))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::Client;
use std::env;
#[tokio::test]
async fn test_all_orders() {
let client = Client::new(env::var("URL").unwrap(), env::var("API_KEY").unwrap());
let result = all_orders(&client, None, None, None, None, None, None).await;
assert!(result.is_ok());
}
}