Skip to content

Commit

Permalink
Merge pull request #5 from mgierada/handle_errors
Browse files Browse the repository at this point in the history
Handle network errors
  • Loading branch information
mgierada authored Aug 26, 2023
2 parents 3c71632 + 9133206 commit 2662d18
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 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 = "1.0.2"
version = "1.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["govee", "api", "wrapper", "client", "sdk"]
Expand Down
61 changes: 36 additions & 25 deletions src/api/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use reqwest::Error as ReqwestError;
use reqwest::{Client, Url};
use serde_json::json;

Expand Down Expand Up @@ -26,18 +27,23 @@ use crate::{
/// # Panics
///
/// This method will panic if the PUT request encounters an error.

impl GoveeClient {
pub async fn control_device(&self, payload: PayloadBody) -> () {
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 _response = client
let result = client
.put(endpoint)
.header("Govee-API-Key", &self.govee_api_key.to_string())
.json(&payload_json)
.send()
.await
.unwrap();
.await;
match result {
Ok(res) => res,
Err(err) => return Err(err),
};
Ok(())
}
}

Expand All @@ -54,17 +60,21 @@ impl GoveeClient {
///
/// This method will panic if the PUT request encounters an error.
impl GoveeClient {
pub async fn control_appliance(&self, payload: PayloadBody) -> () {
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 _response = client
let result = client
.put(endpoint)
.header("Govee-API-Key", &self.govee_api_key)
.json(&payload_json)
.send()
.await
.unwrap();
.await;
match result {
Ok(res) => res,
Err(err) => return Err(err),
};
Ok(())
}
}

Expand All @@ -81,18 +91,16 @@ impl GoveeClient {
///
/// This method will panic if the GET request encounters an error.
impl GoveeClient {
pub async fn get_devices(&self) -> ApiResponseGoveeDevices {
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
.unwrap()
.json::<ApiResponseGoveeDevices>();
let response_json: ApiResponseGoveeDevices = response.await.unwrap();
response_json
.await?;
let response_json = response.json::<ApiResponseGoveeDevices>().await?;
Ok(response_json)
}
}

Expand All @@ -109,18 +117,16 @@ impl GoveeClient {
///
/// This method will panic if the GET request encounters an error.
impl GoveeClient {
pub async fn get_appliances(&self) -> ApiResponseGoveeAppliances {
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
.unwrap()
.json::<ApiResponseGoveeAppliances>();
let response_json: ApiResponseGoveeAppliances = response.await.unwrap();
response_json
.await?;
let response_json = response.json::<ApiResponseGoveeAppliances>().await?;
Ok(response_json)
}
}

Expand All @@ -141,7 +147,11 @@ impl GoveeClient {
///
/// This method will panic if the GET request encounters an error.
impl GoveeClient {
pub async fn get_device_state(&self, device: &str, model: &str) -> ApiResponseGoveeDeviceState {
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);
Expand All @@ -150,10 +160,11 @@ impl GoveeClient {
.get(url)
.header("Govee-API-Key", &self.govee_api_key)
.send()
.await?;
let response_json = response
.json::<ApiResponseGoveeDeviceState>()
.await
.unwrap()
.json::<ApiResponseGoveeDeviceState>();
let response_json: ApiResponseGoveeDeviceState = response.await.unwrap();
response_json
.unwrap();
Ok(response_json)
}
}
28 changes: 22 additions & 6 deletions src/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ mod tests {
async fn test_control_device() {
// Arrange
let mut server = mockito::Server::new();
let _mock = server.mock("PUT", "/v1/devices/control")
let _mock = server
.mock("PUT", "/v1/devices/control")
.match_header("Govee-API-Key", MOCK_API_KEY)
.with_status(200)
.create();
Expand All @@ -31,9 +32,12 @@ mod tests {
cmd: command,
};

// Act
test_client.control_device(payload).await;
let results = test_client.control_device(payload).await;

// Handle the result appropriately
if let Err(err) = results {
panic!("Error occurred: {:?}", err);
}
// Assert that the mock expectations were satisfied
// mock.assert();
}
Expand All @@ -58,7 +62,11 @@ mod tests {
};
// Create the GoveeClient instance
let govee_client = GoveeClient::new(govee_api_key);
govee_client.control_appliance(payload).await;
let results = govee_client.control_appliance(payload).await;
// Handle the result appropriately
if let Err(err) = results {
panic!("Error occurred: {:?}", err);
}
// mock_endpoint.assert();
}

Expand Down Expand Up @@ -101,7 +109,11 @@ mod tests {
)
.create();
let govee_client = GoveeClient::new(govee_api_key);
govee_client.get_devices().await;
let result = govee_client.get_devices().await;
// Handle the result appropriately
if let Err(err) = result {
panic!("Error occurred: {:?}", err);
}
// mock_endpoint.assert();
}

Expand Down Expand Up @@ -156,7 +168,11 @@ mod tests {
)
.create();
let govee_client = GoveeClient::new(govee_api_key);
govee_client.get_appliances().await;
let result = govee_client.get_appliances().await;
// Handle the result appropriately
if let Err(err) = result {
panic!("Error occurred: {:?}", err);
}
// mock_endpoint.assert();
}
}

0 comments on commit 2662d18

Please sign in to comment.