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
//! `DnsPoliciesApi`: `dns_policies` endpoints (`eero-api src/eero/api/dns_policies.py`, new in
//! v8.0.0).
//!
//! Premium (Eero Plus/Secure) feature family; replaces the removed `ProfilesAPI` content-filter/
//! block-list/blocked-application writes (`crate::endpoints::profiles`'s module docs) — those
//! never persisted (silent no-op, live-verified). No `DELETE` verb anywhere in this module:
//! removal is expressed as `is_delete: true` on the same `PUT`.
//!
//! Not exposed by this module (and deliberately not ported): network-level and profile-level
//! DNS-policy *settings* (the twelve boolean content-category toggles such as `ad_block`,
//! `block_malware`, `safe_search_enabled`, and the ad-block on/off switches). No field on any
//! envelope this SDK can read carries a URL for them (`dns_policies.py:26-40`'s own module
//! docstring); hardcoding a guessed literal path would be unverifiable.

use std::sync::Arc;

use serde_json::{Map, Value, json};

use crate::envelope::Envelope;
use crate::error::Error;
use crate::links;
use crate::routes::{self, Resource};
use crate::transport::{RequestBody, Transport};
use crate::util::id_from_url;

/// Builds a request body from required fields plus only the supplied optional ones.
///
/// Ported from `_payload` (`dns_policies.py:50-65`).
fn payload(required: &[(&str, Value)], optional: &[(&str, Option<Value>)]) -> Value {
    let mut map = Map::new();
    for (key, value) in required {
        map.insert((*key).to_owned(), value.clone());
    }
    for (key, value) in optional {
        if let Some(value) = value {
            map.insert((*key).to_owned(), value.clone());
        }
    }
    Value::Object(map)
}

