diff options
author | FxQnLr <[email protected]> | 2024-02-26 15:50:00 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-02-26 15:50:00 +0100 |
commit | 31a57425a76cae121c5d8ef5b0f2442ca6a9ee61 (patch) | |
tree | 19151fea3c1548d6298c0fb819e2a776d9aca74a /src/requests/device.rs | |
parent | 0967f44161e972ff1a8482fd168897a3b183bae3 (diff) | |
parent | ffedfd00a46147b225c834187fc298e88e60d0c5 (diff) | |
download | webol-cli-31a57425a76cae121c5d8ef5b0f2442ca6a9ee61.tar webol-cli-31a57425a76cae121c5d8ef5b0f2442ca6a9ee61.tar.gz webol-cli-31a57425a76cae121c5d8ef5b0f2442ca6a9ee61.zip |
Merge pull request #7 from FxQnLr/webol-dev-9
Webol dev 9
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 | } |