porkbun-api 1.0.4

an async implementation of porkbun's domain management api.
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#![warn(missing_docs)]
//! # porkbun-api
//!
//! this crate provides an async implementation of [porkbun](https://porkbun.com)'s domain management [api](https://porkbun.com/api/json/v3/documentation).
//! It provides a transport-agnostic [Client], and a [DefaultTransport] based on hyper suitable for use in tokio-based applications.
//!
//! ## example
//!
//! ```no_run
//! use porkbun_api::{Client, ApiKey, CreateOrEditDnsRecord};
//! use porkbun_api::transport::DefaultTransportError;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), porkbun_api::Error<DefaultTransportError>> {
//!     let api_key = ApiKey::new("secret", "api_key");
//!     let client = Client::new(api_key);
//!
//!     let domain = &client.domains().await?[0].domain;
//!     let subdomain = Some("my.ip");
//!     let my_ip = client.ping().await?;
//!     let record = CreateOrEditDnsRecord::A_or_AAAA(subdomain, my_ip);
//!     let id = client.create(domain, record).await?;
//!     println!("added record {id}");
//!     client.delete(domain, &id).await?;
//!     println!("removed record {id}");
//!     Ok(())
//! }
//! ```
//!
//! ## Features
//!
//! - `default-client` enabled by default. Includes a default transport layer implementation for the [Client]. This can be disabled if you are implementing your own.
//! - `tracing` optional. When enabled, adds `#[instrument]` spans on all public API methods
//!   and emits events in the Default transport layer.
//!   HTTP-level request/response tracing is intentionally omitted — if you need it,
//!   wrap [`DefaultTransport`] in your own [`MakeRequest`](transport::MakeRequest)
//!   implementation that adds spans. See `examples/http_tracing.rs` for a working example.
//!
//! ## known issues with the porkbun api
//!
//! Hostnames are a subset of DNS names. `🦆.example.com` is a valid DNS name for example, but it is not a valid hostname.
//! The porkbun api _will_ let you set an entry for `🦆.example.com`, but if you then try to query it, it will be returned as `??.example.com`. This is an issue with the porkbun servers.
//!
//! Also, the porkbun api server can also be quite slow, sometimes taking several seconds before it accepts an api call. Keep this in mind when integrating this library within a larger application.

mod serde_util;

pub mod transport;
#[cfg(feature = "default-client")]
use transport::DefaultTransport;
use transport::MakeRequest;
mod error;
pub use error::Error;
use error::{ApiErrorMessage, ApiResponse, ErrorImpl};
mod uri;

use chrono::NaiveDateTime;
use http_body_util::{BodyExt, Full};
use hyper::{
    body::{Body, Bytes},
    Request, StatusCode, Uri,
};
use serde::{Deserialize, Serialize};
use std::{
    borrow::Cow,
    collections::HashMap,
    fmt::Display,
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
    time::Duration,
};

/// Holds the credentials needed to access the API
#[derive(Deserialize, Serialize, Clone)]
pub struct ApiKey {
    secretapikey: String,
    apikey: String,
}

impl ApiKey {
    /// Creates a new [ApiKey] from the given API secret and API key.
    pub fn new(secret: impl Into<String>, api_key: impl Into<String>) -> Self {
        Self {
            secretapikey: secret.into(),
            apikey: api_key.into(),
        }
    }
}

/// Valid DNS record types
#[allow(missing_docs)]
#[derive(Clone, Copy, Deserialize, Serialize, Debug, PartialEq, Eq)]
pub enum DnsRecordType {
    A,
    MX,
    CNAME,
    ALIAS,
    TXT,
    NS,
    AAAA,
    SRV,
    TLSA,
    CAA,
    HTTPS,
    SVCB,
}

impl Display for DnsRecordType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::A => "A",
            Self::MX => "MX",
            Self::CNAME => "CNAME",
            Self::ALIAS => "ALIAS",
            Self::TXT => "TXT",
            Self::NS => "NS",
            Self::AAAA => "AAAA",
            Self::SRV => "SRV",
            Self::TLSA => "TLSA",
            Self::CAA => "CAA",
            Self::HTTPS => "HTTPS",
            Self::SVCB => "SVCB",
        })
    }
}

