1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use crate::auth::{AccessToken, RefreshToken};
use crate::error::TeslatteError;
use chrono::{DateTime, SecondsFormat, TimeZone};
use derive_more::{Display, FromStr};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display};
use tracing::debug;

pub mod auth;
pub mod calendar_history;
pub mod energy;
pub mod error;
pub mod powerwall;
pub mod vehicles;

#[cfg(feature = "cli")]
pub mod cli;

const API_URL: &str = "https://owner-api.teslamotors.com/api/1";

trait Values {
    fn format(&self, url: &str) -> String;
}

/// Vehicle ID used by the owner-api endpoint.
///
/// This data comes from [`Api::vehicles()`] `id` field.
#[derive(Debug, Serialize, Deserialize, Clone, Display, FromStr)]
pub struct VehicleId(u64);

/// Vehicle ID used by other endpoints.
///
/// This data comes from [`Api::vehicles()`] `vehicle_id` field.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ExternalVehicleId(u64);

pub 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),
        }
    }
}

pub struct Api {
    pub access_token: AccessToken,
    // TODO: Why is this an Option?
    pub refresh_token: Option<RefreshToken>,
    client: Client,
}

impl Api {
    pub fn new(access_token: AccessToken, refresh_token: Option<RefreshToken>) -> Self {
        Api {
            access_token,
            refresh_token,
            client: Client::builder()
                .timeout(std::time::Duration::from_secs(10))
                .build()
                .unwrap(), // TODO: unwrap
        }
    }

    async fn get<D>(&self, url: &str) -> Result<ResponseData<D>, TeslatteError>
    where
        D: for<'de> Deserialize<'de> + Debug,
    {
        self.request(&RequestData::GET { url }).await
    }

    async fn post<S>(&self, url: &str, body: S) -> Result<ResponseData<PostResponse>, TeslatteError>
    where
        S: Serialize + Debug,
    {
        let payload =
            &serde_json::to_string(&body).expect("Should not fail creating the request struct.");
        let request_data = RequestData::POST { url, payload };
        let data = self.request::<PostResponse>(&request_data).await?;

        if !data.data.result {
            return Err(TeslatteError::ServerError {
                request: format!("{request_data}"),
                msg: data.data.reason,
                description: None,
                body: Some(data.body),
            });
        }

        Ok(data)
    }

    async fn request<T>(
        &self,
        request_data: &RequestData<'_>,
    ) -> Result<ResponseData<T>, TeslatteError>
    where
        T: for<'de> Deserialize<'de> + Debug,
    {
        debug!("{request_data}");

        let request_builder = match request_data {
            RequestData::GET { url } => self.client.get(*url),
            RequestData::POST { url, payload } => self
                .client
                .post(*url)
                .header("Content-Type", "application/json")
                .body(payload.to_string()),
        };

        let response_body = request_builder
            .header("Accept", "application/json")
            .header("Authorization", format!("Bearer {}", self.access_token.0))
            .send()
            .await
            .map_err(|source| TeslatteError::FetchError {
                source,
                request: format!("{request_data}"),
            })?
            .text()
            .await
            .map_err(|source| TeslatteError::FetchError {
                source,
                request: format!("{request_data}"),
            })?;

        debug!("Response: {response_body}");

        Self::parse_json(request_data, response_body)
    }

    fn parse_json<T>(
        request_data: &RequestData,
        response_body: String,
    ) -> Result<ResponseData<T>, TeslatteError>
    where
        T: for<'de> Deserialize<'de> + Debug,
    {
        let response: Response<T> = serde_json::from_str::<ResponseDeserializer<T>>(&response_body)
            .map_err(|source| TeslatteError::DecodeJsonError {
                source,
                request: format!("{request_data}"),
                body: response_body.to_string(),
            })?
            .into();

        match response {
            Response::Response(data) => Ok(ResponseData {
                data,
                body: response_body,
            }),
            Response::Error(e) => Err(TeslatteError::ServerError {
                request: format!("{request_data}"),
                msg: e.error,
                description: e.error_description,
                body: Some(response_body.to_owned()),
            }),
        }
    }
}

#[derive(Debug, Deserialize)]
struct ResponseDeserializer<T> {
    error: Option<ResponseError>,
    response: Option<T>,
}

