Thin Cloudflare v4 DNS API client for MoonBit built on gaato/mttp_core: zones, DNS records, DNSSEC and zone DNS settings.
moon add gaato/cloudflare_dns
moon add gaato/mttp///|
pub async fn count_records(
dns : @cloudflare_dns.CloudflareDns,
zone_name : String,
) -> Unit {
match dns.list_zones(name=zone_name) {
Ok([zone, ..]) =>
match
dns.list_dns_records(zone_id=zone.id, name_endswith="." + zone_name) {
Ok(records) => println(records.length())
Err(error) => println(@debug.to_string(error))
}
Ok(_) => println("Zone not found")
Err(error) => println(@debug.to_string(error))
}
}///|
fn main {
@mttp.run(async fn() {
guard @env.get_env_var("CLOUDFLARE_API_TOKEN") is Some(token) else {
println("Set CLOUDFLARE_API_TOKEN")
return
}
let backend = @mttp.backend()
defer backend.close()
let dns = @cloudflare_dns.CloudflareDns::new(
backend,
@mttp.clock(),
auth=ApiToken(token),
timer=@mttp.timer(),
total_timeout_ms=300_000,
)
count_records(dns, "example.com")
})
}///|
pub async fn add_records(
dns : @cloudflare_dns.CloudflareDns,
zone_id : String,
) -> Result[@gen.DnsBatchResult, @cloudflare_dns.Failure] {
let web = @gen.ARecord::new(
name="www.example.com",
content="192.0.2.1",
ttl=300,
)
let mail = @gen.MXRecord::new(
name="example.com",
content="mx.example.com",
priority=10,
)
let note = @gen.TXTRecord::new(
name="_verify.example.com",
content="\"verification\"",
)
match dns.create_dns_record(zone_id~, A(web)) {
Err(error) => Err(error)
Ok(_) =>
dns.batch_dns_records(
zone_id~,
@gen.DnsBatch::new(posts=[MX(mail), TXT(note)]),
)
}
}///|
pub async fn transfer_bind(
dns : @cloudflare_dns.CloudflareDns,
zone_id : String,
zone_file : String,
) -> Result[String, @cloudflare_dns.Failure] {
match dns.import_dns_records(zone_id~, zone_file~, proxied=false) {
Ok(_) => dns.export_dns_records(zone_id~)
Err(error) => Err(error)
}
}| Setting | Default with CloudflareDns::new | Notes |
|---|---|---|
| Retries | 2. GET, PUT, DELETE and HEAD on 408, 409, 5xx and network failures; every method, POST and PATCH included, on 429 and when the request was never sent | A 429 honours Retry-After however long, which can be minutes. |
| timeout_ms | 60 s per attempt | |
| total_timeout_ms | none | Set it, with timer=, to bound the waits. |
pub(all) enum Auth {
ApiToken(String)
GlobalApiKey(email~ : String, key~ : String)
}pub struct CloudflareDns {
// private fields
}impl Debug for CloudflareDnsasync fn CloudflareDns::batch_dns_records(self : CloudflareDns, zone_id~ : String, body : DnsBatch, include_shadow_metadata? : Bool) -> Result[DnsBatchResult, Failure]async fn[T] CloudflareDns::call(self : CloudflareDns, request : Request, decode : (ResponseHead, Bytes) -> Result[T, ResponseFailure], operation? : String) -> Result[T, Failure]async fn CloudflareDns::create_dns_record(self : CloudflareDns, zone_id~ : String, body : DnsRecord, include_shadow_metadata? : Bool) -> Result[DnsRecordResponse, Failure]async fn CloudflareDns::create_zone(self : CloudflareDns, body : ZoneCreate) -> Result[Zone, Failure]async fn CloudflareDns::delete_dns_record(self : CloudflareDns, dns_record_id~ : String, zone_id~ : String) -> Result[DnsRecordDeletion, Failure]async fn CloudflareDns::delete_dnssec(self : CloudflareDns, zone_id~ : String) -> Result[String, Failure]async fn CloudflareDns::delete_zone(self : CloudflareDns, zone_id~ : String) -> Result[ZoneDeletion?, Failure]async fn CloudflareDns::edit_dns_record(self : CloudflareDns, dns_record_id~ : String, zone_id~ : String, body : DnsRecordPatch, include_shadow_metadata? : Bool) -> Result[DnsRecordResponse, Failure]async fn CloudflareDns::edit_dns_settings(self : CloudflareDns, zone_id~ : String, body : ZoneDnsSettingsPatch) -> Result[ZoneDnsSettings, Failure]async fn CloudflareDns::edit_dnssec(self : CloudflareDns, zone_id~ : String, body : DnssecPatch) -> Result[Dnssec, Failure]async fn CloudflareDns::edit_zone(self : CloudflareDns, zone_id~ : String, body : ZonePatch) -> Result[Zone, Failure]async fn CloudflareDns::export_dns_records(self : CloudflareDns, zone_id~ : String) -> Result[String, Failure]fn CloudflareDns::from_client(client : Client, auth~ : Auth, base_url? : String, server_urls? : Map[String, String]) -> CloudflareDnsasync fn CloudflareDns::get_dns_record(self : CloudflareDns, dns_record_id~ : String, zone_id~ : String, include_shadow_metadata? : Bool) -> Result[DnsRecordResponse, Failure]async fn CloudflareDns::get_dns_settings(self : CloudflareDns, zone_id~ : String) -> Result[ZoneDnsSettings, Failure]async fn CloudflareDns::get_dnssec(self : CloudflareDns, zone_id~ : String) -> Result[Dnssec, Failure]async fn CloudflareDns::import_dns_records(self : CloudflareDns, zone_id~ : String, zone_file~ : String, proxied? : Bool) -> Result[DnsImportResult, Failure]async fn CloudflareDns::list_dns_record_scan(self : CloudflareDns, zone_id~ : String) -> Result[Array[DnsRecordResponse], Failure]async fn CloudflareDns::list_dns_records(self : CloudflareDns, zone_id~ : String, name? : String, name_exact? : String, name_contains? : String, name_startswith? : String, name_endswith? : String, type_? : DnsRecordType, content? : String, content_exact? : String, content_contains? : String, content_startswith? : String, content_endswith? : String, proxied? : Bool, match_? : DnsRecordMatch, comment? : String, comment_present? : String, comment_absent? : String, comment_exact? : String, comment_contains? : String, comment_startswith? : String, comment_endswith? : String, tag? : String, tag_present? : String, tag_absent? : String, tag_exact? : String, tag_contains? : String, tag_startswith? : String, tag_endswith? : String, search? : String, tag_match? : DnsRecordTagMatch, page? : Int, per_page? : Int, order? : DnsRecordOrder, direction? : DnsRecordDirection, include_shadow_metadata? : Bool, shadowed_by_name? : String, shadowing_name? : String) -> Result[Array[DnsRecordResponse], Failure]fn CloudflareDns::list_dns_records_pages(self : CloudflareDns, zone_id~ : String, name? : String, name_exact? : String, name_contains? : String, name_startswith? : String, name_endswith? : String, type_? : DnsRecordType, content? : String, content_exact? : String, content_contains? : String, content_startswith? : String, content_endswith? : String, proxied? : Bool, match_? : DnsRecordMatch, comment? : String, comment_present? : String, comment_absent? : String, comment_exact? : String, comment_contains? : String, comment_startswith? : String, comment_endswith? : String, tag? : String, tag_present? : String, tag_absent? : String, tag_exact? : String, tag_contains? : String, tag_startswith? : String, tag_endswith? : String, search? : String, tag_match? : DnsRecordTagMatch, page? : Int, per_page? : Int, order? : DnsRecordOrder, direction? : DnsRecordDirection, include_shadow_metadata? : Bool, shadowed_by_name? : String, shadowing_name? : String) -> Paginator[DnsRecordResponse, Failure]async fn CloudflareDns::list_zones(self : CloudflareDns, name? : String, status? : ZoneListStatus, type_? : Array[ZoneListType], account_id? : String, account_name? : String, page? : Int, per_page? : Int, order? : ZoneOrder, direction? : ZoneDirection, match_? : ZoneMatch) -> Result[Array[Zone], Failure]fn CloudflareDns::list_zones_pages(self : CloudflareDns, name? : String, status? : ZoneListStatus, type_? : Array[ZoneListType], account_id? : String, account_name? : String, page? : Int, per_page? : Int, order? : ZoneOrder, direction? : ZoneDirection, match_? : ZoneMatch) -> Paginator[Zone, Failure]fn CloudflareDns::new(backend : &Backend, clock : &Clock, auth~ : Auth, base_url? : String, server_urls? : Map[String, String], retry? : RetryPolicy, timer? : &Timer, timeout_ms? : Int, total_timeout_ms? : Int, max_body_bytes? : Int) -> CloudflareDnsfn[T : FromJson] CloudflareDns::paginate(self : CloudflareDns, first : Request, kind~ : PageKind, items~ : String, next~ : String, operation? : String) -> Paginator[T, Failure]async fn CloudflareDns::review_dns_record_scan(self : CloudflareDns, zone_id~ : String, body : DnsScanReview) -> Result[DnsScanReviewResult, Failure]async fn CloudflareDns::send(self : CloudflareDns, request : Request, operation? : String) -> Result[Reply[Bytes], Failure]async fn CloudflareDns::trigger_dns_record_scan(self : CloudflareDns, zone_id~ : String) -> Result[Unit, Failure]async fn CloudflareDns::trigger_zone_activation_check(self : CloudflareDns, zone_id~ : String) -> Result[ZoneActivation, Failure]async fn CloudflareDns::update_dns_record(self : CloudflareDns, dns_record_id~ : String, zone_id~ : String, body : DnsRecord, include_shadow_metadata? : Bool) -> Result[DnsRecordResponse, Failure]pub(all) struct EnvelopeMessage {
code : Int
message : String
documentation_url : String?
source_pointer : String?
} derive(Debug)pub(all) enum Failure {
Http(HttpFailure)
Transport(TransportError)
Decode(ResponseFailure)
Validation(operation~ : String, parameter~ : String, message~ : String)
} derive(Debug)pub(all) struct HttpFailure {
operation : String
head : ResponseHead
body : Bytes
errors : Array[EnvelopeMessage]
messages : Array[EnvelopeMessage]
} derive(Debug)pub(all) enum PageKind {
Cursor(String)
NextUrl
Page(String)
}Install
Download zipThin Cloudflare v4 DNS API client for MoonBit built on gaato/mttp_core: zones, DNS records, DNSSEC and zone DNS settings.