/// create, or edit with a DNS record with a domain/id pair
#[derive(Debug, Serialize, PartialEq, Eq)]
pub struct CreateOrEditDnsRecord<'a> {
    /// The subdomain for the record being created, not including the domain itself. Leave blank to create a record on the root domain. Use * to create a wildcard record.
    #[serde(rename = "name")]
    pub subdomain: Option<&'a str>,
    /// The type of record that should be created
    #[serde(rename = "type")]
    pub record_type: DnsRecordType,
    /// The answer content for the record.
    pub content: Cow<'a, str>,
    /// The time to live in seconds for the record. The minimum and the default is 600 seconds.
    pub ttl: Option<u64>,
    /// The priority of the record for those that support it.
    //these get returned as strings, might be we can set these to non-standard values?
    pub prio: u32,
    // you'd expect a comment field here, but its missing from the api 🥲
    // doesn't seem to be notes, note, or comments
    //todo: ask if there is an api? the web interface seems to use a different api. including one with bulk mgmt
}

impl<'a> CreateOrEditDnsRecord<'a> {
    /// Makes a new [CreateOrEditDnsRecord] for the given subdomain, with the given record type and content.
    pub fn new(
        subdomain: Option<&'a str>,
        record_type: DnsRecordType,
        content: impl Into<Cow<'a, str>>,
    ) -> Self {
        Self {
            subdomain,
            record_type,
            content: content.into(),
            ttl: None,
            prio: 0,
        }
    }
    /// Makes a new [CreateOrEditDnsRecord]  for creating an A-record for the given subdomain, with the given IP address as a response.
    #[allow(non_snake_case)]
    pub fn A(subdomain: Option<&'a str>, ip: Ipv4Addr) -> Self {
        Self::new(subdomain, DnsRecordType::A, Cow::Owned(ip.to_string()))
    }
    /// Makes a new [CreateOrEditDnsRecord] for creating an AAAA-record for the given subdomain, with the given IP address as a response.
    #[allow(non_snake_case)]
    pub fn AAAA(subdomain: Option<&'a str>, ip: Ipv6Addr) -> Self {
        Self::new(subdomain, DnsRecordType::AAAA, Cow::Owned(ip.to_string()))
    }
    /// Makes a new [CreateOrEditDnsRecord] for creating an A- or AAAA-record (depending on the value of the ip address) for the given subdomain, with the given IP address as a response.
    #[allow(non_snake_case)]
    pub fn A_or_AAAA(subdomain: Option<&'a str>, ip: IpAddr) -> Self {
        match ip {
            IpAddr::V4(my_ip) => Self::A(subdomain, my_ip),
            IpAddr::V6(my_ip) => Self::AAAA(subdomain, my_ip),
        }
    }

    /// Set the time-to-live for this record.
    /// The minimum and the default is 600 seconds. Any value less than 600 seconds will be rounded up.
    #[must_use]
    pub fn with_ttl(self, ttl: Option<Duration>) -> Self {
        Self {
            ttl: ttl.as_ref().map(Duration::as_secs),
            ..self
        }
    }

    /// Set the priority for this record, for records that support it.
    #[must_use]
    pub fn with_priority(self, prio: u32) -> Self {
        Self { prio, ..self }
    }
}

//might be an integer actually but sometimes sends a string
// so we opt to store it as a string just in case it can start with
// a '0'
// todo: ask about this
#[derive(Deserialize, Debug)]
struct EntryId {
    #[serde(with = "serde_util::string_or_int")]
    id: String,
}

