Skip to main content

dinio_sdk/api/
free_domains.rs

1//! `/api/v1/free-domains` (`../core/src/api/free_domains.ts`). Public: no key needed.
2//! Leasing is [`Zones::create`](crate::api::Zones::create) with a name directly below a
3//! free domain.
4
5use reqwest::Method;
6
7use crate::client::Client;
8use crate::error::Result;
9use crate::models::{FreeDomainList, FreeLabelAvailability};
10
11/// Free domain routes, from [`Client::free_domains`]. They work without an API key.
12pub struct FreeDomains<'a> {
13    client: &'a Client,
14}
15
16impl<'a> FreeDomains<'a> {
17    pub(crate) fn new(client: &'a Client) -> Self {
18        Self { client }
19    }
20
21    /// The free domains and the per-user lease limit.
22    ///
23    /// `GET /api/v1/free-domains`
24    pub async fn list(&self) -> Result<FreeDomainList> {
25        let request = self.client.request(Method::GET, &["free-domains"])?;
26        Ok(self.client.send(request).await?.0)
27    }
28
29    /// Whether `<label>.<domain>` can be leased now. The answer can change before the
30    /// lease; [`Zones::create`](crate::api::Zones::create) checks again. The caller's own
31    /// lease limit is not checked here.
32    ///
33    /// `GET /api/v1/free-domains/:domain/available?label=`
34    ///
35    /// # Errors
36    ///
37    /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if `domain` is not a free
38    ///   domain.
39    /// * [`ErrorCode::Validation`](crate::ErrorCode::Validation) for an empty label.
40    ///
41    /// # Examples
42    ///
43    /// ```no_run
44    /// # async fn example(client: &dinio_sdk::Client) -> dinio_sdk::Result<()> {
45    /// let check = client.free_domains().available("dinio.eu", "myhome").await?;
46    /// if check.available {
47    ///     client.zones().create(&check.name).await?;
48    /// } else {
49    ///     println!("{}: {}", check.name, check.reason.unwrap_or_default());
50    /// }
51    /// # Ok(())
52    /// # }
53    /// ```
54    pub async fn available(&self, domain: &str, label: &str) -> Result<FreeLabelAvailability> {
55        let request = self
56            .client
57            .request(Method::GET, &["free-domains", domain, "available"])?
58            .query(&[("label", label)]);
59        Ok(self.client.send(request).await?.0)
60    }
61}