Skip to content

Commit

Permalink
New custom error (#9)
Browse files Browse the repository at this point in the history
Removed reqwest cookies feature.
Added custom error.
Added reqwest client.
  • Loading branch information
grtwje authored May 9, 2022
1 parent 48b7fe9 commit db66aed
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 59 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ path = "src/se_ms_api.rs"

[dependencies]
chrono = "0.4"
reqwest = { version = "0.11", features = ["json", "blocking", "cookies"] }
reqwest = { version = "0.11", features = ["json", "blocking"] }
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
lazy_static = "1.4"
15 changes: 5 additions & 10 deletions src/current_version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Module for querying the current API version of the SolarEdge monitoring server.
use crate::SolaredgeCredentials;
use crate::error::Error;
use crate::{SolaredgeCredentials, REQWEST_CLIENT};
use serde::{Deserialize, Serialize};

/// Current version request
Expand Down Expand Up @@ -36,21 +37,15 @@ impl CurrentVersionReq {
/// # Returns
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<CurrentVersionResp, String> {
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<CurrentVersionResp, Error> {
let url = format!(
"{}version/current?{}",
solaredge.url_start, solaredge.url_end
);

let res = match reqwest::blocking::get(&url) {
Ok(r) => r,
Err(e) => return Err(format!("reqwest get error {}", e)),
};
let res = REQWEST_CLIENT.get(&url).send()?;

let parsed = match res.json::<CurrentVersionResp>() {
Ok(p) => p,
Err(e) => return Err(format!("JSON parse error {}", e)),
};
let parsed = res.json::<CurrentVersionResp>()?;

Ok(parsed)
}
Expand Down
52 changes: 52 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::error;
use std::fmt;

/// An error that can occur in this library.
///
/// Usually errors are when trying to send a request to the SolarEdge server,
/// or when trying to parse the response from the server.
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}

impl Error {
pub(crate) fn new(kind: ErrorKind) -> Error {
Error { kind }
}

/// Convenience function for getting the kind of error.
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}

/// The different kinds of errors that can occur.
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorKind {
/// An error returned from the reqwest crate.
ReqwestError(reqwest::Error),
}

impl error::Error for Error {
fn description(&self) -> &str {
match self.kind {
ErrorKind::ReqwestError(_) => "Reqwest error",
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.kind {
ErrorKind::ReqwestError(s) => write!(f, "Reqwest Error: HTTP status-code{}", s),
}
}
}

impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
Error::new(ErrorKind::ReqwestError(e))
}
}
42 changes: 28 additions & 14 deletions src/se_ms_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
//! }
//!}
//! ```
//! Due to the restrictions that SolarEdge imposes on this API, this library
//! does not try to be performant. For example, it makes blocking HTTP requests.
//!
//! Supported API requests/responses include:
//! * [CurrentVersionReq]/[CurrentVersionResp]
//! * [SiteDetailsReq] / [SiteDetailsResp]
Expand Down Expand Up @@ -65,26 +68,32 @@
//! SiteSensorList,
//! SiteSensorData
//#![warn(unused_crate_dependencies)]
#![warn(unused_crate_dependencies)]
#![deny(unused_extern_crates)]
#![warn(missing_docs)]

pub mod site_details;
pub use current_version::{CurrentVersionReq, CurrentVersionResp};
pub use error::{Error, ErrorKind};
pub use meter_type::MeterType;
pub use site_details::{SiteDetailsReq, SiteDetailsResp};
pub mod site_energy_detailed;
pub use site_energy_detailed::{SiteEnergyDetailedReq, SiteEnergyDetailedResp};
pub mod current_version;
pub use current_version::{CurrentVersionReq, CurrentVersionResp};
pub mod supported_versions;
pub use supported_versions::{SupportedVersionsReq, SupportedVersionsResp};
pub mod date_value;
pub mod meter_type;
pub use meter_type::MeterType;
pub mod meter_value;
pub mod site_location;
pub mod site_module;
pub mod site_public_settings;
pub mod time_unit;

mod current_version;
mod date_value;
mod error;
mod meter_type;
mod meter_value;
mod site_details;
mod site_energy_detailed;
mod site_location;
mod site_module;
mod site_public_settings;
mod supported_versions;
mod time_unit;

#[macro_use]
extern crate lazy_static;

const URL_TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";

Expand Down Expand Up @@ -120,6 +129,11 @@ impl SolaredgeCredentials {
}
}