/// A DNS entry for a domain, returned by the API
#[derive(Deserialize, Debug)]
pub struct DnsEntry {
    /// the unique ID of this entry
    #[serde(with = "serde_util::string_or_int")]
    pub id: String,
    /// the full name of the entry, e.g. `_atproto.example.com`
    pub name: String,
    /// the type of record, e.g. A or TXT.
    #[serde(rename = "type")]
    pub record_type: DnsRecordType,
    /// the content of this record.
    pub content: String,
    /// the time-to-live of this record
    //string in docs
    #[serde(with = "serde_util::u64_from_string_or_int")]
    pub ttl: u64,
    /// The priority of this record
    //string in docs
    #[serde(default, with = "serde_util::u64_from_string_or_int")]
    pub prio: u64,
    /// Any notes set for this record.
    /// Note that you can not set these from the API itself, you have to do so with the management console on the websiter.
    pub notes: Option<String>,
}

#[derive(Deserialize, Debug)]
struct DnsRecordsByDomainOrIDResponse {
    records: Vec<DnsEntry>,
}

/// The default pricing for the registration, renewal and transfer of a given TLD.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Pricing {
    /// the registration price.
    pub registration: String,
    /// the renewal price.
    pub renewal: String,
    /// the transfer price.
    pub transfer: String,
    /// A field indicating that his is a "special" domain, and if so what kind.
    /// Currently this only valid version seems to be ["handshake"](https://porkbun.com/handshake)
    ///
    /// This field is undocumented by porkbun, but I included it anyways to let people filter out these TLDs.
    //todo: ask
    //undocumented field, helps filter out stupid handshake domains
    pub special_type: TldType,
}

impl Pricing {
    /// returns true if this is a normal ICANN/IANA TLDs like .com, .engineering or .gay
    pub fn is_icann(&self) -> bool {
        self.special_type.is_icann()
    }
}

/// Describes what registry a TLD belongs to.
#[derive(Debug)]
pub enum TldType {
    /// The normal ICANN/IANA TLDs like .com, .engineering or .gay
    /// you probably want one of these
    Normal,
    /// An [experimental blockchain protocol](https://porkbun.com/handshake).
    Handshake,
    /// An unknown registery.
    ///
    /// at the time of writting, porkbun only supports ICANN and Handshake, but this variant exists for future-proofing.
    Other(String),
}

impl TldType {
    /// returns true if this is a normal ICANN/IANA TLDs like .com, .engineering or .gay
    pub fn is_icann(&self) -> bool {
        matches!(self, Self::Normal)
    }
}

impl<'de> Deserialize<'de> for TldType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let string_value = Option::<String>::deserialize(deserializer)?;
        Ok(match string_value {
            None => Self::Normal,
            Some(s) => {
                if s.eq_ignore_ascii_case("handshake") {
                    Self::Handshake
                } else {
                    Self::Other(s)
                }
            }
        })
    }
}

#[derive(Deserialize, Debug)]
struct DomainPricingResponse {
    //tld-to-pricing
    pricing: HashMap<String, Pricing>,
}

