1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use reqwest::Error as ReqwestError;
use reqwest::{Client, Url};
use serde_json::json;
use crate::{
structs::govee::{
ApiResponseGoveeAppliances, ApiResponseGoveeDeviceState, ApiResponseGoveeDevices,
PayloadBody,
},
GoveeClient, GOVEE_ROOT_URL,
};
// ------------------------
// Methods for the Govee API
// ------------------------
//
/// Control a Govee device using the provided payload.
///
/// This method sends a PUT request to the Govee API's control endpoint in order to control a Govee device.
///
/// # Arguments
///
/// * `payload` - The payload containing control instructions for the device.
///
/// # Returns
///
/// Returns `Ok(())` if the request is successful. Otherwise, returns a `ReqwestError` indicating the failure
impl GoveeClient {
pub async fn control_device(&self, payload: PayloadBody) -> Result<(), ReqwestError> {
let client = Client::new();
let payload_json = json!(payload);
let endpoint = format!("{}/v1/devices/control", &self.govee_root_url);
let result = client
.put(endpoint)
.header("Govee-API-Key", &self.govee_api_key.to_string())
.json(&payload_json)
.send()
.await;
match result {
Ok(res) => res,
Err(err) => return Err(err),
};
Ok(())
}
}
/// Asynchronously controls multiple devices by sending payloads to the Govee API.
///
/// This method takes a vector of `PayloadBody` objects and sends control requests to the Govee API
/// for each payload asynchronously. It uses the provided `self.govee_api_key` and `self.govee_root_url`
/// to construct the API endpoint for each request.
///
/// # Arguments
///
/// - `payloads`: A vector of `PayloadBody` objects representing the control payloads for the devices.
///
/// # Returns
///
/// Returns a `Result` indicating success or an error that occurred during the requests.
///
/// # Examples
///
/// ```rust
/// let govee_client = GoveeClient::new("your_api_key", "https://api.govee.com");
/// let payloads = vec![payload1, payload2];
/// let result = govee_client.bulk_control_devices(payloads).await;
/// match result {
/// Ok(_) => println!("Devices controlled successfully"),
/// Err(err) => eprintln!("Error controlling devices: {:?}", err),
/// }
/// `
impl GoveeClient {
pub async fn bulk_control_devices(
&self,
payloads: Vec<PayloadBody>,
) -> Result<(), ReqwestError> {
let client = Client::new();
let endpoint = format!("{}/v1/devices/control", &self.govee_root_url);
let requests = payloads
.iter()
.map(|payload| {
let payload_json = json!(payload);
let endpoint = endpoint.clone();
let govee_api_key = self.govee_api_key.to_string();
let client = client.clone();
async move {
client
.put(&endpoint)
.header("Govee-API-Key", &govee_api_key)
.json(&payload_json)
.send()
.await
}
})
.collect::<Vec<_>>();
let results = futures::future::join_all(requests).await;
for result in results {
match result {
Ok(_) => (),
Err(err) => return Err(err),
}
}
Ok(())
}
}
/// Controls a Govee appliance using the provided payload.
///
/// This method sends a PUT request to the Govee API to control an appliance
/// with the specified payload. The payload should be in the form of a PayloadBody struct.
///
/// # Arguments
///
/// * `payload` - The payload containing the control instructions for the appliance.
///
/// # Returns
///
/// Returns `Ok(())` if the request is successful. Otherwise, returns a `ReqwestError` indicating the failure
impl GoveeClient {
pub async fn control_appliance(&self, payload: PayloadBody) -> Result<(), ReqwestError> {
let client = Client::new();
let payload_json = json!(payload);
let endpoint = format!("{}/v1/appliance/devices/control", GOVEE_ROOT_URL);
let result = client
.put(endpoint)
.header("Govee-API-Key", &self.govee_api_key)
.json(&payload_json)
.send()
.await;
match result {
Ok(res) => res,
Err(err) => return Err(err),
};
Ok(())
}
}
/// Retrieves a list of Govee devices.
///
/// This method sends a GET request to the Govee API to retrieve a list of devices
/// associated with the Govee account.
///
/// # Returns
///
/// An `ApiResponseGoveeDevices` containing information about the devices.
///
/// # Returns
///
/// Returns `Ok(())` if the request is successful. Otherwise, returns a `ReqwestError` indicating the failure
impl GoveeClient {
pub async fn get_devices(&self) -> Result<ApiResponseGoveeDevices, ReqwestError> {
let client = Client::new();
let endpoint = format!("{}/v1/devices", GOVEE_ROOT_URL);
let response = client
.get(endpoint)
.header("Govee-API-Key", &self.govee_api_key)
.send()
.await?;
let response_json = response.json::<ApiResponseGoveeDevices>().await?;
Ok(response_json)
}
}
/// Retrieves a list of Govee appliances.
///
/// This method sends a GET request to the Govee API to retrieve a list of appliances
/// associated with the Govee account.
///
/// # Returns
///
/// An `ApiResponseGoveeAppliances` containing information about the appliances.
///
/// # Returns
///
/// Returns `Ok(())` if the request is successful. Otherwise, returns a `ReqwestError` indicating the failure
impl GoveeClient {
pub async fn get_appliances(&self) -> Result<ApiResponseGoveeAppliances, ReqwestError> {
let client = Client::new();
let endpoint = format!("{}/v1/appliance/devices", GOVEE_ROOT_URL);
let response = client
.get(endpoint)
.header("Govee-API-Key", &self.govee_api_key)
.send()
.await?;
let response_json = response.json::<ApiResponseGoveeAppliances>().await?;
Ok(response_json)
}
}
/// Retrieves the state of a Govee device.
///
/// This method sends a GET request to the Govee API to retrieve the state of a specific device.
///
/// # Arguments
///
/// * `device` - The device ID or name.
/// * `model` - The model of the device.
///
/// # Returns
///
/// An `ApiResponseGoveeDeviceState` containing the current state of the device.
///
/// # Returns
///
/// Returns `Ok(())` if the request is successful. Otherwise, returns a `ReqwestError` indicating the failure
impl GoveeClient {
pub async fn get_device_state(
&self,
device: &str,
model: &str,
) -> Result<ApiResponseGoveeDeviceState, ReqwestError> {
let client = Client::new();
let params = [("device", device), ("model", model)];
let endpoint = format!("{}/v1/devices/state", GOVEE_ROOT_URL);
let url = Url::parse_with_params(&endpoint, ¶ms).unwrap();
let response = client
.get(url)
.header("Govee-API-Key", &self.govee_api_key)
.send()
.await?;
let response_json = response
.json::<ApiResponseGoveeDeviceState>()
.await
.unwrap();
Ok(response_json)
}
}