Skip to content

Commit

Permalink
Merge pull request #2 from mgierada/add_appliances_endpoints
Browse files Browse the repository at this point in the history
Add appliances endpoints
  • Loading branch information
mgierada authored Jul 29, 2023
2 parents ef7021c + 982293c commit c3d9f91
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
29 changes: 28 additions & 1 deletion src/api/api.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand All @@ -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::<ApiResponseGoveeAppliances >();
let response_json: ApiResponseGoveeAppliances = response.await.unwrap();
response_json
}

pub async fn get_device_state(
govee_root_url: &str,
govee_api_key: &str,
Expand Down
7 changes: 7 additions & 0 deletions src/structs/govee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ pub struct ApiResponseGoveeDevices {
pub data: Option<GoveeData>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ApiResponseGoveeAppliances {
code: i16,
message: String,
pub data: Option<GoveeData>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GoveeData {
pub devices: Vec<GoveeDevice>,
Expand Down
80 changes: 79 additions & 1 deletion src/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c3d9f91

Please sign in to comment.