#[derive(Serialize, Deserialize)]
struct UpdateNameServers {
    ns: Vec<String>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct DomainListAll {
    /// An index to start at when retrieving the domains, defaults to 0. To get all domains increment by 1000 until you receive an empty array.
    start: usize,
    /// should be "yes"
    #[serde(default, with = "serde_util::yesno")]
    include_labels: bool,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct DomainListAllResponse {
    domains: Vec<DomainInfo>,
}

/// A domain registration returned by the API server
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DomainInfo {
    /// The registered domain, including the TLD
    pub domain: String,
    // usually ACTIVE or..
    /// The status of the domain. "ACTIVE" if active,
    pub status: String,
    /// the TLD of the domain
    pub tld: String,
    /// The date-time this domain was created
    // ask: what is the TZ of this?
    #[serde(with = "serde_util::datetime")]
    pub create_date: NaiveDateTime,
    /// The date-time this domain will expire
    #[serde(with = "serde_util::datetime")]
    pub expire_date: NaiveDateTime,
    /// whether the security lock has been turned on or not
    // docs say these are "1", probably booleans?
    #[serde(with = "serde_util::stringoneintzero")]
    pub security_lock: bool,
    #[serde(with = "serde_util::stringoneintzero")]
    /// whether whois privacy has been turned on or not
    pub whois_privacy: bool,
    /// whether auto-renewal is enabled or not
    // docs say this is a bool, is a string
    #[serde(with = "serde_util::stringoneintzero")]
    pub auto_renew: bool,
    /// whether this is an external domain or not
    #[serde(with = "serde_util::stringoneintzero")]
    pub not_local: bool,
    /// Any labels that have been assigned to this domain from the web interface
    #[serde(default)]
    pub labels: Vec<Label>,
}

/// A label added to a domain.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Label {
    /// The unique ID of the label
    #[serde(deserialize_with = "serde_util::string_or_int::deserialize")]
    pub id: String,
    /// the name of the label
    pub title: String,
    /// the color of the label (used in the web interface)
    pub color: String,
}

/// A url forwarding configuration
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Forward {
    /// The subdomain to forward, or None to forward the root domain
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subdomain: Option<String>,
    /// The location to forward to
    pub location: String,
    #[serde(rename = "type")]
    /// The type of redirect to use (permanent or temporary)
    pub forward_type: ForwardType,
    /// Whether to include the path in the forwarded request
    #[serde(with = "serde_util::yesno")]
    pub include_path: bool,
    /// Whether to forward all subdomains
    #[serde(with = "serde_util::yesno")]
    pub wildcard: bool,
}

impl Forward {
    /// creates a new [Forward] configuration with the given subdomain and location.
    /// Sets the forward_type to temporary, includes the path, and does not enable wildcard forwarding.
    pub fn new(subdomain: Option<impl Into<String>>, to: impl Into<String>) -> Self {
        Self {
            subdomain: subdomain.map(|s| s.into()),
            location: to.into(),
            forward_type: ForwardType::Temporary,
            include_path: true,
            wildcard: false,
        }
    }

    /// Enable or disable wildcard forwarding
    pub fn with_wildcard(self, wildcard: bool) -> Self {
        Self { wildcard, ..self }
    }

    /// Include or exclude the path in the forwarded request
    pub fn include_path(self, include_path: bool) -> Self {
        Self {
            include_path,
            ..self
        }
    }

