Răsfoiți Sursa

特殊处理,代理设置

hl 1 an în urmă
părinte
comite
9a14c7bd88
3 a modificat fișierele cu 53 adăugiri și 33 ștergeri
  1. 19 13
      exchanges/src/phemex_swap_rest.rs
  2. 12 0
      exchanges/src/proxy.rs
  3. 22 20
      exchanges/tests/phemex_swap_test.rs

+ 19 - 13
exchanges/src/phemex_swap_rest.rs

@@ -11,6 +11,8 @@ use serde_json::{json, Value};
 use tracing::{error, info, trace};
 
 use crate::http_tool::RestTool;
+use crate::proxy;
+use crate::proxy::ParsingDetail;
 use crate::response_base::ResponseData;
 
 #[derive(Clone, Debug)]
@@ -33,22 +35,27 @@ impl PhemexSwapRest {
     /*****************************************获取一个对象****************************************************/
     /*******************************************************************************************************/
 
-    pub fn new(is_colo: bool, login_param: BTreeMap<String, String>) -> PhemexSwapRest
+    pub fn new(is_colo: bool, is_proxy: bool, login_param: BTreeMap<String, String>) -> PhemexSwapRest
     {
-        return PhemexSwapRest::new_with_tag("default-PhemexSwapRest".to_string(), is_colo, login_param);
+        return PhemexSwapRest::new_with_tag("default-PhemexSwapRest".to_string(), is_colo, is_proxy, login_param);
     }
-    pub fn new_with_tag(tag: String, is_colo: bool, login_param: BTreeMap<String, String>) -> PhemexSwapRest {
+    pub fn new_with_tag(tag: String, is_colo: bool, is_proxy: bool, login_param: BTreeMap<String, String>) -> PhemexSwapRest {
         let base_url = if is_colo {
-            "https://api.phemex.com".to_string()
+            let z = "https://api.phemex.com".to_string();
+            info!("开启高速(未配置,走普通:{})通道",z);
+            z
         } else {
-            "https://api.phemex.com".to_string()
+            let z = "https://api.phemex.com".to_string();
+            info!("走普通通道:{}",z);
+            z
         };
 
-        if is_colo {
-            info!("开启高速(未配置,走普通:{})通道",base_url);
+        if is_proxy {
+            proxy::ParsingDetail::http_enable_proxy(Some("phemex"));
         } else {
-            info!("走普通通道:{}",base_url);
+            proxy::ParsingDetail::http_enable_proxy(None);
         }
+
         /*****返回结构体*******/
         PhemexSwapRest {
             tag,
@@ -89,8 +96,7 @@ impl PhemexSwapRest {
     }
 
     //持仓(查询交易账户和仓位)
-    pub async fn get_account_and_positions(&mut self,params:Value) -> ResponseData {
-
+    pub async fn get_account_and_positions(&mut self, params: Value) -> ResponseData {
         let data = self.request("GET".to_string(),
                                 "".to_string(),
                                 "/g-accounts/accountPositions".to_string(),
@@ -194,7 +200,7 @@ impl PhemexSwapRest {
                          prefix_url: String,
                          request_url: String,
                          is_login: bool,
-                          params_json: Value) -> ResponseData
+                         params_json: Value) -> ResponseData
     {
         trace!("login_param:{:?}", self.login_param);
         //解析账号信息
@@ -221,7 +227,7 @@ impl PhemexSwapRest {
         if method == "POST" {
             body = params_json.to_string();
         }
-        if method == "GET" || method == "PUT" ||method ==  "DELETE" {
+        if method == "GET" || method == "PUT" || method == "DELETE" {
             let z = params_json.to_string();
             params = RestTool::parse_params_to_str(z);
         }
@@ -331,7 +337,7 @@ impl PhemexSwapRest {
         let response = request_builder.send().await.unwrap();
         let is_success = response.status().is_success(); // 先检查状态码
         let text = response.text().await.unwrap();
-        trace!("text:{:?}",text);
+        // trace!("text:{:?}",text);
         return if is_success {
             self.on_success_data(&text)
         } else {

+ 12 - 0
exchanges/src/proxy.rs

@@ -118,6 +118,18 @@ impl ParsingDetail {
             false
         }
     }
+
+    pub fn removes_proxy(is_unusual: Option<&str>) -> bool {
+        //拿到环境变量解析的数据
+        let parsing_detail = Self::parsing_environment_variables(is_unusual);
+        if parsing_detail.ip_address.len() > 0 && parsing_detail.port.len() > 0 {
+            env::remove_var("http_proxy");
+            env::remove_var("https_proxy");
+            true
+        } else {
+            false
+        }
+    }
 }
 
 

+ 22 - 20
exchanges/tests/phemex_swap_test.rs

@@ -8,10 +8,11 @@ use tracing::trace;
 use exchanges::phemex_swap_rest::PhemexSwapRest;
 use exchanges::phemex_swap_ws::{PhemexSwapLogin, PhemexSwapSubscribeType, PhemexSwapWs, PhemexSwapWsType};
 use exchanges::proxy;
+use exchanges::proxy::ProxyResponseEnum::NO;
 use exchanges::response_base::ResponseData;
 
-const ACCESS_KEY: &str = "";
-const SECRET_KEY: &str = "";
+const ACCESS_KEY: &str = "81afd14f-bc0a-4599-8ae6-8b4dc20e95a0";
+const SECRET_KEY: &str = "BdytnHfrhod7e0RlIACkrdpgRMACjuPK1j8eTxY5wvdkNWVkNDdiZi0xZTNkLTQ2ZjktYjA5YS03NTI0ODJhYzZmZjY";
 
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -75,15 +76,16 @@ async fn ws_custom_subscribe() {
     let param = PhemexSwapLogin {
         access_key: ACCESS_KEY.to_string(),
         secret_key: SECRET_KEY.to_string(),
-        pass_key: "".to_string(),
     };
     let t1 = tokio::spawn(async move {
         let mut ws = get_ws(Option::from(param), PhemexSwapWsType::PublicAndPrivate);
         ws.set_symbols(vec!["BTC_USDT".to_string(), "ETC_USDT".to_string()]);
         ws.set_subscribe(vec![
-            PhemexSwapSubscribeType::PuFuturesTrades,
+            // PhemexSwapSubscribeType::PuFuturesTrades,
             // PhemexSwapSubscribeType::PuFuturesOrderBook,
             // PhemexSwapSubscribeType::PuFuturesRecords,
+
+            PhemexSwapSubscribeType::PrFuturesAccountOrderPosition,
         ]);
         //链接
         let bool_v3_clone = Arc::clone(&is_shutdown_arc);
@@ -102,13 +104,12 @@ fn get_ws(btree_map: Option<PhemexSwapLogin>, ws_type: PhemexSwapWsType) -> Phem
     phemex_ws
 }
 
-
 //服务器时间
 #[tokio::test]
 async fn rest_get_server_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
+    let mut ret = get_rest(false);
     let req_data = ret.get_server().await;
     println!("Phemex--服务器时间--{:?}", req_data);
 }
@@ -118,11 +119,11 @@ async fn rest_get_server_test() {
 async fn rest_get_market_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
+    let mut ret = get_rest(false);
     let req_data = ret.get_market(json!({
 
     })).await;
-    println!("Phemex--查詢合約基礎信息--{:?}", req_data);
+    // println!("Phemex--查詢合約基礎信息--{:?}", req_data);
 }
 
 
@@ -131,8 +132,13 @@ async fn rest_get_market_test() {
 async fn rest_get_account_and_positions_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
-    let req_data = ret.get_account_and_positions().await;
+    let mut ret = get_rest(true);
+    let req_data = ret.get_account_and_positions(
+        json!({
+            "currency":"USDT"
+        })
+    ).await;
+
     println!("Phemex--持仓(查询交易账户和仓位)--{:?}", req_data);
 }
 
@@ -141,7 +147,7 @@ async fn rest_get_account_and_positions_test() {
 async fn rest_set_leverage_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
+    let mut ret = get_rest(true);
     let req_data = ret.set_leverage(json!({
         "symbol":"ZKUSDT",
         "leverageRr":"1"
@@ -155,7 +161,7 @@ async fn rest_set_leverage_test() {
 async fn rest_orders_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
+    let mut ret = get_rest(true);
     let req_data = ret.orders(json!({
           "symbol": "BTCUSDT",
            "side": "Buy",
@@ -170,7 +176,7 @@ async fn rest_orders_test() {
 async fn rest_cancel_order_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
+    let mut ret = get_rest(true);
     let req_data = ret.cancel_order(json!({
           "symbol ": "BTCUSDT",
           "posSide": "Long",
@@ -184,24 +190,20 @@ async fn rest_cancel_order_test() {
 async fn rest_cancel_order_all_test() {
     global::log_utils::init_log_with_trace();
 
-    let mut ret = get_rest();
+    let mut ret = get_rest(true);
     let req_data = ret.cancel_order_all(json!({
     })).await;
     println!("Phemex--撤销所有)--{:?}", req_data);
 }
 
 
+fn get_rest(is_proxy:bool) -> PhemexSwapRest {
 
-
-fn get_rest() -> PhemexSwapRest {
-    if proxy::ParsingDetail::http_enable_proxy(Some("phemex")) {
-        trace!("检测有代理配置,配置走代理");
-    }
     let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
     btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());
     btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string());
 
-    let phemex_exc = PhemexSwapRest::new(false, btree_map.clone());
+    let phemex_exc = PhemexSwapRest::new(false, is_proxy,btree_map.clone());
     phemex_exc
 }