diff --git a/Cargo.toml b/Cargo.toml index ac1a9d9..7b9b453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pi-hole-api" -version = "0.3.3" +version = "0.4.0" authors = ["Connor Holloway "] edition = "2018" license = "MIT" @@ -28,4 +28,4 @@ num-traits = "0.2.15" [dev-dependencies] trust-dns-resolver = "0.21.2" test-context = "0.1.3" -serial_test = "0.8.0" \ No newline at end of file +serial_test = "0.8.0" diff --git a/examples/simple.rs b/examples/simple.rs index 5c846ae..316100f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -3,7 +3,7 @@ use pi_hole_api::{PiHoleAPIConfig, UnauthenticatedPiHoleAPI}; fn main() -> Result<(), Box> { let api = PiHoleAPIConfig::new("http://192.168.0.19".to_string()); - let status = api.get_summary(); - println!("{:?}", status); + let version = api.get_version(); + println!("{:?}", version); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 58e8fb9..3ae8c61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,15 +72,6 @@ impl PiHoleAPIKey for PiHoleAPIConfigWithKey { } pub trait UnauthenticatedPiHoleAPI { - /// Get statistics in a raw format (no number format) - fn get_summary_raw(&self) -> Result; - - /// Get statistics in a formatted style - fn get_summary(&self) -> Result; - - /// Get statistics on the number of domains and ads for each 10 minute period - fn get_over_time_data_10_mins(&self) -> Result; - /// Get the Pi-Hole version. fn get_version(&self) -> Result; @@ -115,22 +106,6 @@ impl UnauthenticatedPiHoleAPI for T where T: PiHoleAPIHost, { - fn get_summary_raw(&self) -> Result { - simple_json_request(self.get_host(), "/admin/api.php?summaryRaw", &NO_PARAMS) - } - - fn get_summary(&self) -> Result { - simple_json_request(self.get_host(), "/admin/api.php?summary", &NO_PARAMS) - } - - fn get_over_time_data_10_mins(&self) -> Result { - simple_json_request( - self.get_host(), - "/admin/api.php?overTimeData10mins", - &NO_PARAMS, - ) - } - /// Get simple PiHole version fn get_version(&self) -> Result { let raw_version: Version = @@ -250,6 +225,15 @@ pub trait AuthenticatedPiHoleAPI { /// Get max logage fn get_max_logage(&self) -> Result; + + /// Get statistics in a raw format (no number format) + fn get_summary_raw(&self) -> Result; + + /// Get statistics in a formatted style + fn get_summary(&self) -> Result; + + /// Get statistics on the number of domains and ads for each 10 minute period + fn get_over_time_data_10_mins(&self) -> Result; } fn authenticated_json_request<'a, T, I, K, V>( @@ -581,4 +565,31 @@ where )?; Ok(raw_data.remove("maxlogage").unwrap()) } + + fn get_summary_raw(&self) -> Result { + authenticated_json_request( + self.get_host(), + "/admin/api.php", + [("summaryRaw", "")], + self.get_api_key(), + ) + } + + fn get_summary(&self) -> Result { + authenticated_json_request( + self.get_host(), + "/admin/api.php", + [("summary", "")], + self.get_api_key(), + ) + } + + fn get_over_time_data_10_mins(&self) -> Result { + authenticated_json_request( + self.get_host(), + "/admin/api.php", + [("overTimeData10mins", "")], + self.get_api_key(), + ) + } } diff --git a/tests/tests.rs b/tests/tests.rs index 301d0a5..7d8e1dd 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -80,7 +80,7 @@ impl TestContext for PiHoleTestContext { #[test] #[serial] fn get_summary_raw_test(ctx: &mut PiHoleTestContext) { - let summary_raw = ctx.unauthenticated_api.get_summary_raw().unwrap(); + let summary_raw = ctx.authenticated_api.get_summary_raw().unwrap(); assert!( summary_raw.status == "enabled" || summary_raw.status == "disabled", "Pi-Hole is neither enabled nor disabled" @@ -91,7 +91,7 @@ fn get_summary_raw_test(ctx: &mut PiHoleTestContext) { #[test] #[serial] fn get_summary_test(ctx: &mut PiHoleTestContext) { - let summary = ctx.unauthenticated_api.get_summary().unwrap(); + let summary = ctx.authenticated_api.get_summary().unwrap(); assert!( summary.status == "enabled" || summary.status == "disabled", "Pi-Hole is neither enabled nor disabled" @@ -103,9 +103,7 @@ fn get_summary_test(ctx: &mut PiHoleTestContext) { #[serial] fn get_over_time_data_10_mins_test(ctx: &mut PiHoleTestContext) { // Takes a while to update so performing a request will not immediately increase the counter - ctx.unauthenticated_api - .get_over_time_data_10_mins() - .unwrap(); + ctx.authenticated_api.get_over_time_data_10_mins().unwrap(); } #[test_context(PiHoleTestContext)]