    /// Set the forward type to either temporary or permanent
    pub fn with_forward_type(self, forward_type: ForwardType) -> Self {
        Self {
            forward_type,
            ..self
        }
    }
}

/// The type of redirect to use
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ForwardType {
    Temporary,
    Permanent,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct GetUrlForwardingResponse {
    forwards: Vec<ForwardWithID>,
}

/// An existing url forwarding configuration with an associated ID
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ForwardWithID {
    /// The unique ID of this forwarding configuration
    #[serde(deserialize_with = "serde_util::string_or_int::deserialize")]
    pub id: String,
    /// The configuration
    #[serde(flatten, rename = "forward")]
    pub config: Forward,
}

/// The SSL certificate bundle for a domain
#[derive(Deserialize, Debug)]
pub struct SslBundle {
    /// The complete certificate chain.
    #[serde(rename = "certificatechain")]
    pub certificate_chain: String,
    /// The private key.
    #[serde(rename = "privatekey")]
    pub private_key: String,
    /// The public key.
    #[serde(rename = "publickey")]
    pub public_key: String,
}

#[derive(Serialize)]
struct WithApiKeys<'a, T: Serialize> {
    #[serde(flatten)]
    api_key: &'a ApiKey,
    #[serde(flatten)]
    inner: T,
}

/// A client for interfacing with the Porkbun API servers
pub struct Client<P: MakeRequest> {
    inner: P,
    api_key: ApiKey,
}

#[cfg(feature = "default-client")]
impl Client<DefaultTransport> {
    /// creates a new client using the supplied api key and the default transport implementation.
    ///
    /// if you wish to change the transport layer, or you're not using tokio,  use [`new_with_transport`](Client::new_with_transport)
    pub fn new(api_key: ApiKey) -> Self {
        Client::new_with_transport(api_key, DefaultTransport::default())
    }
}

impl<T> Client<T>
where
    T: MakeRequest,
    <T::Body as Body>::Error: Into<T::Error>,
{
    /// creates a new client using the supplied api key and transport.
    ///
    /// if you don't care about the implementation details of the transport, consider using [`new`](Client::new) which uses a default implementation.
    pub fn new_with_transport(api_key: ApiKey, transport: T) -> Self {
        Self {
            inner: transport,
            api_key,
        }
    }
    async fn post<D: for<'a> Deserialize<'a>>(
        &self,
        uri: Uri,
        body: Full<Bytes>,
    ) -> Result<D, Error<T::Error>> {
        let request = Request::post(uri).body(body).unwrap(); //both uri and body are known at this point
        let resp = self
            .inner
            .request(request)
            .await
            .map_err(ErrorImpl::TransportError)?;
        let (head, body) = resp.into_parts();
        let bytes = body
            .collect()
            .await
            .map_err(|e| ErrorImpl::TransportError(e.into()))?
            .to_bytes();
        let result = std::result::Result::<_, ApiErrorMessage>::from(
            serde_json::from_slice::<ApiResponse<_>>(&bytes)
                .map_err(ErrorImpl::DeserializationError)?,
        );

        match (head.status, result) {
            (StatusCode::OK, Ok(x)) => Ok(x),
            (status, maybe_message) => Err((status, maybe_message.err()).into()),
        }
    }
    async fn post_with_api_key<S: Serialize, D: for<'a> Deserialize<'a>>(
        &self,
        uri: Uri,
        body: S,
    ) -> Result<D, Error<T::Error>> {
        let with_api_key = WithApiKeys {
            api_key: &self.api_key,
            inner: body,
        };
        let json = serde_json::to_string(&with_api_key).map_err(ErrorImpl::SerializationError)?;
        let body = http_body_util::Full::new(Bytes::from(json));
        self.post(uri, body).await
    }

    /// pings the api servers returning your ip address.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn ping(&self) -> Result<IpAddr, Error<T::Error>> {
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct PingResponse {
            your_ip: IpAddr,
        }
        let ping: PingResponse = self.post_with_api_key(uri::ping(), ()).await?;
        Ok(ping.your_ip)
    }

