1
- #![ allow( dead_code, unused_imports, unused_variables) ]
1
+ /* #![allow(dead_code, unused_imports, unused_variables) ]*/
2
2
#![ cfg_attr(
3
- all( not( debug_assertions) , target_os = "windows" ) ,
4
- windows_subsystem = "windows"
3
+ all( not( debug_assertions) , target_os = "windows" ) ,
4
+ windows_subsystem = "windows"
5
5
) ]
6
6
7
- // 1. Grab the mDNS address of the device(s) you want to query
8
- // 2. Create a new RESTClient instance for each device
9
- // 3. Start a new thread for each RESTClient instance
10
- // 4. Each thread will poll the device for new data
11
- // 5. Each thread will send the new data to the main thread
12
- // 6. The main thread will update the UI with the new data
13
-
14
7
use crate :: modules:: m_dnsquery;
15
8
use log:: { debug, error, info, warn} ;
16
9
use reqwest:: Client ;
17
10
use serde:: Deserialize ;
18
11
use std:: collections:: hash_map:: HashMap ;
19
- use std:: sync:: { Arc , Mutex } ;
20
12
21
13
/// A struct to hold the REST client
22
14
/// ## Fields
@@ -25,97 +17,97 @@ use std::sync::{Arc, Mutex};
25
17
/// - `name`: the name of the url to query
26
18
/// - `data`: a hashmap of the data returned from the api
27
19
pub struct RESTClient {
28
- http_client : Client ,
29
- base_url : String ,
30
- method : String ,
20
+ http_client : Client ,
21
+ base_url : String ,
22
+ method : String ,
31
23
}
32
24
33
25
/// A function to create a new RESTClient instance
34
26
/// ## Arguments
35
27
/// - `base_url` The base url of the api to query
36
28
impl RESTClient {
37
- pub fn new ( base_url : String , method : String ) -> Self {
38
- Self {
39
- http_client : Client :: new ( ) ,
40
- base_url,
41
- method,
42
- }
29
+ pub fn new ( base_url : String , method : String ) -> Self {
30
+ Self {
31
+ http_client : Client :: new ( ) ,
32
+ base_url,
33
+ method,
43
34
}
35
+ }
44
36
}
45
37
46
38
pub async fn request ( rest_client : & RESTClient ) -> Result < String , String > {
47
- info ! ( "Making REST request" ) ;
39
+ info ! ( "Making REST request" ) ;
48
40
49
- let response: String ;
50
- let response = match rest_client. method . as_str ( ) {
51
- "GET" => {
52
- response = rest_client
53
- . http_client
54
- . get ( & rest_client. base_url )
55
- . send ( )
56
- . await
57
- . map_err ( |e| e. to_string ( ) ) ?
58
- . text ( )
59
- . await
60
- . map_err ( |e| e. to_string ( ) ) ?;
61
- response
62
- }
63
- "POST" => {
64
- response = rest_client
65
- . http_client
66
- . post ( & rest_client. base_url )
67
- . send ( )
68
- . await
69
- . map_err ( |e| e. to_string ( ) ) ?
70
- . text ( )
71
- . await
72
- . map_err ( |e| e. to_string ( ) ) ?;
73
- response
74
- }
75
- _ => {
76
- error ! ( "Invalid method" ) ;
77
- return Err ( "Invalid method" . to_string ( ) ) ;
78
- }
79
- } ;
41
+ let response: String ;
42
+ let response = match rest_client. method . as_str ( ) {
43
+ "GET" => {
44
+ response = rest_client
45
+ . http_client
46
+ . get ( & rest_client. base_url )
47
+ . send ( )
48
+ . await
49
+ . map_err ( |e| e. to_string ( ) ) ?
50
+ . text ( )
51
+ . await
52
+ . map_err ( |e| e. to_string ( ) ) ?;
53
+ response
54
+ }
55
+ "POST" => {
56
+ response = rest_client
57
+ . http_client
58
+ . post ( & rest_client. base_url )
59
+ . send ( )
60
+ . await
61
+ . map_err ( |e| e. to_string ( ) ) ?
62
+ . text ( )
63
+ . await
64
+ . map_err ( |e| e. to_string ( ) ) ?;
65
+ response
66
+ }
67
+ _ => {
68
+ error ! ( "Invalid method" ) ;
69
+ return Err ( "Invalid method" . to_string ( ) ) ;
70
+ }
71
+ } ;
80
72
81
- Ok ( response)
73
+ Ok ( response)
82
74
}
83
75
84
76
/// A function to run a REST Client and create a new RESTClient instance for each device found
85
77
/// ## Arguments
86
78
/// - `endpoint` The endpoint to query for
87
79
/// - `device_name` The name of the device to query
88
80
pub async fn run_rest_client (
89
- endpoint : String ,
90
- device_name : String ,
91
- method : String ,
81
+ endpoint : String ,
82
+ device_name : String ,
83
+ method : String ,
92
84
) -> Result < String , String > {
93
- info ! ( "Starting REST client" ) ;
94
- // read the json config file
95
- let data = std:: fs:: read_to_string ( "config/config.json" ) . expect ( "Unable to read config file" ) ;
96
- // parse the json config file
97
- let config: serde_json:: Value = serde_json:: from_str ( & data) . map_err ( |e| e. to_string ( ) ) ?;
98
- debug ! ( "Current Config: {:?}" , config) ;
99
- let mut request_response: String = String :: new ( ) ;
100
- let mut url = config[ "urls" ] [ device_name] . as_str ( ) ;
101
- let full_url_result = match url {
102
- Some ( url) => url,
103
- None => {
104
- error ! ( "Unable to get url" ) ;
105
- url = Some ( "" ) ;
106
- return Err ( format ! ( "Unable to get url: {:?}" , url) ) ;
107
- }
108
- } ;
109
- let full_url = format ! ( "{}{}" , full_url_result, endpoint) ;
110
- //info!("Full url: {}", full_url);
111
- let rest_client = RESTClient :: new ( full_url, method) ;
112
- let request_result = request ( & rest_client) . await ;
113
- match request_result {
114
- Ok ( response) => {
115
- request_response = response;
116
- println ! ( "Request response: {:?}" , request_response) ;
117
- }
118
- Err ( e) => println ! ( "Request failed: {}" , e) ,
85
+ info ! ( "Starting REST client" ) ;
86
+ // read the json config file
87
+ let data = std:: fs:: read_to_string ( "config/config.json" ) . expect ( "Unable to read config file" ) ;
88
+ // parse the json config file
89
+ let config: serde_json:: Value = serde_json:: from_str ( & data) . map_err ( |e| e. to_string ( ) ) ?;
90
+ debug ! ( "Current Config: {:?}" , config) ;
91
+ let mut request_response: String = String :: new ( ) ;
92
+ let mut url = config[ "urls" ] [ device_name] . as_str ( ) ;
93
+ let full_url_result = match url {
94
+ Some ( url) => url,
95
+ None => {
96
+ error ! ( "Unable to get url" ) ;
97
+ url = Some ( "" ) ;
98
+ return Err ( format ! ( "Unable to get url: {:?}" , url) ) ;
99
+ }
100
+ } ;
101
+ let full_url = format ! ( "{}{}" , full_url_result, endpoint) ;
102
+ //info!("Full url: {}", full_url);
103
+ let rest_client = RESTClient :: new ( full_url, method) ;
104
+ let request_result = request ( & rest_client) . await ;
105
+ match request_result {
106
+ Ok ( response) => {
107
+ request_response = response;
108
+ println ! ( "Request response: {:?}" , request_response) ;
119
109
}
120
- Ok ( request_response)
110
+ Err ( e) => println ! ( "Request failed: {}" , e) ,
111
+ }
112
+ Ok ( request_response)
121
113
}
0 commit comments