diff --git a/Cargo.toml b/Cargo.toml index 6118b05..a0eafa6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "govee-api" authors = ["Maciej Gierada"] -version = "0.0.4" +version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" keywords = ["govee", "api", "wrapper", "client", "sdk"] diff --git a/README.md b/README.md index 5fd1673..e4e539a 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,11 @@ A blazingly fast thin wrapper around the public Govee API written in Rust 🚀. THIS IS PRE ALPHA VERSION! + +| is supported | endpoint | method | +| ------------ | --------------------------------- | ------------------- | +| yes | GET /v1/appliance/devices | `get_appliances` | +| yes | PUT /v1/appliance/devices/control | `control_appliance` | +| yes | GET /v1/devices | `get_devices` | +| yes | PUT /v1/devices/control | `control_device` | +| yes | GET /v1/devices/state | `get_device_state` | diff --git a/src/api/api.rs b/src/api/api.rs index 511c384..220c4e3 100644 --- a/src/api/api.rs +++ b/src/api/api.rs @@ -1,7 +1,7 @@ use reqwest::{Client, Url}; use serde_json::json; -use crate::structs::govee::{ApiResponseGoveeDeviceState, ApiResponseGoveeDevices, PayloadBody}; +use crate::structs::govee::{ApiResponseGoveeDeviceState, ApiResponseGoveeDevices, PayloadBody, ApiResponseGoveeAppliances}; // ------------------------ // Methods for the Govee API @@ -20,6 +20,19 @@ pub async fn control_device(govee_root_url: &str, govee_api_key: &str, payload: .unwrap(); } +pub async fn control_appliance(govee_root_url: &str, govee_api_key: &str, payload: PayloadBody) -> () { + let client = Client::new(); + let payload_json = json!(payload); + let endpoint = format!("{}/v1/appliance/devices/control", govee_root_url); + let _response = client + .put(endpoint) + .header("Govee-API-Key", govee_api_key) + .json(&payload_json) + .send() + .await + .unwrap(); +} + pub async fn get_devices(govee_root_url: &str, govee_api_key: &str) -> ApiResponseGoveeDevices { let client = Client::new(); let endpoint = format!("{}/v1/devices", govee_root_url); @@ -34,6 +47,20 @@ pub async fn get_devices(govee_root_url: &str, govee_api_key: &str) -> ApiRespon response_json } +pub async fn get_appliances(govee_root_url: &str, govee_api_key: &str) -> ApiResponseGoveeAppliances{ + let client = Client::new(); + let endpoint = format!("{}/v1/appliance/devices", govee_root_url); + let response = client + .get(endpoint) + .header("Govee-API-Key", govee_api_key) + .send() + .await + .unwrap() + .json::(); + let response_json: ApiResponseGoveeAppliances = response.await.unwrap(); + response_json +} + pub async fn get_device_state( govee_root_url: &str, govee_api_key: &str, diff --git a/src/structs/govee.rs b/src/structs/govee.rs index eecbe01..78bca66 100644 --- a/src/structs/govee.rs +++ b/src/structs/govee.rs @@ -51,6 +51,13 @@ pub struct ApiResponseGoveeDevices { pub data: Option, } +#[derive(Debug, Deserialize, Serialize)] +pub struct ApiResponseGoveeAppliances { + code: i16, + message: String, + pub data: Option, +} + #[derive(Debug, Deserialize, Serialize)] pub struct GoveeData { pub devices: Vec, diff --git a/src/tests/api.rs b/src/tests/api.rs index cfbf34f..a41d149 100644 --- a/src/tests/api.rs +++ b/src/tests/api.rs @@ -3,7 +3,7 @@ mod tests { use mockito; use crate::{ - api::api::{control_device, get_device_state, get_devices}, + api::api::{control_device, get_device_state, get_devices, control_appliance, get_appliances}, structs::govee::{GoveeCommand, PayloadBody}, }; @@ -29,6 +29,29 @@ mod tests { control_device(&govee_root_url, &govee_api_key, payload).await; mock_endpoint.assert(); } + + #[tokio::test] + async fn test_control_appliance() { + let mut server = mockito::Server::new(); + let govee_root_url = server.url(); + let govee_api_key = "1234567890"; + let mock_endpoint = server + .mock("put", "/v1/appliance/devices/control") + .match_header("govee-api-key", govee_api_key) + .with_status(200) + .create(); + let command = GoveeCommand { + name: "mode".to_string(), + value: "16".to_string(), + }; + let payload = PayloadBody { + device: "device_id".to_string(), + model: "model_id".to_string(), + cmd: command, + }; + control_appliance(&govee_root_url, &govee_api_key, payload).await; + mock_endpoint.assert(); + } #[tokio::test] async fn test_get_devices() { @@ -72,6 +95,61 @@ mod tests { get_devices(&govee_root_url, &govee_api_key).await; mock_endpoint.assert(); } + + #[tokio::test] + async fn test_get_appliances() { + let mut server = mockito::Server::new(); + let govee_root_url = server.url(); + let govee_api_key = "1234567890"; + let mock_endpoint = server + .mock("get", "/v1/appliance/devices") + .match_header("govee-api-key", govee_api_key) + .with_status(200) + .with_body( + r#"{ + "code": 200, + "message": "Success", + "devices": [ + { + "device": "appliance_id", + "model": "model_id", + "deviceName": "device_name", + "controllable": true, + "retrievable": true, + "supportCmds": [ + "turn", + "mode" + ], + "properties": { + "mode": { + "options": [ + { + "name": "Low", + "value": 1 + }, + { + "name": "Medium", + "value": 2 + }, + { + "name": "High", + "value": 3 + }, + { + "name": "Sleep", + "value": 4 + } + ] + } + } + } + ] + }"#, + ) + .create(); + get_appliances(&govee_root_url, &govee_api_key).await; + mock_endpoint.assert(); + } #[tokio::test] async fn test_get_device_state() {