rusteero 2.0.0

Async Rust client for the Eero (Amazon) mesh Wi-Fi cloud API — a port of the Python eero-api library
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! Data Usage API: `eero-api`'s `DataUsageAPI`.
//!
//! Ported from `eero-api src/eero/api/data_usage.py` at `v8.0.4`
//! (`.claude/tasks/briefs/v8/g3-devices.md`). The whole module was rewritten in `05a2b07`
//! (v8.0.0): the pre-8.0.0 shape this crate previously shipped — one `get_data_usage(payload:
//! Value, resource: Option<&str>)` method that attached an arbitrary JSON body to a `GET`
//! request — is gone entirely, replaced by eleven distinct, **query-params-only** methods, one
//! per resource. `data_usage.py`'s own module docstring is explicit: "None of these endpoints
//! accept a request body; the API rejects a body-bearing `GET` with a `400`"
//! (`data_usage.py:10-11`).
//!
//! Every method resolves its network segment via the same "prefer `parent`'s own `self_url`,
//! else the bare `network_id` template" rule as [`crate::params::resolve_network_url`] — see
//! `DataUsageApi::network_id_or_self_url` for why this is implemented locally rather than by
//! threading `parent` through [`crate::routes::Resource::resolve`] itself.
//!
//! `cadence` is required on six of the eleven methods (`get_data_usage`, `get_device_usage`,
//! `get_eeros_summary`, `get_eero_usage`, `get_profile_usage`, `get_unprofiled_summary` — taking
//! `cadence: &str`) and optional on the other three (`get_breakdown`, `get_devices_usage`,
//! `get_unprofiled_devices` — taking `cadence: Option<&str>`); `get_report_settings` takes no
//! query parameters at all. Every method here funnels through [`crate::transport::Transport`],
//! which already implements the "not authenticated" precondition and every status-to-error
//! mapping a response can produce.

use std::sync::Arc;

use reqwest::Method;
use serde_json::{Value, json};
use url::Url;

use crate::envelope::Envelope;
use crate::error::Error;
use crate::params;
use crate::routes;
use crate::routes::Resource;
use crate::transport::{RequestBody, Transport};

/// `eero-api`'s `DataUsageAPI` (`src/eero/api/data_usage.py`).
///
/// Build one with [`DataUsageApi::new`], wrapping a [`Transport`] already shared with the rest
/// of the `EeroApi` aggregator — `DataUsageApi` never constructs or owns a `Transport` itself.
#[derive(Debug)]
pub struct DataUsageApi {
    transport: Arc<Transport>,
}

impl DataUsageApi {
    /// Wraps `transport` as a `DataUsageApi`.
    #[must_use]
    pub fn new(transport: Arc<Transport>) -> Self {
        Self { transport }
    }