lazy_static! {
pub(crate) static ref REQWEST_CLIENT: reqwest::blocking::Client =
reqwest::blocking::Client::new();
}

#[cfg(test)]
pub(crate) fn is_normal<T: Sized + Send + Sync + Unpin>() {}

Expand Down
15 changes: 5 additions & 10 deletions src/site_details.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Module for site details requests and responses exchanged with the SolarEdge server monitoring API.
use crate::error::Error;
use crate::site_location::SiteLocation;
use crate::site_module::SiteModule;
use crate::site_public_settings::SitePublicSettings;
use crate::SolaredgeCredentials;
use crate::{SolaredgeCredentials, REQWEST_CLIENT};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -90,21 +91,15 @@ impl SiteDetailsReq {
/// # Returns
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SiteDetailsResp, String> {
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SiteDetailsResp, Error> {
let url = format!(
"{}site/{}/details?{}",
solaredge.url_start, solaredge.site_id, solaredge.url_end
);

let res = match reqwest::blocking::get(&url) {
Ok(r) => r,
Err(e) => return Err(format!("reqwest get error {}", e)),
};
let res = REQWEST_CLIENT.get(&url).send()?;

let parsed = match res.json::<SiteDetailsResp>() {
Ok(p) => p,
Err(e) => return Err(format!("JSON parse error: {}", e)),
};
let parsed = res.json::<SiteDetailsResp>()?;

Ok(parsed)
}
Expand Down
17 changes: 5 additions & 12 deletions src/site_energy_detailed.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Module for detailed site energy measurements from meters such as consumption, export (feed-in), import (purchase), etc.
use crate::error::Error;
use crate::meter_type::MeterType;
use crate::meter_value::MeterValue;
use crate::time_unit::TimeUnit;
use crate::SolaredgeCredentials;
use crate::URL_TIME_FORMAT;
use crate::{SolaredgeCredentials, REQWEST_CLIENT};
use serde::{Deserialize, Serialize};

/// site_energyDetails request
Expand Down Expand Up @@ -92,7 +93,7 @@ impl SiteEnergyDetailedReq {
/// # Returns
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SiteEnergyDetailedResp, String> {
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SiteEnergyDetailedResp, Error> {
let url = format!(
"{}site/{}/energyDetails?{}{}{}{}{}",
solaredge.url_start,
Expand All @@ -104,17 +105,9 @@ impl SiteEnergyDetailedReq {
solaredge.url_end
);

//println!("url: {}\n", url);
let res = match reqwest::blocking::get(&url) {
Ok(r) => r,
Err(e) => return Err(format!("reqwest get error {}", e)),
};
//println!("raw response: {:?}", res);
let res = REQWEST_CLIENT.get(&url).send()?;

let parsed = match res.json::<SiteEnergyDetailedResp>() {
Ok(p) => p,
Err(e) => return Err(format!("JSON parse error {}", e)),
};
let parsed = res.json::<SiteEnergyDetailedResp>()?;

Ok(parsed)
}
Expand Down
15 changes: 5 additions & 10 deletions src/supported_versions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Module for querying the API versions supported by the SolarEdge monitoring server.
use crate::SolaredgeCredentials;
use crate::error::Error;
use crate::{SolaredgeCredentials, REQWEST_CLIENT};
use serde::{Deserialize, Serialize};

/// Supported versions request
Expand Down Expand Up @@ -36,21 +37,15 @@ impl SupportedVersionsReq {
/// # Returns
/// The SolarEdge response or an error string.
/// Errors can occur on the request send or when parsing the response.
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SupportedVersionsResp, String> {
pub fn send(&self, solaredge: &SolaredgeCredentials) -> Result<SupportedVersionsResp, Error> {
let url = format!(
"{}version/supported?{}",
solaredge.url_start, solaredge.url_end
);

let res = match reqwest::blocking::get(&url) {
Ok(r) => r,
Err(e) => return Err(format!("reqwest get error {}", e)),
};
let res = REQWEST_CLIENT.get(&url).send()?;

let parsed = match res.json::<SupportedVersionsResp>() {
Ok(p) => p,
Err(e) => return Err(format!("JSON parse error {}", e)),
};
let parsed = res.json::<SupportedVersionsResp>()?;

Ok(parsed)
}
Expand Down

0 comments on commit db66aed

Please sign in to comment.