Skip to content

Commit

Permalink
Wrk (#10)
Browse files Browse the repository at this point in the history
* Clippy pedantic

* Added site power request message.
  • Loading branch information
grtwje authored May 15, 2022
1 parent cd28be9 commit 2d5c176
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 57 deletions.
17 changes: 9 additions & 8 deletions src/current_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use serde::{Deserialize, Serialize};

/// Current version request
#[derive(Clone, Debug, PartialEq)]
pub struct CurrentVersionReq;
pub struct Req;

/// Current version response
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct CurrentVersionResp {
pub struct Resp {
/// The API version running on the server
pub version: Version,
}
Expand All @@ -21,14 +21,15 @@ pub struct Version {
pub release: String,
}

impl CurrentVersionReq {
impl Req {
/// Create a current version request message that can be sent to SolarEdge.
#[must_use]
pub fn new() -> Self {
CurrentVersionReq {}
Req {}
}
}

impl SendReq<CurrentVersionResp> for CurrentVersionReq {
impl SendReq<Resp> for Req {
fn build_url(&self, solaredge: &SolaredgeCredentials) -> String {
format!(
"{}version/current?{}",
Expand All @@ -37,7 +38,7 @@ impl SendReq<CurrentVersionResp> for CurrentVersionReq {
}
}

impl Default for CurrentVersionReq {
impl Default for Req {
fn default() -> Self {
Self::new()
}
Expand All @@ -50,8 +51,8 @@ mod tests {

#[test]
fn normal_types_unit_test() {
is_normal::<CurrentVersionReq>();
is_normal::<CurrentVersionResp>();
is_normal::<Req>();
is_normal::<Resp>();
is_normal::<Version>();
}
}
15 changes: 8 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,47 @@ use std::fmt;
/// or when trying to parse the response from the server.
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
kind: Kind,
}

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

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

/// The different kinds of errors that can occur.
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorKind {
pub enum Kind {
/// 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",
Kind::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),
Kind::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))
Error::new(Kind::ReqwestError(e))
}
}
29 changes: 21 additions & 8 deletions src/se_ms_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
//! does not try to be performant. For example, it makes blocking HTTP requests.
//!
//! Supported API requests/responses include:
//! * [CurrentVersionReq]/[CurrentVersionResp]
//! * [CurrentVersionReq] / [CurrentVersionResp]
//! * [SiteDetailsReq] / [SiteDetailsResp]
//! * [SiteEnergyDetailedReq] / [SiteEnergyDetailedResp]
//! * [SupportedVersionsReq]/[SupportedVersionsResp]
//! * [SitePowerDetailedReq] / [SitePowerDetailedResp]
//! * [SupportedVersionsReq] / [SupportedVersionsResp]
//!
//! TODO:
//! SitesList,
Expand All @@ -53,7 +54,6 @@
//! SitePower bulk,
//! SiteOverview,
//! SiteOverview bulk,
//! SitePowerDetailed,
//! SitePowerFlow,
//! SiteStorageInformation,
//! SiteImage,
Expand All @@ -72,14 +72,22 @@
#![deny(unused_extern_crates)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::doc_markdown)]

pub use current_version::{CurrentVersionReq, CurrentVersionResp};
pub use error::{Error, ErrorKind};
pub use current_version::Req as CurrentVersionReq;
pub use current_version::Resp as CurrentVersionResp;
pub use error::{Error, Kind};
pub use meter_type::MeterType;
use serde::Deserialize;
pub use site_details::{SiteDetailsReq, SiteDetailsResp};
pub use site_energy_detailed::{SiteEnergyDetailedReq, SiteEnergyDetailedResp};
pub use supported_versions::{SupportedVersionsReq, SupportedVersionsResp};
pub use site_details::Req as SiteDetailsReq;
pub use site_details::Resp as SiteDetailsResp;
pub use site_energy_detailed::Req as SiteEnergyDetailedReq;
pub use site_energy_detailed::Resp as SiteEnergyDetailedResp;
pub use site_power_detailed::Req as SitePowerDetailedReq;
pub use site_power_detailed::Resp as SitePowerDetailedResp;
pub use supported_versions::Req as SupportedVersionsReq;
pub use supported_versions::Resp as SupportedVersionsResp;

mod current_version;
mod date_value;
Expand All @@ -90,6 +98,7 @@ mod site_details;
mod site_energy_detailed;
mod site_location;
mod site_module;
mod site_power_detailed;
mod site_public_settings;
mod supported_versions;
mod time_unit;
Expand All @@ -115,6 +124,7 @@ pub struct SolaredgeCredentials {

impl SolaredgeCredentials {
/// Create a Solaredge destination for the requests from the given site id and api_key.
#[must_use]
pub fn new(site_id: &str, api_key: &str) -> Self {
let site_id = site_id.to_string();
let api_key = format!("api_key={}", api_key);
Expand All @@ -123,6 +133,7 @@ impl SolaredgeCredentials {
}

/// See the site ID being used in the credentials.
#[must_use]
pub fn site_id(&self) -> &str {
&self.site_id
}
Expand All @@ -142,6 +153,8 @@ pub trait SendReq<Resp> {
///
/// # Returns
/// The SolarEdge response or an error string.
///
/// # Errors
/// Errors can occur on the request send or when parsing the response.
fn send(&self, solaredge: &SolaredgeCredentials) -> Result<Resp, Error>
where
Expand Down
17 changes: 9 additions & 8 deletions src/site_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::collections::HashMap;

/// site_details request
#[derive(Clone, Debug, PartialEq)]
pub struct SiteDetailsReq;
pub struct Req;

/// site_details response
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct SiteDetailsResp {
pub struct Resp {
/// Detailed information about the monitoring site
pub details: SiteDetails,
}
Expand Down Expand Up @@ -75,14 +75,15 @@ pub struct SiteDetails {
pub public_settings: SitePublicSettings,
}

impl SiteDetailsReq {
impl Req {
/// Create a site details request message that can be sent to SolarEdge.
#[must_use]
pub fn new() -> Self {
SiteDetailsReq {}
Req {}
}
}

impl SendReq<SiteDetailsResp> for SiteDetailsReq {
impl SendReq<Resp> for Req {
fn build_url(&self, solaredge: &SolaredgeCredentials) -> String {
format!(
"{}site/{}/details?{}",
Expand All @@ -91,7 +92,7 @@ impl SendReq<SiteDetailsResp> for SiteDetailsReq {
}
}

impl Default for SiteDetailsReq {
impl Default for Req {
fn default() -> Self {
Self::new()
}
Expand All @@ -104,8 +105,8 @@ mod tests {

#[test]
fn normal_types_unit_test() {
is_normal::<SiteDetailsReq>();
is_normal::<SiteDetailsResp>();
is_normal::<Req>();
is_normal::<Resp>();
is_normal::<SiteDetails>();
}
}
35 changes: 18 additions & 17 deletions src/site_energy_detailed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};

/// site_energyDetails request
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SiteEnergyDetailedReq {
pub struct Req {
start_time: String,
end_time: String,
time_unit: String,
Expand All @@ -19,7 +19,7 @@ pub struct SiteEnergyDetailedReq {
/// site_energyDetails response
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SiteEnergyDetailedResp {
pub struct Resp {
/// Energy details
pub energy_details: EnergyDetails,
}
Expand All @@ -38,7 +38,7 @@ pub struct EnergyDetails {
pub meters: Vec<MeterValue>,
}

impl SiteEnergyDetailedReq {
impl Req {
/// Create an energy details request message that can be sent to SolarEdge.
///
/// # Arguments
Expand All @@ -49,6 +49,7 @@ impl SiteEnergyDetailedReq {
/// For the time period requested, energy detail values will be
/// chunked into units of this size.
/// * `meters` - meter types to collect energy details for
#[must_use]
pub fn new(
start_time: chrono::NaiveDateTime,
end_time: chrono::NaiveDateTime,
Expand All @@ -68,14 +69,14 @@ impl SiteEnergyDetailedReq {
Some(m) => format!(
"meters={}&",
m.iter()
.map(|x| x.to_string())
.map(MeterType::to_string)
.collect::<Vec<_>>()
.join(",")
),
None => "".to_string(),
};

SiteEnergyDetailedReq {
Req {
start_time,
end_time,
time_unit,
Expand All @@ -84,7 +85,7 @@ impl SiteEnergyDetailedReq {
}
}

impl SendReq<SiteEnergyDetailedResp> for SiteEnergyDetailedReq {
impl SendReq<Resp> for Req {
fn build_url(&self, solaredge: &SolaredgeCredentials) -> String {
format!(
"{}site/{}/energyDetails?{}{}{}{}{}",
Expand All @@ -108,21 +109,21 @@ mod tests {
#[test]
fn site_energy_detailed_req_new_unit_test() {
let dt = "2022-01-01 00:00:00";
let ndt = match NaiveDateTime::parse_from_str(dt, "%Y-%m-%d %H:%M:%S") {
Ok(ndt) => ndt,
Err(_) => panic!("test failed"),
};
let req = SiteEnergyDetailedReq::new(ndt, ndt, None, None);
assert_eq!(req.start_time, format!("startTime={}&", dt));
assert_eq!(req.end_time, format!("endTime={}&", dt));
assert_eq!(req.time_unit, "");
assert_eq!(req.meters, "");
if let Ok(ndt) = NaiveDateTime::parse_from_str(dt, "%Y-%m-%d %H:%M:%S") {
let req = Req::new(ndt, ndt, None, None);
assert_eq!(req.start_time, format!("startTime={}&", dt));
assert_eq!(req.end_time, format!("endTime={}&", dt));
assert_eq!(req.time_unit, "");
assert_eq!(req.meters, "");
} else {
panic!("test failed");
}
}

#[test]
fn normal_types_unit_test() {
is_normal::<SiteEnergyDetailedReq>();
is_normal::<SiteEnergyDetailedResp>();
is_normal::<Req>();
is_normal::<Resp>();
is_normal::<EnergyDetails>();
}
}
Loading

0 comments on commit 2d5c176

Please sign in to comment.