Skip to main content

dna_rs/ops/
tld.rs

1use crate::client::DnaClient;
2use crate::error::DnaResult;
3use crate::models::TldListResponse;
4use crate::models::tld::{TldInfo, TldItem};
5use crate::ops::util::parse_tld_pricing;
6
7impl DnaClient {
8    /// Fetch TLD list and pricing matrix.
9    pub async fn get_tld_list(
10        &self,
11        result_count: u32,
12        skip_count: u32,
13    ) -> DnaResult<TldListResponse> {
14        let query = [
15            ("MaxResultCount", result_count.to_string()),
16            ("SkipCount", skip_count.to_string()),
17        ];
18        let raw: serde_json::Value = self.http.get("products/tlds", Some(&query)).await?;
19
20        let total_count = raw
21            .get("totalCount")
22            .and_then(serde_json::Value::as_u64)
23            .unwrap_or(0) as u32;
24
25        let items = raw
26            .get("items")
27            .and_then(serde_json::Value::as_array)
28            .cloned()
29            .unwrap_or_default();
30
31        let mut result = Vec::with_capacity(items.len());
32
33        for (idx, tld_val) in items.into_iter().enumerate() {
34            let tld: TldItem = serde_json::from_value(tld_val)?;
35
36            let min_char = tld
37                .constraints
38                .as_ref()
39                .and_then(|c| c.min_length)
40                .unwrap_or(1);
41            let max_char = tld
42                .constraints
43                .as_ref()
44                .and_then(|c| c.max_length)
45                .unwrap_or(63);
46            let (pricing, currencies) = parse_tld_pricing(&tld.prices);
47
48            result.push(TldInfo {
49                id: (idx + 1) as u32,
50                tld: tld.name.unwrap_or_default(),
51                status: tld.status.unwrap_or_else(|| "Active".into()),
52                min_char,
53                max_char,
54                min_period: tld.min_registration_period.unwrap_or(1),
55                max_period: tld.max_registration_period.unwrap_or(10),
56                pricing,
57                currencies,
58            });
59        }
60
61        Ok(TldListResponse {
62            tld_items: result,
63            total_count,
64        })
65    }
66}