    /// `GET /2.2/networks/{network_id}/data_usage` — network-wide data-usage statistics.
    ///
    /// Ported from `DataUsageAPI.get_data_usage` (`data_usage.py:155-197`). `start`/`end` are
    /// required; `cadence` is required (`cadence_required=True`); `timezone` is optional,
    /// included only when supplied.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] with `field: "cadence"` if `cadence` is not `"hourly"` or
    /// `"daily"`, or if `parent`'s own `url` field (when present) is malformed, before any
    /// request is sent. Returns `Error::Authentication("Not authenticated")` if no valid session
    /// is configured, or whatever other status-mapped [`Error`] the request produces otherwise.
    pub async fn get_data_usage(
        &self,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence(cadence)?;
        self.get_usage(
            &routes::data_usage::V8_GET_DATA_USAGE,
            network_id,
            start,
            end,
            Some(cadence),
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/breakdown` — data-usage breakdown by category.
    ///
    /// Ported from `DataUsageAPI.get_breakdown` (`data_usage.py:199-236`). `cadence` is
    /// optional, validated only when supplied.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_data_usage`].
    pub async fn get_breakdown(
        &self,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: Option<&str>,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence_opt(cadence)?;
        self.get_usage(
            &routes::data_usage::GET_DATA_USAGE_BREAKDOWN,
            network_id,
            start,
            end,
            cadence,
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/devices` — data-usage statistics for every
    /// device on a network, optionally filtered by `profile_id`.
    ///
    /// Ported from `DataUsageAPI.get_devices_usage` (`data_usage.py:238-280`). `cadence` and
    /// `profile_id` are both optional, each included only when supplied.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_data_usage`].
    #[allow(clippy::too_many_arguments)]
    pub async fn get_devices_usage(
        &self,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: Option<&str>,
        timezone: Option<&str>,
        profile_id: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence_opt(cadence)?;
        let id_or_url = self.network_id_or_self_url(network_id, parent)?;
        let url = routes::data_usage::GET_DEVICES_DATA_USAGE.resolve(
            self.transport.api_host(),
            &id_or_url,
            None,
        )?;
        let mut query = usage_query(start, end, cadence, timezone);
        if let Some(profile_id) = profile_id {
            query.push(("profile_id", profile_id.to_owned()));
        }
        self.transport
            .request(Method::GET, url, &query, RequestBody::None)
            .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/devices/{device_mac}` — data-usage statistics
    /// for a single device.
    ///
    /// Ported from `DataUsageAPI.get_device_usage` (`data_usage.py:282-325`): the collection URL
    /// ([`crate::routes::data_usage::GET_DEVICES_DATA_USAGE`]) with `device_mac` literal-appended
    /// after `validate_identifier` — never a second `.format()` call (`data_usage.py:317`).
    /// `cadence` is required.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] with `field: "id"` if `device_mac` is not a single
    /// path-segment identifier, in addition to every case [`DataUsageApi::get_data_usage`]
    /// returns.
    #[allow(clippy::too_many_arguments)]
    pub async fn get_device_usage(
        &self,
        network_id: &str,
        device_mac: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        self.get_child_usage(
            &routes::data_usage::GET_DEVICES_DATA_USAGE,
            network_id,
            device_mac,
            start,
            end,
            cadence,
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/eeros/summary` — aggregated data-usage summary
    /// across every eero on a network.
    ///
    /// Ported from `DataUsageAPI.get_eeros_summary` (`data_usage.py:327-368`). `cadence` is
    /// required.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_data_usage`].
    pub async fn get_eeros_summary(
        &self,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence(cadence)?;
        self.get_usage(
            &routes::data_usage::GET_EEROS_DATA_USAGE_SUMMARY,
            network_id,
            start,
            end,
            Some(cadence),
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/eeros/{eero_id}` — data-usage statistics for a
    /// single eero.
    ///
    /// Ported from `DataUsageAPI.get_eero_usage` (`data_usage.py:370-413`). `cadence` is
    /// required.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_device_usage`].
    #[allow(clippy::too_many_arguments)]
    pub async fn get_eero_usage(
        &self,
        network_id: &str,
        eero_id: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        self.get_child_usage(
            &routes::data_usage::GET_EEROS_DATA_USAGE,
            network_id,
            eero_id,
            start,
            end,
            cadence,
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/profiles/{profile_id}` — data-usage statistics
    /// for a single profile.
    ///
    /// Ported from `DataUsageAPI.get_profile_usage` (`data_usage.py:415-458`). `cadence` is
    /// required.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_device_usage`].
    #[allow(clippy::too_many_arguments)]
    pub async fn get_profile_usage(
        &self,
        network_id: &str,
        profile_id: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        self.get_child_usage(
            &routes::data_usage::GET_PROFILES_DATA_USAGE,
            network_id,
            profile_id,
            start,
            end,
            cadence,
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/unprofiled/devices` — data-usage statistics
    /// for devices with no profile.
    ///
    /// Ported from `DataUsageAPI.get_unprofiled_devices` (`data_usage.py:460-497`). `cadence` is
    /// optional.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_data_usage`].
    pub async fn get_unprofiled_devices(
        &self,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: Option<&str>,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence_opt(cadence)?;
        self.get_usage(
            &routes::data_usage::GET_UNPROFILED_DEVICES_DATA_USAGE,
            network_id,
            start,
            end,
            cadence,
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/unprofiled/summary` — aggregated data-usage
    /// summary for unprofiled devices.
    ///
    /// Ported from `DataUsageAPI.get_unprofiled_summary` (`data_usage.py:499-540`). `cadence` is
    /// required.
    ///
    /// # Errors
    ///
    /// See [`DataUsageApi::get_data_usage`].
    pub async fn get_unprofiled_summary(
        &self,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence(cadence)?;
        self.get_usage(
            &routes::data_usage::GET_UNPROFILED_DATA_USAGE_SUMMARY,
            network_id,
            start,
            end,
            Some(cadence),
            timezone,
            parent,
        )
        .await
    }

    /// `GET /2.2/networks/{network_id}/data_usage/report_settings` — the network's data-usage
    /// report settings. No query parameters at all.
    ///
    /// Ported from `DataUsageAPI.get_report_settings` (`data_usage.py:542-566`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] if `parent`'s own `url` field (when present) is malformed.
    /// Returns `Error::Authentication("Not authenticated")` if no valid session is configured,
    /// or whatever other status-mapped [`Error`] the request produces otherwise.
    pub async fn get_report_settings(
        &self,
        network_id: &str,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let id_or_url = self.network_id_or_self_url(network_id, parent)?;
        let url = routes::data_usage::GET_DATA_USAGE_REPORT_SETTINGS.resolve(
            self.transport.api_host(),
            &id_or_url,
            None,
        )?;
        self.transport
            .request(Method::GET, url, &[], RequestBody::None)
            .await
    }

    /// `PUT /2.2/networks/{network_id}/data_usage/report_settings` — set the network's
    /// data-usage report settings.
    ///
    /// Ported from `DataUsageAPI.set_report_settings` (`data_usage.py:568-621`), the only write
    /// in this module: JSON body `{"cadence": cadence, "notification_day": notification_day}`
    /// (`data_usage.py:620`). `cadence` is validated before this crate's own "not authenticated"
    /// precondition even runs (`data_usage.py:614`) — matched here by validating it before the
    /// underlying [`Transport::request`] call. Logs the fixed uncharacterised-write warning
    /// before issuing the request (`data_usage.py:616`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] with `field: "cadence"` if `cadence` is not `"hourly"` or
    /// `"daily"`, before any request is sent. Otherwise as [`DataUsageApi::get_report_settings`].
    pub async fn set_report_settings(
        &self,
        network_id: &str,
        cadence: &str,
        notification_day: &str,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let cadence = validated_cadence(cadence)?;
        let id_or_url = self.network_id_or_self_url(network_id, parent)?;
        let url = routes::data_usage::SET_DATA_USAGE_REPORT_SETTINGS.resolve(
            self.transport.api_host(),
            &id_or_url,
            None,
        )?;
        crate::links::warn_uncharacterised_write("set data usage report settings for network");
        self.transport
            .request(
                Method::PUT,
                url,
                &[],
                RequestBody::Json(json!({
                    "cadence": cadence,
                    "notification_day": notification_day,
                })),
            )
            .await
    }

    /// Shared GET-with-query-only request builder for every network-level (non-per-item)
    /// resource in this module.
    #[allow(clippy::too_many_arguments)]
    async fn get_usage(
        &self,
        route: &Resource,
        network_id: &str,
        start: &str,
        end: &str,
        cadence: Option<&str>,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let id_or_url = self.network_id_or_self_url(network_id, parent)?;
        let url = route.resolve(self.transport.api_host(), &id_or_url, None)?;
        let query = usage_query(start, end, cadence, timezone);
        self.transport
            .request(Method::GET, url, &query, RequestBody::None)
            .await
    }

    /// Shared GET-with-query-only request builder for every per-item (device/eero/profile)
    /// resource in this module: resolves `route`'s collection URL, then literal-appends `child`
    /// after validating it as a single path-segment identifier.
    #[allow(clippy::too_many_arguments)]
    ///
    /// Validates `child` (a bare id) **before** `cadence` — mirroring Python's own evaluation
    /// order at every one of this method's three call sites (`data_usage.py:317,405,450`), where
    /// `_validate_child_id(...)` is embedded directly in the f-string argument expression handed
    /// to `_get_usage`, so it raises before `_get_usage`'s own `cadence` check ever runs
    /// (phase-G fix list item 15).
    async fn get_child_usage(
        &self,
        route: &Resource,
        network_id: &str,
        child: &str,
        start: &str,
        end: &str,
        cadence: &str,
        timezone: Option<&str>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let validated_child = crate::links::validate_identifier(child)?;
        let cadence = validated_cadence(cadence)?;
        let id_or_url = self.network_id_or_self_url(network_id, parent)?;
        let collection = route.resolve(self.transport.api_host(), &id_or_url, None)?;
        let url = append_path(&collection, validated_child)?;
        let query = usage_query(start, end, Some(cadence), timezone);
        self.transport
            .request(Method::GET, url, &query, RequestBody::None)
            .await
    }

    /// Resolves `network_id`'s id-or-self-url for use as a [`Resource`]'s `id_or_url`: prefers
    /// `parent`'s own published `url` ([`crate::links::self_url`]) when present, falling back to
    /// the bare `network_id` unchanged.
    ///
    /// Every `DataUsageAPI` method resolves its network segment via
    /// `resolve_network_url(network_id, parent)` (`data_usage.py:97-153`'s shared `_get_usage`),
    /// which prefers `parent`'s own `self_url` over the `network_id` template exactly like
    /// [`crate::params::resolve_network_url`] does. `Resource::resolve`'s own `parent` argument
    /// only supports a *named* link inside a *different* resource's `resources` map
    /// (`sub_resource_url`), not "prefer this same resource's own `url`" — so this helper performs
    /// that preference itself, up front, and hands the result to `Resource::resolve` as a bare
    /// `id_or_url` with `parent: None`: when `parent`'s `self_url` is present, the returned string
    /// is an absolute `http(s)://` URL on this transport's host, which `Resource::resolve`'s
    /// underlying `resource_url` already knows how to validate and append a template's suffix
    /// onto (the same branch an API-returned absolute URL takes) — so no separate "self-url
    /// preferred" branch needs to exist inside `Resource`/`Nested` itself.
    fn network_id_or_self_url(
        &self,
        network_id: &str,
        parent: Option<&Value>,
    ) -> Result<String, Error> {
        if let Some(parent) = parent
            && let Some(self_url) = crate::links::self_url(self.transport.api_host(), parent)?
        {
            return Ok(self_url.to_string());
        }
        Ok(network_id.to_owned())
    }
}

/// Validates a required `cadence` value against [`params::CADENCE_VALUES`] (`"hourly"`/
/// `"daily"`).
fn validated_cadence(cadence: &str) -> Result<&str, Error> {
    params::validate_cadence(cadence, params::CADENCE_VALUES)
}

/// Validates an optional `cadence` value, only when supplied.
fn validated_cadence_opt(cadence: Option<&str>) -> Result<Option<&str>, Error> {
    cadence.map(validated_cadence).transpose()
}

/// Builds the `start`/`end`/`cadence`/`timezone` query-parameter list every `DataUsageAPI`
/// method shares: `start`/`end` are always present, `cadence`/`timezone` only when `Some`.
fn usage_query(
    start: &str,
    end: &str,
    cadence: Option<&str>,
    timezone: Option<&str>,
) -> Vec<(&'static str, String)> {
    let mut query = vec![("start", start.to_owned()), ("end", end.to_owned())];
    if let Some(cadence) = cadence {
        query.push(("cadence", cadence.to_owned()));
    }
    if let Some(timezone) = timezone {
        query.push(("timezone", timezone.to_owned()));
    }
    query
}

/// Appends a validated child path segment to an already-resolved collection URL.
fn append_path(base: &Url, child: &str) -> Result<Url, Error> {
    let joined = format!("{}/{child}", base.as_str().trim_end_matches('/'));
    Url::parse(&joined).map_err(|err| Error::validation("url", format!("not a valid URL: {err}")))
}