|
1 |
| -use crate::modules::m_dnsquery; |
| 1 | +use futures_util::future::ok; |
2 | 2 | use log::{debug, error, info, warn};
|
3 | 3 | use reqwest::Client;
|
4 | 4 | use serde::Deserialize;
|
| 5 | +use serde_json::{Map, Value}; |
5 | 6 | use std::collections::hash_map::HashMap;
|
| 7 | +use std::io::Read; |
6 | 8 |
|
7 | 9 | /// A struct to hold the REST client
|
8 | 10 | /// ## Fields
|
@@ -38,6 +40,7 @@ pub async fn request(rest_client: &RESTClient) -> Result<String, String> {
|
38 | 40 | response = rest_client
|
39 | 41 | .http_client
|
40 | 42 | .get(&rest_client.base_url)
|
| 43 | + .header("User-Agent", "EyeTrackVR") |
41 | 44 | .send()
|
42 | 45 | .await
|
43 | 46 | .map_err(|e| e.to_string())?
|
@@ -77,31 +80,169 @@ pub async fn run_rest_client(
|
77 | 80 | method: String,
|
78 | 81 | ) -> Result<String, String> {
|
79 | 82 | info!("Starting REST client");
|
80 |
| - // read the json config file |
81 |
| - let data = std::fs::read_to_string("config/config.json").expect("Unable to read config file"); |
82 |
| - // parse the json config file |
83 |
| - let config: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?; |
84 |
| - debug!("Current Config: {:?}", config); |
| 83 | + let full_url = format!("{}{}", device_name, endpoint); |
85 | 84 | let mut request_response: String = String::new();
|
86 |
| - let mut url = config["urls"][device_name].as_str(); |
87 |
| - let full_url_result = match url { |
88 |
| - Some(url) => url, |
89 |
| - None => { |
90 |
| - error!("Unable to get url"); |
91 |
| - url = Some(""); |
92 |
| - return Err(format!("Unable to get url: {:?}", url)); |
93 |
| - } |
94 |
| - }; |
95 |
| - let full_url = format!("{}{}", full_url_result, endpoint); |
96 | 85 | //info!("Full url: {}", full_url);
|
97 | 86 | let rest_client = RESTClient::new(full_url, method);
|
98 | 87 | let request_result = request(&rest_client).await;
|
99 | 88 | match request_result {
|
100 | 89 | Ok(response) => {
|
101 | 90 | request_response = response;
|
102 |
| - println!("Request response: {:?}", request_response); |
| 91 | + println!("[REST Client]: Request response: {:?}", request_response); |
| 92 | + } |
| 93 | + Err(e) => println!("[REST Client]: Request failed: {}", e), |
| 94 | + } |
| 95 | + Ok(request_response) |
| 96 | +} |
| 97 | + |
| 98 | +async fn run_gh_release_latest() -> Result<String, String> { |
| 99 | + info!("Starting GitHub release client"); |
| 100 | + let endpoint = "/releases/latest"; |
| 101 | + let device_name = "https://api.github.com/repos/lorow/OpenIris"; |
| 102 | + let method = "GET".to_string(); |
| 103 | + let mut request_response: String = String::new(); |
| 104 | + let request_result = run_rest_client(endpoint.to_string(), device_name.to_string(), method).await; |
| 105 | + |
| 106 | + match request_result { |
| 107 | + Ok(response) => { |
| 108 | + request_response = response; |
| 109 | + /* println!( |
| 110 | + "[Github Release Latest]: Request response: {:?}", |
| 111 | + request_response |
| 112 | + ); */ |
103 | 113 | }
|
104 |
| - Err(e) => println!("Request failed: {}", e), |
| 114 | + Err(e) => println!("[Github Release Latest]: Request failed: {}", e), |
105 | 115 | }
|
106 | 116 | Ok(request_response)
|
107 | 117 | }
|
| 118 | + |
| 119 | +/* |
| 120 | +release_id: 0, |
| 121 | +new_release: false, |
| 122 | +assets: [], |
| 123 | +*/ |
| 124 | +///! DEPRICATED - but it works .... |
| 125 | +pub async fn run_gh_release_assets() -> Result<String, String> { |
| 126 | + info!("Starting GitHub release client"); |
| 127 | + let gh_response = run_gh_release_latest().await; |
| 128 | + let mut request_response: String = String::new(); |
| 129 | + match gh_response { |
| 130 | + Ok(response) => { |
| 131 | + request_response = response; |
| 132 | + /* println!( |
| 133 | + "[Github Release Asset]: Request Response: {:?}", |
| 134 | + request_response |
| 135 | + ); */ |
| 136 | + } |
| 137 | + Err(e) => println!("[Github Release Asset]: Request failed: {}", e), |
| 138 | + } |
| 139 | + |
| 140 | + let json_response = serde_json::from_str::<Map<String, Value>>(&request_response); |
| 141 | + |
| 142 | + match json_response { |
| 143 | + Ok(response) => { |
| 144 | + println!("[Github Release]: JSON response: {:?}", response); |
| 145 | + // check if the json object is empty |
| 146 | + if response.is_empty() { |
| 147 | + warn!("[Github Release]: JSON object is empty"); |
| 148 | + return Err("[Github Release]: JSON object is empty".into()); |
| 149 | + } |
| 150 | + |
| 151 | + // check if the json object has the key "id" - and if it does check if the value is the same as the current value saved to the file |
| 152 | + if response.contains_key("id") { |
| 153 | + let id = response["id"].to_string(); |
| 154 | + // read the json config file and create it if it doesn't exist |
| 155 | + let mut data = String::new(); |
| 156 | + let mut file = std::fs::OpenOptions::new() |
| 157 | + .read(true) |
| 158 | + .write(true) |
| 159 | + .create(true) |
| 160 | + .open("config/config.json"); |
| 161 | + |
| 162 | + match file { |
| 163 | + Ok(ref mut file) => { |
| 164 | + file.read_to_string(&mut data).unwrap(); |
| 165 | + } |
| 166 | + Err(e) => { |
| 167 | + error!("[Github Release]: Unable to open config file: {}", e); |
| 168 | + return Err("[Github Release]: Unable to open config file".into()); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // if the file is empty, create a new file with the id, and assets array |
| 173 | + if data.is_empty() { |
| 174 | + println!("[Github Release]: File is empty - creating new file"); |
| 175 | + let mut config = Map::new(); |
| 176 | + let mut assets = Map::new(); |
| 177 | + let mut asset = Map::new(); |
| 178 | + asset.insert("id".to_string(), Value::String(id)); |
| 179 | + asset.insert("new_release".to_string(), Value::Bool(true)); |
| 180 | + |
| 181 | + let mut assets_array = Vec::new(); |
| 182 | + // get the assets array from the json response and add it to the assets_array |
| 183 | + let assets_array_json = response["assets"].as_array(); |
| 184 | + let assets_array_json_result = match assets_array_json { |
| 185 | + Some(assets_array_json) => assets_array_json, |
| 186 | + None => { |
| 187 | + error!("[Github Release]: Unable to get assets array"); |
| 188 | + return Err("[Github Release]: Unable to get assets array".into()); |
| 189 | + } |
| 190 | + }; |
| 191 | + for asset in assets_array_json_result { |
| 192 | + assets_array.push(asset.clone()); |
| 193 | + } |
| 194 | + asset.insert("assets".to_string(), Value::Array(assets_array)); |
| 195 | + assets.insert("OpenIris".to_string(), Value::Object(asset)); |
| 196 | + config.insert("assets".to_string(), Value::Object(assets)); |
| 197 | + let config_json = serde_json::to_string_pretty(&config).unwrap(); |
| 198 | + std::fs::write("config/config.json", config_json).expect("Unable to write file"); |
| 199 | + return Ok("[Github Release]: Grabbed Newest Github Asset Config - Created new config file".to_string()); |
| 200 | + } |
| 201 | + |
| 202 | + // we got hre because the file is not empty - so parse the json config file |
| 203 | + |
| 204 | + // check if the id is the same as the one in the config file |
| 205 | + |
| 206 | + // if the id is the same, return and do nothing |
| 207 | + |
| 208 | + // if the id is different, update the config file with the new id and assets array |
| 209 | + // parse the json config file |
| 210 | + let config: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?; |
| 211 | + debug!("[Github Release]: Current Config: {:?}", config); |
| 212 | + |
| 213 | + if id == config["assets"]["OpenIris"]["id"] { |
| 214 | + println!("[Github Release]: No new release - using cached config"); |
| 215 | + return Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string()); |
| 216 | + } |
| 217 | + |
| 218 | + let mut config = Map::new(); |
| 219 | + let mut assets = Map::new(); |
| 220 | + let mut asset = Map::new(); |
| 221 | + asset.insert("id".to_string(), Value::String(id)); |
| 222 | + asset.insert("new_release".to_string(), Value::Bool(true)); |
| 223 | + |
| 224 | + let mut assets_array = Vec::new(); |
| 225 | + // get the assets array from the json response and add it to the assets_array |
| 226 | + let assets_array_json = response["assets"].as_array(); |
| 227 | + let assets_array_json_result = match assets_array_json { |
| 228 | + Some(assets_array_json) => assets_array_json, |
| 229 | + None => { |
| 230 | + error!("Unable to get assets array"); |
| 231 | + return Err("Unable to get assets array".into()); |
| 232 | + } |
| 233 | + }; |
| 234 | + for asset in assets_array_json_result { |
| 235 | + assets_array.push(asset.clone()); |
| 236 | + } |
| 237 | + asset.insert("assets".to_string(), Value::Array(assets_array)); |
| 238 | + assets.insert("OpenIris".to_string(), Value::Object(asset)); |
| 239 | + config.insert("assets".to_string(), Value::Object(assets)); |
| 240 | + let config_json = serde_json::to_string_pretty(&config).unwrap(); |
| 241 | + std::fs::write("config/config.json", config_json).expect("Unable to write file"); |
| 242 | + return Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string()); |
| 243 | + } |
| 244 | + } |
| 245 | + Err(e) => println!("JSON parse failed: {}", e), |
| 246 | + } |
| 247 | + Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string()) |
| 248 | +} |
0 commit comments