dna_rs/client.rs
1//! [`DnaClient`] struct and constructor methods.
2//!
3//! All API operation methods live in the `ops` sub-modules and are added
4//! to this type via separate `impl DnaClient` blocks.
5
6use crate::error::DnaResult;
7use crate::http::HttpClient;
8use crate::{URL_OTE, URL_PROD};
9
10/// Async REST client for the Domain Name API.
11///
12/// Create one instance per reseller session and reuse it across calls.
13///
14/// # Examples
15///
16/// ```rust,no_run
17/// use dna_rs::DnaClient;
18///
19/// # #[tokio::main]
20/// # async fn main() -> Result<(), dna_rs::DnaError> {
21/// // Production
22/// let client = DnaClient::new("RESELLER-UUID", "API-TOKEN")?;
23///
24/// // OTE / test environment
25/// let ote = DnaClient::new_ote("RESELLER-UUID", "API-TOKEN")?;
26/// # Ok(())
27/// # }
28/// ```
29pub struct DnaClient {
30 pub(crate) http: HttpClient,
31}
32
33impl DnaClient {
34 /// Connect to the **production** endpoint.
35 pub fn new(reseller_id: &str, token: &str) -> DnaResult<Self> {
36 Ok(Self {
37 http: HttpClient::new(URL_PROD, reseller_id, token)?,
38 })
39 }
40
41 /// Connect to the **OTE** (test/sandbox) endpoint.
42 pub fn new_ote(reseller_id: &str, token: &str) -> DnaResult<Self> {
43 Ok(Self {
44 http: HttpClient::new(URL_OTE, reseller_id, token)?,
45 })
46 }
47
48 /// Connect to a **custom** base URL (useful for mocking in tests).
49 pub fn with_url(reseller_id: &str, token: &str, url: &str) -> DnaResult<Self> {
50 Ok(Self {
51 http: HttpClient::new(url, reseller_id, token)?,
52 })
53 }
54}