#[derive(Debug)]
enum Response<T> {
    Response(T),
    Error(ResponseError),
}

impl<T> From<ResponseDeserializer<T>> for Response<T> {
    fn from(response: ResponseDeserializer<T>) -> Self {
        match response.error {
            Some(error) => Response::Error(error),
            None => match response.response {
                Some(response) => Response::Response(response),
                None => panic!("ResponseDeserializer has no error or response."),
            },
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct PostResponse {
    reason: String,
    result: bool,
}

#[derive(Debug, Deserialize)]
struct ResponseError {
    error: String,
    error_description: Option<String>,
}

#[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.
///
/// This struct will automatically deref to the data type for better ergonomics.
#[derive(Debug)]
pub struct ResponseData<T> {
    data: T,
    body: String,
}

impl<T> ResponseData<T> {
    pub fn data(&self) -> &T {
        &self.data
    }

    pub fn body(&self) -> &str {
        &self.body
    }
}

impl<T> std::ops::Deref for ResponseData<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

/// GET /api/1/[url]
macro_rules! get {
    ($name:ident, $return_type:ty, $url:expr) => {
        pub async fn $name(
            &self,
        ) -> Result<crate::ResponseData<$return_type>, crate::error::TeslatteError> {
            let url = format!("{}{}", crate::API_URL, $url);
            self.get(&url).await
        }
    };
}
pub(crate) use get;

/// GET /api/1/[url] with an argument.
///
/// Pass in the URL as a format string with one arg, which has to impl Display.
macro_rules! get_arg {
    ($name:ident, $return_type:ty, $url:expr, $arg_type:ty) => {
        pub async fn $name(
            &self,
            arg: &$arg_type,
        ) -> miette::Result<crate::ResponseData<$return_type>, crate::error::TeslatteError> {
            let url = format!($url, arg);
            let url = format!("{}{}", crate::API_URL, url);
            self.get(&url).await
        }
    };
}
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<crate::ResponseData<$return_type>, crate::error::TeslatteError> {
            let url = values.format($url);
            let url = format!("{}{}", crate::API_URL, 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) => {
        pub async fn $name(
            &self,
            arg: &$arg_type,
            data: &$request_type,
        ) -> miette::Result<crate::ResponseData<crate::PostResponse>, crate::error::TeslatteError> {
            let url = format!($url, arg);
            let url = format!("{}{}", crate::API_URL, url);
            self.post(&url, data).await
        }
    };
}
pub(crate) use post_arg;

/// Post like above but with an empty body using the Empty struct.
macro_rules! post_arg_empty {
    ($name:ident, $url:expr, $arg_type:ty) => {
        pub async fn $name(
            &self,
            arg: &$arg_type,
        ) -> miette::Result<crate::ResponseData<crate::PostResponse>, crate::error::TeslatteError> {
            let url = format!($url, arg);
            let url = format!("{}{}", crate::API_URL, url);
            self.post(&url, &Empty {}).await
        }
    };
}
pub(crate) use post_arg_empty;

pub(crate) fn rfc3339<Tz>(d: &DateTime<Tz>) -> String
where
    Tz: TimeZone,
    Tz::Offset: Display,
{
    d.to_rfc3339_opts(SecondsFormat::Secs, true)
}

pub(crate) fn join_query_pairs(pairs: &[(&str, String)]) -> String {
    pairs
        .iter()
        .map(|(k, v)| format!("{}={}", k, v.replace('+', "%2B")))
        .collect::<Vec<_>>()
        .join("&")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vehicles::ChargeState;

    #[test]
    fn error() {
        let s = r#"{
            "response": null,
            "error":{"error": "timeout","error_description": "s"}
        }"#;

        let request_data = RequestData::POST {
            url: "https://example.com",
            payload: "doesn't matter",
        };

        let e = Api::parse_json::<ChargeState>(&request_data, s.to_string());
        if let Err(e) = e {
            if let TeslatteError::ServerError {
                msg, description, ..
            } = e
            {
                assert_eq!(&msg, "timeout");
                assert_eq!(&description.unwrap(), "s");
            } else {
                panic!("unexpected error: {:?}", e);
            }
        } else {
            panic!("expected an error");
        }
    }
}