    /// Get a mapping of available TLDs to their pricing structure.
    /// This method does not require authentication, and it will work with any [ApiKey].
    ///
    /// This method includes all TLDs, including special ones like handshake domains.
    /// If you only want ICANN TLDs, and you probably do, use [icann_domain_pricing](Client::icann_domain_pricing) instead.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn domain_pricing(&self) -> Result<HashMap<String, Pricing>, Error<T::Error>> {
        let resp: DomainPricingResponse = self.post(uri::domain_pricing(), Full::default()).await?;
        Ok(resp.pricing)
    }

    /// Get a mapping of available TLDs to their pricing structure, filtered to only include ICANN TLDs.
    /// This method does not require authentication, and it will work with any [ApiKey].
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn icann_domain_pricing(
        &self,
    ) -> Result<impl Iterator<Item = (String, Pricing)>, Error<T::Error>> {
        let resp: DomainPricingResponse = self.post(uri::domain_pricing(), Full::default()).await?;
        Ok(resp.pricing.into_iter().filter(|(_, v)| v.is_icann()))
    }

    async fn list_domains(&self, offset: usize) -> Result<Vec<DomainInfo>, Error<T::Error>> {
        let resp: DomainListAllResponse = self
            .post_with_api_key(
                uri::domain_list_all(),
                DomainListAll {
                    start: offset,
                    include_labels: true,
                },
            )
            .await?;
        Ok(resp.domains)
    }

    /// get all the domains associated with this account
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn domains(&self) -> Result<Vec<DomainInfo>, Error<T::Error>> {
        let mut all = self.list_domains(0).await?;
        let mut last_len = all.len();
        // if paginated by 1000, we could probably get away with checking if equal to 1000 and skipping the final check
        while last_len != 0 {
            let next = self.list_domains(all.len()).await?;
            last_len = next.len();
            all.extend(next.into_iter());
        }
        Ok(all)
    }

    /// Gets all the DNS records for a given domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn get_all(&self, domain: &str) -> Result<Vec<DnsEntry>, Error<T::Error>> {
        let rsp: DnsRecordsByDomainOrIDResponse = self
            .post_with_api_key(uri::get_dns_record_by_domain_and_id(domain, None)?, ())
            .await?;
        Ok(rsp.records)
    }

    /// Gets a single DNS record for a given domain, by its unique ID.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn get_single(
        &self,
        domain: &str,
        id: &str,
    ) -> Result<Option<DnsEntry>, Error<T::Error>> {
        let rsp: DnsRecordsByDomainOrIDResponse = self
            .post_with_api_key(uri::get_dns_record_by_domain_and_id(domain, Some(id))?, ())
            .await?;
        let rsp = rsp.records.into_iter().next();
        Ok(rsp)
    }

    /// Create a new DNS record for the given domain.
    /// Will fail if there already exists a record with the same name and type.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn create(
        &self,
        domain: &str,
        cmd: CreateOrEditDnsRecord<'_>,
    ) -> Result<String, Error<T::Error>> {
        let resp: EntryId = self
            .post_with_api_key(uri::create_dns_record(domain)?, cmd)
            .await?;
        Ok(resp.id)
    }

    /// Edits an existing DNS record for a given domain, by its unique ID.
    /// IDs can be discovered by first calling [get_all](Client::get_all).
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn edit(
        &self,
        domain: &str,
        id: &str,
        cmd: CreateOrEditDnsRecord<'_>,
    ) -> Result<(), Error<T::Error>> {
        self.post_with_api_key(uri::edit_dns_record(domain, id)?, cmd)
            .await
    }

    /// Deletes an existing DNS record for a given domain, by its unique ID.
    /// IDs can be discovered by first calling [get_all](Client::get_all).
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn delete(&self, domain: &str, id: &str) -> Result<(), Error<T::Error>> {
        self.post_with_api_key(uri::delete_dns_record_by_id(domain, id)?, ())
            .await
    }

    /// Gets the configured nameservers for a particular domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn nameservers(&self, domain: &str) -> Result<Vec<String>, Error<T::Error>> {
        let resp: UpdateNameServers = self
            .post_with_api_key(uri::get_name_servers(domain)?, ())
            .await?;
        Ok(resp.ns)
    }

    /// Updates the nameservers for a particular domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn update_nameservers(
        &self,
        domain: &str,
        name_servers: Vec<String>,
    ) -> Result<(), Error<T::Error>> {
        self.post_with_api_key(
            uri::update_name_servers(domain)?,
            UpdateNameServers { ns: name_servers },
        )
        .await
    }

    /// Get all the url forwards for a given domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn get_url_forwards(
        &self,
        domain: &str,
    ) -> Result<Vec<ForwardWithID>, Error<T::Error>> {
        let resp: GetUrlForwardingResponse = self
            .post_with_api_key(uri::get_url_forward(domain)?, ())
            .await?;
        Ok(resp.forwards)
    }

    /// Add a new url forward to the given domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn add_url_forward(&self, domain: &str, cmd: Forward) -> Result<(), Error<T::Error>> {
        self.post_with_api_key(uri::add_url_forward(domain)?, cmd)
            .await
    }

    /// Delete a url forward with the given id from the given domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn delete_url_forward(&self, domain: &str, id: &str) -> Result<(), Error<T::Error>> {
        self.post_with_api_key(uri::delete_url_forward(domain, id)?, ())
            .await
    }

    /// Get the SSL certificate bundle for a given domain
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
    pub async fn get_ssl_bundle(&mut self, domain: &str) -> Result<SslBundle, Error<T::Error>> {
        self.post_with_api_key(uri::get_ssl_bundle(domain)?, ())
            .await
    }
}