/// `eero-api`'s `DnsPoliciesAPI` (`src/eero/api/dns_policies.py`, new in v8.0.0).
#[derive(Debug)]
pub struct DnsPoliciesApi {
    transport: Arc<Transport>,
}

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

    /// Resolves `route`'s URL for `network_id`/`parent`, logs the fixed uncharacterised-write
    /// `operation` warning, then `PUT`s `body` — in that exact order, mirroring every write in
    /// `dns_policies.py` (resolve the URL, build the payload, warn, then `put(...)`), rather than
    /// warning before URL resolution/validation can fail.
    async fn put_policy(
        &self,
        route: &Resource,
        network_id: &str,
        parent: Option<&Value>,
        operation: &str,
        body: Value,
    ) -> Result<Envelope, Error> {
        let url = route.resolve(self.transport.api_host(), network_id, parent)?;
        links::warn_uncharacterised_write(operation);
        self.transport
            .request(route.method.clone(), url, &[], RequestBody::Json(body))
            .await
    }

    /// Gets the network's advanced content filter allow/block lists — returns the raw Eero API
    /// response. Live-verified.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:129-163`
    /// (`DnsPoliciesAPI.get_advanced_content_filter`): sends `GET`
    /// `routes::dns_policies::DNS_POLICIES_GET_ADVANCED_CONTENT_FILTER`, preferring the parent
    /// network envelope's own `advanced_content_filter` link when supplied and resolvable.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Authentication`] if no valid session is configured,
    /// `Error::PremiumRequired` if the network is not on Eero Plus/Secure, or whatever other
    /// status-mapped [`Error`] the request produces.
    pub async fn get_advanced_content_filter(
        &self,
        network_id: &str,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        self.transport
            .resource(
                &routes::dns_policies::DNS_POLICIES_GET_ADVANCED_CONTENT_FILTER,
                network_id,
                parent,
                &[],
                RequestBody::None,
            )
            .await
    }

    /// Adds (or, with `is_delete: Some(true)`, removes) a domain from the network-wide allow
    /// list — returns the raw Eero API response.
    ///
    /// Only `domain` plus whichever optional fields are `Some` are sent; every `None` optional is
    /// omitted from the request entirely.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:167-230` (`DnsPoliciesAPI.allow_domain`):
    /// sends `PUT` `routes::dns_policies::DNS_POLICIES_ALLOW_DOMAIN`. Logs one
    /// `warn_uncharacterised_write("allow domain for network")` immediately before the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_advanced_content_filter`].
    #[allow(clippy::too_many_arguments)] // mirrors dns_policies.py:167-176's own signature
    pub async fn allow_domain(
        &self,
        network_id: &str,
        domain: &str,
        add_cname: Option<bool>,
        reason_to_allow: Option<i64>,
        is_delete: Option<bool>,
        keep_profiles: Option<&[&str]>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let body = payload(
            &[("domain", Value::String(domain.to_owned()))],
            &[
                ("add_cname", add_cname.map(Value::Bool)),
                ("reason_to_allow", reason_to_allow.map(Into::into)),
                ("is_delete", is_delete.map(Value::Bool)),
                ("keep_profiles", keep_profiles.map(to_string_array)),
            ],
        );
        self.put_policy(
            &routes::dns_policies::DNS_POLICIES_ALLOW_DOMAIN,
            network_id,
            parent,
            "allow domain for network",
            body,
        )
        .await
    }

    /// Allows a list of CNAME domains network-wide — returns the raw Eero API response.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:233-272` (`DnsPoliciesAPI.allow_cnames`):
    /// sends `PUT` `routes::dns_policies::DNS_POLICIES_ALLOW_CNAMES` with body `{"domains":
    /// domains}` — this endpoint declares only the `domains` field. Logs one
    /// `warn_uncharacterised_write("allow CNAMEs for network")` immediately before the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_advanced_content_filter`].
    pub async fn allow_cnames(
        &self,
        network_id: &str,
        domains: &[&str],
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        self.put_policy(
            &routes::dns_policies::DNS_POLICIES_ALLOW_CNAMES,
            network_id,
            parent,
            "allow CNAMEs for network",
            json!({ "domains": domains }),
        )
        .await
    }

    /// Adds (or, with `is_delete: Some(true)`, removes) a domain from the network-wide block
    /// list — returns the raw Eero API response.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:275-324` (`DnsPoliciesAPI.block_domain`):
    /// sends `PUT` `routes::dns_policies::DNS_POLICIES_BLOCK_DOMAIN`. Logs one
    /// `warn_uncharacterised_write("block domain for network")` immediately before the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_advanced_content_filter`].
    pub async fn block_domain(
        &self,
        network_id: &str,
        domain: &str,
        is_delete: Option<bool>,
        keep_profiles: Option<&[&str]>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let body = payload(
            &[("domain", Value::String(domain.to_owned()))],
            &[
                ("is_delete", is_delete.map(Value::Bool)),
                ("keep_profiles", keep_profiles.map(to_string_array)),
            ],
        );
        self.put_policy(
            &routes::dns_policies::DNS_POLICIES_BLOCK_DOMAIN,
            network_id,
            parent,
            "block domain for network",
            body,
        )
        .await
    }

    /// Adds (or, with `is_delete: Some(true)`, removes) a domain from one or more profiles'
    /// allow lists — returns the raw Eero API response.
    ///
    /// `domain` and `profiles` are always sent; the remaining fields only when `Some`.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:327-389`
    /// (`DnsPoliciesAPI.allow_domain_for_profiles`): sends `PUT`
    /// `routes::dns_policies::DNS_POLICIES_ALLOW_DOMAIN_FOR_PROFILES`. Logs one
    /// `warn_uncharacterised_write("allow domain for profiles on network")` immediately before
    /// the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_advanced_content_filter`].
    #[allow(clippy::too_many_arguments)] // mirrors dns_policies.py:327-336's own signature
    pub async fn allow_domain_for_profiles(
        &self,
        network_id: &str,
        domain: &str,
        profiles: &[&str],
        override_: Option<bool>,
        add_cname: Option<bool>,
        reason_to_allow: Option<i64>,
        is_delete: Option<bool>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let body = payload(
            &[
                ("domain", Value::String(domain.to_owned())),
                ("profiles", to_string_array(profiles)),
            ],
            &[
                ("override", override_.map(Value::Bool)),
                ("add_cname", add_cname.map(Value::Bool)),
                ("reason_to_allow", reason_to_allow.map(Into::into)),
                ("is_delete", is_delete.map(Value::Bool)),
            ],
        );
        self.put_policy(
            &routes::dns_policies::DNS_POLICIES_ALLOW_DOMAIN_FOR_PROFILES,
            network_id,
            parent,
            "allow domain for profiles on network",
            body,
        )
        .await
    }

    /// Allows a list of CNAME domains for one or more profiles — returns the raw Eero API
    /// response.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:392-432`
    /// (`DnsPoliciesAPI.allow_cnames_for_profiles`): sends `PUT`
    /// `routes::dns_policies::DNS_POLICIES_ALLOW_CNAMES_FOR_PROFILES` with body `{"domains":
    /// domains, "profiles": profiles}`. Logs one `warn_uncharacterised_write("allow CNAMEs for
    /// profiles on network")` immediately before the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_advanced_content_filter`].
    pub async fn allow_cnames_for_profiles(
        &self,
        network_id: &str,
        domains: &[&str],
        profiles: &[&str],
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        self.put_policy(
            &routes::dns_policies::DNS_POLICIES_ALLOW_CNAMES_FOR_PROFILES,
            network_id,
            parent,
            "allow CNAMEs for profiles on network",
            json!({ "domains": domains, "profiles": profiles }),
        )
        .await
    }

    /// Adds (or, with `is_delete: Some(true)`, removes) a domain from one or more profiles'
    /// block lists — returns the raw Eero API response.
    ///
    /// `domain` and `profiles` are always sent; `is_delete`/`override_` only when `Some`.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:437-488`
    /// (`DnsPoliciesAPI.block_domain_for_profiles`): sends `PUT`
    /// `routes::dns_policies::DNS_POLICIES_BLOCK_DOMAIN_FOR_PROFILES`. Logs one
    /// `warn_uncharacterised_write("block domain for profiles on network")` immediately before
    /// the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_advanced_content_filter`].
    pub async fn block_domain_for_profiles(
        &self,
        network_id: &str,
        domain: &str,
        profiles: &[&str],
        is_delete: Option<bool>,
        override_: Option<bool>,
        parent: Option<&Value>,
    ) -> Result<Envelope, Error> {
        let body = payload(
            &[
                ("domain", Value::String(domain.to_owned())),
                ("profiles", to_string_array(profiles)),
            ],
            &[
                ("is_delete", is_delete.map(Value::Bool)),
                ("override", override_.map(Value::Bool)),
            ],
        );
        self.put_policy(
            &routes::dns_policies::DNS_POLICIES_BLOCK_DOMAIN_FOR_PROFILES,
            network_id,
            parent,
            "block domain for profiles on network",
            body,
        )
        .await
    }

    /// Gets the applications a profile can block, and which are blocked — returns the raw Eero
    /// API response. Live-verified.
    ///
    /// `profile_id` is first normalised to its trailing id via [`id_from_url`] (a no-op for an
    /// already-bare id) before being substituted into the `dns_policies/profiles/{id}` sub-tree —
    /// a profile's own path/URL (`networks/{id}/profiles/{pid}`) cannot be mapped onto that
    /// sub-tree by template substitution, since the two resource families share only the trailing
    /// id (`dns_policies.py:77-92`). Takes no `parent`: this sub-tree publishes no link on any
    /// envelope this SDK can read.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:491-521`
    /// (`DnsPoliciesAPI.get_profile_applications`): sends `GET`
    /// `routes::dns_policies::DNS_POLICIES_GET_PROFILE_APPLICATIONS`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] if `profile_id` cannot be normalised to a trailing path
    /// segment (see [`id_from_url`]). Otherwise, see
    /// [`DnsPoliciesApi::get_advanced_content_filter`].
    pub async fn get_profile_applications(
        &self,
        network_id: &str,
        profile_id: &str,
    ) -> Result<Envelope, Error> {
        let child = id_from_url(profile_id)?;
        self.transport
            .nested(
                &routes::dns_policies::DNS_POLICIES_GET_PROFILE_APPLICATIONS,
                network_id,
                &child,
                None,
                &[],
                RequestBody::None,
            )
            .await
    }

    /// Sets the applications blocked for a profile — returns the raw Eero API response.
    ///
    /// **Fully replaces** the profile's blocked-applications list; there is no add/remove
    /// primitive. Same `profile_id` normalisation as
    /// [`DnsPoliciesApi::get_profile_applications`]. Takes no `parent`.
    ///
    /// Ported from `eero-api src/eero/api/dns_policies.py:521-551`
    /// (`DnsPoliciesAPI.set_profile_blocked_applications`): sends `PUT`
    /// `routes::dns_policies::DNS_POLICIES_SET_PROFILE_BLOCKED_APPLICATIONS` with body
    /// `{"applications": applications}`. Logs one `warn_uncharacterised_write("set blocked
    /// applications for profile on network")` immediately before the request.
    ///
    /// # Errors
    ///
    /// See [`DnsPoliciesApi::get_profile_applications`].
    pub async fn set_profile_blocked_applications(
        &self,
        network_id: &str,
        profile_id: &str,
        applications: &[&str],
    ) -> Result<Envelope, Error> {
        let child = id_from_url(profile_id)?;
        let url = routes::dns_policies::DNS_POLICIES_SET_PROFILE_BLOCKED_APPLICATIONS.resolve(
            self.transport.api_host(),
            network_id,
            &child,
            None,
        )?;
        links::warn_uncharacterised_write("set blocked applications for profile on network");
        self.transport
            .request(
                routes::dns_policies::DNS_POLICIES_SET_PROFILE_BLOCKED_APPLICATIONS
                    .method
                    .clone(),
                url,
                &[],
                RequestBody::Json(json!({ "applications": applications })),
            )
            .await
    }
}

/// Converts a slice of string references into a JSON array of strings.
fn to_string_array(values: &[&str]) -> Value {
    Value::Array(
        values
            .iter()
            .map(|v| Value::String((*v).to_owned()))
            .collect(),
    )
}