diff options
Diffstat (limited to 'src/requests/device.rs')
-rw-r--r-- | src/requests/device.rs | 86 |
1 files changed, 39 insertions, 47 deletions
diff --git a/src/requests/device.rs b/src/requests/device.rs index cbc838e..7583406 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs | |||
@@ -1,66 +1,58 @@ | |||
1 | use crate::{error::CliError, default_headers, format_url, Protocols}; | 1 | use crate::{check_success, config::Config, default_headers, error::Error, format_url, Protocols}; |
2 | 2 | ||
3 | pub async fn put(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { | 3 | pub async fn put( |
4 | let url = format_url("device", Protocols::Http)?; | 4 | config: &Config, |
5 | println!("{}", url); | 5 | id: String, |
6 | mac: String, | ||
7 | broadcast_addr: String, | ||
8 | ip: String, | ||
9 | ) -> Result<(), Error> { | ||
10 | let url = format_url(config, "device", &Protocols::Http); | ||
11 | println!("{url}"); | ||
6 | let res = reqwest::Client::new() | 12 | let res = reqwest::Client::new() |
7 | .put(url) | 13 | .put(url) |
8 | .headers(default_headers()?) | 14 | .headers(default_headers(config)?) |
9 | .body( | 15 | .body(format!( |
10 | format!( | 16 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, |
11 | r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, | 17 | )) |
12 | id, | ||
13 | mac, | ||
14 | broadcast_addr, | ||
15 | ip | ||
16 | ) | ||
17 | ) | ||
18 | .send() | 18 | .send() |
19 | .await | 19 | .await?; |
20 | .map_err(CliError::Reqwest)? | ||
21 | .text() | ||
22 | .await; | ||
23 | 20 | ||
24 | println!("{:?}", res); | 21 | let body = check_success(res).await?; |
22 | println!("{body}"); | ||
25 | Ok(()) | 23 | Ok(()) |
26 | } | 24 | } |
27 | 25 | ||
28 | pub async fn get(id: String) -> Result<(), CliError> { | 26 | pub async fn get(config: &Config, id: String) -> Result<(), Error> { |
29 | let res = reqwest::Client::new() | 27 | let res = reqwest::Client::new() |
30 | .get(format_url("device", Protocols::Http)?) | 28 | .get(format_url(config, "device", &Protocols::Http)) |
31 | .headers(default_headers()?) | 29 | .headers(default_headers(config)?) |
32 | .body( | 30 | .body(format!(r#"{{"id": "{id}"}}"#)) |
33 | format!(r#"{{"id": "{}"}}"#, id) | ||
34 | ) | ||
35 | .send() | 31 | .send() |
36 | .await | 32 | .await?; |
37 | .map_err(CliError::Reqwest)? | ||
38 | .text() | ||
39 | .await; | ||
40 | 33 | ||
41 | println!("{:?}", res); | 34 | let body = check_success(res).await?; |
35 | println!("{body}"); | ||
42 | Ok(()) | 36 | Ok(()) |
43 | } | 37 | } |
44 | 38 | ||
45 | pub async fn post(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { | 39 | pub async fn post( |
40 | config: &Config, | ||
41 | id: String, | ||
42 | mac: String, | ||
43 | broadcast_addr: String, | ||
44 | ip: String, | ||
45 | ) -> Result<(), Error> { | ||
46 | let res = reqwest::Client::new() | 46 | let res = reqwest::Client::new() |
47 | .post(format_url("device", Protocols::Http)?) | 47 | .post(format_url(config, "device", &Protocols::Http)) |
48 | .headers(default_headers()?) | 48 | .headers(default_headers(config)?) |
49 | .body( | 49 | .body(format!( |
50 | format!( | 50 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, |
51 | r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, | 51 | )) |
52 | id, | ||
53 | mac, | ||
54 | broadcast_addr, | ||
55 | ip | ||
56 | ) | ||
57 | ) | ||
58 | .send() | 52 | .send() |
59 | .await | 53 | .await?; |
60 | .map_err(CliError::Reqwest)? | ||
61 | .text() | ||
62 | .await; | ||
63 | 54 | ||
64 | println!("{:?}", res); | 55 | let body = check_success(res).await?; |
56 | println!("{body}"); | ||
65 | Ok(()) | 57 | Ok(()) |
66 | } | 58 | } |