use serde::Deserialize;
use serde::Serialize;
use dotenv::dotenv;
pub mod utils;
pub mod domains;
pub mod response;
pub mod domains_dns;
pub const NAMECHEAP_API_URL: &str = "https://api.namecheap.com";
pub const NAMECHEAP_SANDBOX_API_URL: &str = "https://api.sandbox.namecheap.com";
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[derive(PartialEq, Eq, Hash)]
pub struct NameCheapClient {
pub api_user: String,
pub api_key: String,
pub client_ip: String,
pub user_name: String,
pub production: bool,
pub api_url: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[derive(PartialEq, Eq, Hash)]
pub struct Domain {
pub id: i64,
pub name: String,
pub user: String,
pub created: String,
pub expires: String,
pub is_expired: bool,
pub is_locked: bool,
pub auto_renew: bool,
pub whois_guard: bool,
pub is_premium: bool,
pub is_our_dns: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[derive(PartialEq, Eq, Hash)]
pub struct Contact {
pub type_: String,
pub address_1: String,
pub address_2: String,
pub city: String,
pub country: String,
pub email_address: String,
pub fax: String,
pub first_name: String,
pub job_title: String,
pub last_name: String,
pub organization_name: String,
pub phone: String,
pub phone_ext: String,
pub postal_code: String,
pub state_province: String,
pub state_province_choice: String,
pub read_only: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[derive(PartialEq, Eq, Hash)]
pub struct Host {
pub host_id: String,
pub name: String,
pub address: String,
#[serde(rename = "type")]
pub type_: String,
#[serde(rename = "is_active")]
pub is_active: bool,
#[serde(rename = "ttl")]
pub ttl: i64,
#[serde(rename = "mxpref")]
pub mx_pref: String,
#[serde(rename = "is_ddnsenabled")]
pub is_ddns_enabled: bool,
#[serde(rename = "friendly_name")]
pub friendly_name: String,
#[serde(rename = "associated_app_title")]
pub associated_app_title: String,
}
impl Host {
pub fn new() -> Self {
Host {
host_id: String::new(),
name: String::new(),
address: String::new(),
type_: String::new(),
is_active: false,
ttl: 0,
mx_pref: String::new(),
is_ddns_enabled: false,
friendly_name: String::new(),
associated_app_title: String::new(),
}
}
}
impl NameCheapClient {
pub fn new(
api_user: String,
api_key: String,
client_ip: String,
user_name: String,
production: bool
) -> Self {
NameCheapClient {
api_user,
api_key,
client_ip,
user_name,
production,
api_url: if production {
Some(NAMECHEAP_API_URL.to_string())
} else {
Some(NAMECHEAP_SANDBOX_API_URL.to_string())
},
}
}
pub fn new_from_env() -> Result<Self, Box<dyn std::error::Error>> {
dotenv().ok();
use std::env::var;
let user_name = var("NAMECHEAP_USER_NAME")?;
let api_key = var("NAMECHEAP_API_KEY")?;
let client_ip = var("NAMECHEAP_CLIENT_IP")?;
let production = var("NAMECHEAP_PRODUCTION")
.unwrap_or_else(|_| "false".to_string())
.parse()
.unwrap_or(false);
Ok(Self::new(user_name.clone(), api_key, client_ip, user_name, production))
}
}