diff --git a/src/lib.rs b/src/lib.rs index bc71400..67d4aef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,20 +35,24 @@ pub struct VehicleId(u64); #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ExternalVehicleId(u64); -pub enum RequestData<'a> { - GET { url: &'a str }, - POST { url: &'a str, payload: &'a str }, +enum RequestData<'a> { + Get { url: &'a str }, + Post { url: &'a str, payload: &'a str }, } impl Display for RequestData<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - RequestData::GET { url } => write!(f, "GET {}", url), - RequestData::POST { url, payload } => write!(f, "POST {} {}", url, payload), + RequestData::Get { url } => write!(f, "GET {}", url), + RequestData::Post { url, payload } => write!(f, "POST {} {}", url, payload), } } } +/// API client for the Tesla API. +/// +/// Main entry point for the API. It contains the access token and refresh token, and can be used +/// to make requests to the API. pub struct Api { pub access_token: AccessToken, pub refresh_token: Option, @@ -71,7 +75,7 @@ impl Api { where D: for<'de> Deserialize<'de> + Debug, { - self.request(&RequestData::GET { url }).await + self.request(&RequestData::Get { url }).await } async fn post(&self, url: &str, body: S) -> Result, TeslatteError> @@ -80,7 +84,7 @@ impl Api { { let payload = &serde_json::to_string(&body).expect("Should not fail creating the request struct."); - let request_data = RequestData::POST { url, payload }; + let request_data = RequestData::Post { url, payload }; let data = self.request::(&request_data).await?; if !data.data.result { @@ -105,8 +109,8 @@ impl Api { debug!("{request_data}"); let request_builder = match request_data { - RequestData::GET { url } => self.client.get(*url), - RequestData::POST { url, payload } => self + RequestData::Get { url } => self.client.get(*url), + RequestData::Post { url, payload } => self .client .post(*url) .header("Content-Type", "application/json") @@ -188,12 +192,14 @@ impl From> for Response { } } +/// Standard response data from the API. Contains a reason string and a result bool. #[derive(Debug, Deserialize)] pub struct PostResponse { reason: String, result: bool, } +/// Standard error response from the API. #[derive(Debug, Deserialize)] struct ResponseError { error: String, @@ -203,10 +209,11 @@ struct ResponseError { #[derive(Debug, Serialize)] struct Empty {} -/// Data and body from a request. The body can be used for debugging. The CLI can optionally -/// print the raw JSON so the user can manipulate it. +/// Data and body from a request. The body can be used for debugging. /// -/// This struct will automatically deref to the data type for better ergonomics. +/// The CLI can optionally print the raw JSON so the user can manipulate it. +/// +/// This struct will automatically deref to the `data` type for better ergonomics. #[derive(Debug)] pub struct ResponseData { data: T, @@ -335,7 +342,7 @@ mod tests { "error":{"error": "timeout","error_description": "s"} }"#; - let request_data = RequestData::POST { + let request_data = RequestData::Post { url: "https://example.com", payload: "doesn't matter", }; diff --git a/src/vehicles.rs b/src/vehicles.rs index 6b6a911..94fe4b3 100644 --- a/src/vehicles.rs +++ b/src/vehicles.rs @@ -461,7 +461,7 @@ mod tests { } "#; - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/data_request/charge_state", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -508,7 +508,7 @@ mod tests { } "#; - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/data_request/climate_state", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -535,7 +535,7 @@ mod tests { } "#; - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/data_request/drive_state", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -557,7 +557,7 @@ mod tests { } "#; - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/data_request/gui_settings", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -607,7 +607,7 @@ mod tests { } "#; - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/data_request/vehicle_config", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -682,7 +682,7 @@ mod tests { } "#; - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/data_request/vehicle_state", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -692,7 +692,7 @@ mod tests { fn json_vehicle_data_htlc_2023_09_19() { let s = include_str!("../testdata/vehicle_data_HTLC_2023_09_19.json"); - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/vehicle_data", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -702,7 +702,7 @@ mod tests { fn json_vehicle_data_htlc_2023_10_09() { let s = include_str!("../testdata/vehicle_data_HTLC_2023_10_09.json"); - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/vehicle_data", }; Api::parse_json::(&request_data, s.to_string()).unwrap(); @@ -712,7 +712,7 @@ mod tests { fn json_vehicle_data_bitcoin_lightning_2023_10_09() { let s = include_str!("../testdata/vehicle_data_BitcoinLightning_2023_10_09.json"); - let request_data = RequestData::GET { + let request_data = RequestData::Get { url: "https://owner-api.teslamotors.com/api/1/vehicles/1234567890/vehicle_data", }; Api::parse_json::(&request_data, s.to_string()).unwrap();