use super::models; use crate::api::core::models::Error; use crate::core; use std::collections::HashMap; /// pub struct Api { client: reqwest::Client, api_config: core::config::ApiConfig, } impl std::fmt::Debug for Api { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.debug_struct("Api of api.kgon.member_account").finish() } } impl Api { /// pub fn new(api_config: core::config::ApiConfig) -> Api { Api { client: reqwest::Client::new(), api_config, } } /// pub async fn get_balance_for_user( &self, data: models::GetBalanceForUserRequest, ) -> Result { let mut params = HashMap::new(); params.insert("username", data.username); let res = match self .client .post(format!("{}/balance", self.api_config.k_url)) .header(reqwest::header::ACCEPT, "application/json") .header( reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded", ) .header("k-secret", self.api_config.k_secret.as_str()) .header("k-username", self.api_config.k_username.as_str()) .form(¶ms) .send() .await { Ok(res) => res, Err(e) => { return Err(Error { code: -1, msg: Some(e.to_string()), }); } }; match res.status() { reqwest::StatusCode::OK => match res.json::().await { Ok(r) => { if r.code != 0 { return Err(Error { code: r.code, msg: r.msg, }); } let balance = r.balance.unwrap_or(0.00); let balance_bota = r.balance_bota.unwrap_or(0.00); let balance_sum = r.balance_sum.unwrap_or(0.00); let companies = r.companies; Ok(models::GetBalanceForUserResponse { balance, balance_bota, balance_sum, companies, }) } Err(e) => Err(Error { code: -1, msg: Some(e.to_string()), }), }, _ => Err(Error { code: -1, msg: None, }), } } /// pub async fn get_balance_for_partner( &self, data: models::GetBalanceForPartnerRequest, ) -> Result { let res = match self .client .post(format!("{}/partner/balance", self.api_config.k_url)) .header(reqwest::header::ACCEPT, "application/json") .header( reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded", ) .header("k-secret", self.api_config.k_secret.as_str()) .header("k-username", self.api_config.k_username.as_str()) .send() .await { Ok(res) => res, Err(e) => { return Err(Error { code: -1, msg: Some(e.to_string()), }); } }; match res.status() { reqwest::StatusCode::OK => match res.json::().await { Ok(r) => { if r.code != 0 { return Err(Error { code: r.code, msg: r.msg, }); } let balance = r.balance.unwrap_or(0.00); let balance_bota = r.balance_bota.unwrap_or(0.00); Ok(models::GetBalanceForPartnerResponse { balance, balance_bota, }) } Err(e) => Err(Error { code: -1, msg: Some(e.to_string()), }), }, _ => Err(Error { code: -1, msg: None, }), } } /// pub async fn create_deposit( &self, data: models::CreateDepositRequest, ) -> Result { let mut params = HashMap::new(); params.insert("username", data.username); if let Some(cash_type) = data.cash_type { params.insert("cashType", cash_type); } params.insert("amount", data.amount.to_string()); if let Some(request_key) = data.request_key { params.insert("requestKey", request_key); } let res = match self .client .post(format!("{}/deposit", self.api_config.k_url)) .header(reqwest::header::ACCEPT, "application/json") .header( reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded", ) .header("k-secret", self.api_config.k_secret.as_str()) .header("k-username", self.api_config.k_username.as_str()) .form(¶ms) .send() .await { Ok(res) => res, Err(e) => { return Err(Error { code: -1, msg: Some(e.to_string()), }); } }; match res.status() { reqwest::StatusCode::OK => match res.json::().await { Ok(r) => { if r.code != 0 { return Err(Error { code: r.code, msg: r.msg, }); } let balance = r.balance.unwrap_or(0.00); Ok(models::CreateDepositResponse { balance }) } Err(e) => Err(Error { code: -1, msg: Some(e.to_string()), }), }, _ => Err(Error { code: -1, msg: None, }), } } /// pub async fn create_withdraw( &self, data: models::CreateWithdrawRequest, ) -> Result { let mut params = HashMap::new(); params.insert("username", data.username); if let Some(request_key) = data.request_key { params.insert("requestKey", request_key); } let res = match self .client .post(format!("{}/withdraw", self.api_config.k_url)) .header(reqwest::header::ACCEPT, "application/json") .header( reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded", ) .header("k-secret", self.api_config.k_secret.as_str()) .header("k-username", self.api_config.k_username.as_str()) .form(¶ms) .send() .await { Ok(res) => res, Err(e) => { return Err(Error { code: -1, msg: Some(e.to_string()), }); } }; match res.status() { reqwest::StatusCode::OK => match res.json::().await { Ok(r) => { if r.code != 0 { return Err(Error { code: r.code, msg: r.msg, }); } let balance = r.balance.unwrap_or(0.00); let balance_cash = r.balance_cash.unwrap_or(0.00); let balance_cash_bota = r.balance_cash_bota.unwrap_or(0.00); let amount = r.amount.unwrap_or(0.00); Ok(models::CreateWithdrawResponse { balance, balance_cash, balance_cash_bota, amount, }) } Err(e) => Err(Error { code: -1, msg: Some(e.to_string()), }), }, _ => Err(Error { code: -1, msg: None, }), } } }