-
Notifications
You must be signed in to change notification settings - Fork 7
Implement screeb commands to list all screens and get a single one by id #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
859d1c3
Implement List/Get
sergey-borovkov 29ceef4
Ref. code review and add user agent
sergey-borovkov f94abd5
Cargo clippy fixes
sergey-borovkov 329a17b
Cargo fmt
sergey-borovkov 83ce583
Ref. code review
sergey-borovkov ddafd21
Merge branch 'master' into screen-commands
sergey-borovkov d6ba7e8
Fix tests
sergey-borovkov f86b1a2
Use format!
sergey-borovkov 5704a62
Update test
sergey-borovkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,199 @@ | ||
| use crate::Authentication; | ||
| use crate::{Authentication, AuthenticationError}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use thiserror::Error; | ||
|
|
||
| use serde_json::Value; | ||
|
|
||
| #[derive(Error, Debug)] | ||
| pub enum CommandError { | ||
| #[error("auth error")] | ||
| AuthenticationError(#[from] AuthenticationError), | ||
| #[error("request error")] | ||
| RequestError(#[from] reqwest::Error), | ||
| #[error("parse error")] | ||
| ParseError(#[from] serde_json::Error), | ||
| #[error("unknown error #[0]")] | ||
| WrongResponseStatus(u16), | ||
| } | ||
| // TODO: Use in the future no need to deserialize for current features | ||
| #[derive(Debug, Deserialize, Serialize, Default)] | ||
| pub struct ScreenConfig {} | ||
| // TODO: Use in the future no need to deserialize for current features | ||
| #[derive(Deserialize, Serialize, Debug, Default)] | ||
| pub struct Screen { | ||
|
sergey-borovkov marked this conversation as resolved.
|
||
| #[serde(default)] | ||
| pub id: String, | ||
| #[serde(default)] | ||
| pub team_id: Option<String>, | ||
| #[serde(default)] | ||
| pub created_at: Option<String>, | ||
| #[serde(default)] | ||
| pub name: Option<String>, | ||
| #[serde(default)] | ||
| pub is_enabled: bool, | ||
| #[serde(default)] | ||
| pub last_ping: Option<String>, | ||
| #[serde(default)] | ||
| pub last_ip: Option<String>, | ||
| #[serde(default)] | ||
| pub local_ip: Option<String>, | ||
| #[serde(default)] | ||
| pub mac: Option<String>, | ||
| #[serde(default)] | ||
| pub last_screenshot_time: Option<String>, | ||
| #[serde(default)] | ||
| pub uptime: Option<String>, | ||
| #[serde(default)] | ||
| pub load_avg: Option<String>, | ||
| #[serde(default)] | ||
| pub signal_strength: Option<i32>, | ||
| #[serde(default)] | ||
| pub interface: Option<String>, | ||
| #[serde(default)] | ||
| pub debug: bool, | ||
| #[serde(default)] | ||
| pub location: Option<String>, | ||
| #[serde(default)] | ||
| pub team: Option<String>, | ||
| #[serde(default)] | ||
| pub timezone: Option<String>, | ||
| #[serde(default)] | ||
| pub hostname: Option<String>, | ||
| #[serde(default)] | ||
| pub ws_open: bool, | ||
| #[serde(default)] | ||
| pub status: Option<String>, | ||
| #[serde(default)] | ||
| pub last_screenshot: Option<String>, | ||
| #[serde(default)] | ||
| pub in_sync: bool, | ||
| #[serde(default)] | ||
| pub software_version: Option<String>, | ||
| #[serde(default)] | ||
| pub hardware_version: Option<String>, | ||
| #[serde(default)] | ||
| pub config: ScreenConfig, | ||
| } | ||
| pub struct ScreenCommand { | ||
| authentication: Authentication, | ||
| } | ||
|
|
||
| impl ScreenCommand { | ||
| fn new(authentication: Authentication) -> Self { | ||
| pub fn new(authentication: Authentication) -> Self { | ||
| Self { authentication } | ||
| } | ||
| pub fn list(&self) -> anyhow::Result<Value, CommandError> { | ||
|
sergey-borovkov marked this conversation as resolved.
|
||
| let url = self.authentication.config.url.clone() + "/v4/screens"; | ||
| let response = self.authentication.build_client()?.get(url).send()?; | ||
| if response.status().as_u16() != 200 { | ||
| return Err(CommandError::WrongResponseStatus( | ||
| response.status().as_u16(), | ||
| )); | ||
| } | ||
| serde_json::from_str(&response.text().unwrap_or_else(|_| "{}".to_owned())) | ||
| .map_err(CommandError::ParseError) | ||
| } | ||
| pub fn get(&self, id: &str) -> anyhow::Result<Value, CommandError> { | ||
| let url = self.authentication.config.url.clone() + "/v4/screens?id=eq." + id; | ||
| let response = self.authentication.build_client()?.get(url).send()?; | ||
| if response.status().as_u16() != 200 { | ||
| return Err(CommandError::WrongResponseStatus( | ||
| response.status().as_u16(), | ||
| )); | ||
| } | ||
|
|
||
| serde_json::from_str(&response.text().unwrap_or_else(|_| "{}".to_owned())) | ||
|
sergey-borovkov marked this conversation as resolved.
|
||
| .map_err(CommandError::ParseError) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::authentication::Config; | ||
| use crate::Authentication; | ||
| use httpmock::{Method::GET, MockServer}; | ||
|
|
||
| use envtestkit::lock::lock_test; | ||
| use envtestkit::set_env; | ||
| use std::ffi::OsString; | ||
| use std::fs; | ||
|
|
||
| use crate::commands::ScreenCommand; | ||
| use serde_json::Value; | ||
| use tempdir::TempDir; | ||
|
|
||
| #[test] | ||
| fn test_list_screens_should_return_correct_screen_list() { | ||
| let tmp_dir = TempDir::new("test").unwrap(); | ||
| let _lock = lock_test(); | ||
| let _test = set_env(OsString::from("HOME"), tmp_dir.path().to_str().unwrap()); | ||
| fs::write(tmp_dir.path().join(".screenly").to_str().unwrap(), "token").unwrap(); | ||
|
sergey-borovkov marked this conversation as resolved.
|
||
| let mock_server = MockServer::start(); | ||
| mock_server.mock(|when, then| { | ||
| when.method(GET) | ||
| .path("/v4/screens") | ||
| .header("Authorization", "Token token") | ||
| .header("user-agent", "screenly-cli 0.1.0"); | ||
| then | ||
| .status(200) | ||
| .body(b"[{\"id\":\"017a5104-524b-33d8-8026-9087b59e7eb5\",\"team_id\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"created_at\":\"2021-06-28T05:07:55+00:00\",\"name\":\"Renat's integrated wired NM\",\"is_enabled\":true,\"coords\":[55.22931, 48.90429],\"last_ping\":\"2021-08-25T06:17:20.728+00:00\",\"last_ip\":null,\"local_ip\":\"192.168.1.146\",\"mac\":\"b8:27:eb:d6:83:6f\",\"last_screenshot_time\":\"2021-08-25T06:09:04.399+00:00\",\"uptime\":\"230728.38\",\"load_avg\":\"0.14\",\"signal_strength\":null,\"interface\":\"eth0\",\"debug\":false,\"location\":\"Kamsko-Ust'inskiy rayon, Russia\",\"team\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"timezone\":\"Europe/Moscow\",\"type\":\"hardware\",\"hostname\":\"srly-4shnfrdc5cd2p0p\",\"ws_open\":false,\"status\":\"Offline\",\"last_screenshot\":\"https://us-assets.screenlyapp.com/01CD1W50NR000A28F31W83B1TY/screenshots/01F98G8MJB6FC809MGGYTSWZNN/5267668e6db35498e61b83d4c702dbe8\",\"in_sync\":false,\"software_version\":\"Screenly 2 Player\",\"hardware_version\":\"Raspberry Pi 3B\",\"config\":{\"hdmi_mode\": 34, \"hdmi_boost\": 2, \"hdmi_drive\": 0, \"hdmi_group\": 0, \"verify_ssl\": true, \"audio_output\": \"hdmi\", \"hdmi_timings\": \"\", \"overscan_top\": 0, \"overscan_left\": 0, \"use_composite\": false, \"display_rotate\": 0, \"overscan_right\": 0, \"overscan_scale\": 0, \"overscan_bottom\": 0, \"disable_overscan\": 0, \"shuffle_playlist\": false, \"framebuffer_width\": 0, \"use_composite_pal\": false, \"framebuffer_height\": 0, \"hdmi_force_hotplug\": true, \"use_composite_ntsc\": false, \"hdmi_pixel_encoding\": 0, \"play_history_enabled\": false}}]"); | ||
| }); | ||
|
|
||
| let config = Config::new(mock_server.base_url()); | ||
| let authentication = Authentication::new_with_config(config); | ||
| let screen_command = ScreenCommand::new(authentication); | ||
| let expected = serde_json::from_str::<Value>("[{\"id\":\"017a5104-524b-33d8-8026-9087b59e7eb5\",\"team_id\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"created_at\":\"2021-06-28T05:07:55+00:00\",\"name\":\"Renat's integrated wired NM\",\"is_enabled\":true,\"coords\":[55.22931, 48.90429],\"last_ping\":\"2021-08-25T06:17:20.728+00:00\",\"last_ip\":null,\"local_ip\":\"192.168.1.146\",\"mac\":\"b8:27:eb:d6:83:6f\",\"last_screenshot_time\":\"2021-08-25T06:09:04.399+00:00\",\"uptime\":\"230728.38\",\"load_avg\":\"0.14\",\"signal_strength\":null,\"interface\":\"eth0\",\"debug\":false,\"location\":\"Kamsko-Ust'inskiy rayon, Russia\",\"team\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"timezone\":\"Europe/Moscow\",\"type\":\"hardware\",\"hostname\":\"srly-4shnfrdc5cd2p0p\",\"ws_open\":false,\"status\":\"Offline\",\"last_screenshot\":\"https://us-assets.screenlyapp.com/01CD1W50NR000A28F31W83B1TY/screenshots/01F98G8MJB6FC809MGGYTSWZNN/5267668e6db35498e61b83d4c702dbe8\",\"in_sync\":false,\"software_version\":\"Screenly 2 Player\",\"hardware_version\":\"Raspberry Pi 3B\",\"config\":{\"hdmi_mode\": 34, \"hdmi_boost\": 2, \"hdmi_drive\": 0, \"hdmi_group\": 0, \"verify_ssl\": true, \"audio_output\": \"hdmi\", \"hdmi_timings\": \"\", \"overscan_top\": 0, \"overscan_left\": 0, \"use_composite\": false, \"display_rotate\": 0, \"overscan_right\": 0, \"overscan_scale\": 0, \"overscan_bottom\": 0, \"disable_overscan\": 0, \"shuffle_playlist\": false, \"framebuffer_width\": 0, \"use_composite_pal\": false, \"framebuffer_height\": 0, \"hdmi_force_hotplug\": true, \"use_composite_ntsc\": false, \"hdmi_pixel_encoding\": 0, \"play_history_enabled\": false}}]").unwrap(); | ||
| let v = screen_command.list().unwrap(); | ||
| assert_eq!(v, expected); | ||
| } | ||
|
|
||
| fn list() {} | ||
| #[test] | ||
| fn test_list_screens_when_not_authenticated_should_return_error() { | ||
| let tmp_dir = TempDir::new("test").unwrap(); | ||
| let _lock = lock_test(); | ||
| let _test = set_env(OsString::from("HOME"), tmp_dir.path().to_str().unwrap()); | ||
| let mock_server = MockServer::start(); | ||
| mock_server.mock(|when, then| { | ||
| when.method(GET) | ||
| .path("/v4/screens") | ||
| .header("Authorization", "Token token") | ||
| .header("user-agent", "screenly-cli 0.1.0"); | ||
| then | ||
| .status(200) | ||
| .body(b"[{\"id\":\"017a5104-524b-33d8-8026-9087b59e7eb5\",\"team_id\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"created_at\":\"2021-06-28T05:07:55+00:00\",\"name\":\"Renat's integrated wired NM\",\"is_enabled\":true,\"coords\":[55.22931, 48.90429],\"last_ping\":\"2021-08-25T06:17:20.728+00:00\",\"last_ip\":null,\"local_ip\":\"192.168.1.146\",\"mac\":\"b8:27:eb:d6:83:6f\",\"last_screenshot_time\":\"2021-08-25T06:09:04.399+00:00\",\"uptime\":\"230728.38\",\"load_avg\":\"0.14\",\"signal_strength\":null,\"interface\":\"eth0\",\"debug\":false,\"location\":\"Kamsko-Ust'inskiy rayon, Russia\",\"team\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"timezone\":\"Europe/Moscow\",\"type\":\"hardware\",\"hostname\":\"srly-4shnfrdc5cd2p0p\",\"ws_open\":false,\"status\":\"Offline\",\"last_screenshot\":\"https://us-assets.screenlyapp.com/01CD1W50NR000A28F31W83B1TY/screenshots/01F98G8MJB6FC809MGGYTSWZNN/5267668e6db35498e61b83d4c702dbe8\",\"in_sync\":false,\"software_version\":\"Screenly 2 Player\",\"hardware_version\":\"Raspberry Pi 3B\",\"config\":{\"hdmi_mode\": 34, \"hdmi_boost\": 2, \"hdmi_drive\": 0, \"hdmi_group\": 0, \"verify_ssl\": true, \"audio_output\": \"hdmi\", \"hdmi_timings\": \"\", \"overscan_top\": 0, \"overscan_left\": 0, \"use_composite\": false, \"display_rotate\": 0, \"overscan_right\": 0, \"overscan_scale\": 0, \"overscan_bottom\": 0, \"disable_overscan\": 0, \"shuffle_playlist\": false, \"framebuffer_width\": 0, \"use_composite_pal\": false, \"framebuffer_height\": 0, \"hdmi_force_hotplug\": true, \"use_composite_ntsc\": false, \"hdmi_pixel_encoding\": 0, \"play_history_enabled\": false}}]"); | ||
| }); | ||
|
|
||
| fn get(_id: &str) {} | ||
| let config = Config::new(mock_server.base_url()); | ||
| let authentication = Authentication::new_with_config(config); | ||
| let screen_command = ScreenCommand::new(authentication); | ||
| let v = screen_command.list(); | ||
| assert!(v.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_screen_should_return_screen() { | ||
| let tmp_dir = TempDir::new("test").unwrap(); | ||
| let _lock = lock_test(); | ||
| let _test = set_env(OsString::from("HOME"), tmp_dir.path().to_str().unwrap()); | ||
| fs::write(tmp_dir.path().join(".screenly").to_str().unwrap(), "token").unwrap(); | ||
| let mock_server = MockServer::start(); | ||
| mock_server.mock(|when, then| { | ||
| when.method(GET) | ||
| .path("/v4/screens") | ||
| .query_param("id", "eq.017a5104-524b-33d8-8026-9087b59e7eb5") | ||
| .header("user-agent", "screenly-cli 0.1.0") | ||
| .header("Authorization", "Token token"); | ||
| then | ||
| .status(200) | ||
| .body(b"[{\"id\":\"017a5104-524b-33d8-8026-9087b59e7eb5\",\"team_id\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"created_at\":\"2021-06-28T05:07:55+00:00\",\"name\":\"Renat's integrated wired NM\",\"is_enabled\":true,\"coords\":[55.22931, 48.90429],\"last_ping\":\"2021-08-25T06:17:20.728+00:00\",\"last_ip\":null,\"local_ip\":\"192.168.1.146\",\"mac\":\"b8:27:eb:d6:83:6f\",\"last_screenshot_time\":\"2021-08-25T06:09:04.399+00:00\",\"uptime\":\"230728.38\",\"load_avg\":\"0.14\",\"signal_strength\":null,\"interface\":\"eth0\",\"debug\":false,\"location\":\"Kamsko-Ust'inskiy rayon, Russia\",\"team\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"timezone\":\"Europe/Moscow\",\"type\":\"hardware\",\"hostname\":\"srly-4shnfrdc5cd2p0p\",\"ws_open\":false,\"status\":\"Offline\",\"last_screenshot\":\"https://us-assets.screenlyapp.com/01CD1W50NR000A28F31W83B1TY/screenshots/01F98G8MJB6FC809MGGYTSWZNN/5267668e6db35498e61b83d4c702dbe8\",\"in_sync\":false,\"software_version\":\"Screenly 2 Player\",\"hardware_version\":\"Raspberry Pi 3B\",\"config\":{\"hdmi_mode\": 34, \"hdmi_boost\": 2, \"hdmi_drive\": 0, \"hdmi_group\": 0, \"verify_ssl\": true, \"audio_output\": \"hdmi\", \"hdmi_timings\": \"\", \"overscan_top\": 0, \"overscan_left\": 0, \"use_composite\": false, \"display_rotate\": 0, \"overscan_right\": 0, \"overscan_scale\": 0, \"overscan_bottom\": 0, \"disable_overscan\": 0, \"shuffle_playlist\": false, \"framebuffer_width\": 0, \"use_composite_pal\": false, \"framebuffer_height\": 0, \"hdmi_force_hotplug\": true, \"use_composite_ntsc\": false, \"hdmi_pixel_encoding\": 0, \"play_history_enabled\": false}}]"); | ||
| }); | ||
|
|
||
| let config = Config::new(mock_server.base_url()); | ||
| let authentication = Authentication::new_with_config(config); | ||
| let screen_command = ScreenCommand::new(authentication); | ||
| let expected = serde_json::from_str::<Value>("[{\"id\":\"017a5104-524b-33d8-8026-9087b59e7eb5\",\"team_id\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"created_at\":\"2021-06-28T05:07:55+00:00\",\"name\":\"Renat's integrated wired NM\",\"is_enabled\":true,\"coords\":[55.22931, 48.90429],\"last_ping\":\"2021-08-25T06:17:20.728+00:00\",\"last_ip\":null,\"local_ip\":\"192.168.1.146\",\"mac\":\"b8:27:eb:d6:83:6f\",\"last_screenshot_time\":\"2021-08-25T06:09:04.399+00:00\",\"uptime\":\"230728.38\",\"load_avg\":\"0.14\",\"signal_strength\":null,\"interface\":\"eth0\",\"debug\":false,\"location\":\"Kamsko-Ust'inskiy rayon, Russia\",\"team\":\"016343c2-82b8-0000-a121-e30f1035875e\",\"timezone\":\"Europe/Moscow\",\"type\":\"hardware\",\"hostname\":\"srly-4shnfrdc5cd2p0p\",\"ws_open\":false,\"status\":\"Offline\",\"last_screenshot\":\"https://us-assets.screenlyapp.com/01CD1W50NR000A28F31W83B1TY/screenshots/01F98G8MJB6FC809MGGYTSWZNN/5267668e6db35498e61b83d4c702dbe8\",\"in_sync\":false,\"software_version\":\"Screenly 2 Player\",\"hardware_version\":\"Raspberry Pi 3B\",\"config\":{\"hdmi_mode\": 34, \"hdmi_boost\": 2, \"hdmi_drive\": 0, \"hdmi_group\": 0, \"verify_ssl\": true, \"audio_output\": \"hdmi\", \"hdmi_timings\": \"\", \"overscan_top\": 0, \"overscan_left\": 0, \"use_composite\": false, \"display_rotate\": 0, \"overscan_right\": 0, \"overscan_scale\": 0, \"overscan_bottom\": 0, \"disable_overscan\": 0, \"shuffle_playlist\": false, \"framebuffer_width\": 0, \"use_composite_pal\": false, \"framebuffer_height\": 0, \"hdmi_force_hotplug\": true, \"use_composite_ntsc\": false, \"hdmi_pixel_encoding\": 0, \"play_history_enabled\": false}}]").unwrap(); | ||
| let v = screen_command | ||
| .get("017a5104-524b-33d8-8026-9087b59e7eb5") | ||
| .unwrap(); | ||
| assert_eq!(v, expected); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.