wip: new macro for struct arg
This commit is contained in:
parent
8b7410729e
commit
9392dd5910
|
@ -1,12 +1,34 @@
|
|||
use crate::error::TeslatteError;
|
||||
use crate::powerwall::PowerwallId;
|
||||
use crate::vehicles::VehicleData;
|
||||
use crate::{get, get_arg, post_arg, post_arg_empty, Api, Empty, ExternalVehicleId, VehicleId};
|
||||
use crate::{
|
||||
get, get_arg, get_args, post_arg, post_arg_empty, Api, Empty, ExternalVehicleId, VehicleId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[rustfmt::skip]
|
||||
impl Api {
|
||||
get!(energy_sites, Vec<EnergySite>, "/products");
|
||||
// https://owner-api.teslamotors.com/api/1/energy_sites/1370797147/calendar_history?period=day&kind=power
|
||||
get_args!(energy_sites_calendar_history, CalendarHistory, "/energy_sites/{}/calendar_history", CalendarHistoryValues);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct CalendarHistory {}
|
||||
|
||||
trait Values {
|
||||
fn format(&self, url: &str) -> String;
|
||||
}
|
||||
|
||||
pub struct CalendarHistoryValues {
|
||||
period: String,
|
||||
kind: String,
|
||||
}
|
||||
|
||||
impl Values for CalendarHistoryValues {
|
||||
fn format(&self, url: &str) -> String {
|
||||
format!("{}?period={}&kind={}", url, self.period, self.kind)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -227,6 +227,18 @@ macro_rules! get_arg {
|
|||
}
|
||||
pub(crate) use get_arg;
|
||||
|
||||
/// GET /api/1/[url] with a struct.
|
||||
macro_rules! get_args {
|
||||
($name:ident, $return_type:ty, $url:expr, $args:ty) => {
|
||||
pub async fn $name(&self, values: &$args) -> miette::Result<$return_type, TeslatteError> {
|
||||
let url = values.format($url);
|
||||
let url = format!("/api/1{}", url);
|
||||
self.get(&url).await
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use get_args;
|
||||
|
||||
/// POST /api/1/[url] with an argument and data
|
||||
macro_rules! post_arg {
|
||||
($name:ident, $request_type:ty, $url:expr, $arg_type:ty) => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/// Please note that these structs are generated from my own responses.
|
||||
/// Please note that many of these structs are generated from my own API call responses.
|
||||
///
|
||||
/// Sometimes the API will return a null for a field where I've put in a non Option type, which
|
||||
/// will cause the deserializer to fail. Please log an issue to fix these if you come across it.
|
||||
|
@ -39,6 +39,8 @@ pub struct VehicleData {
|
|||
pub backseat_token: Option<String>,
|
||||
/// gak: This was null for me, assuming String.
|
||||
pub backseat_token_updated_at: Option<String>,
|
||||
|
||||
/// Some of these have been null for me, so making them all Option.
|
||||
pub charge_state: Option<ChargeState>,
|
||||
pub climate_state: Option<ClimateState>,
|
||||
pub drive_state: Option<DriveState>,
|
||||
|
|
Loading…
Reference in a new issue