Development DNS

A simple way to consume the Atomia DNS web service using .Net

Last Friday, we opened the doors to AtomiaDNS.net, a playground for those who want to test Atomia DNS without installing it. It is a free, fully functional DNS service without any guarantees in terms of up-time or availability.

Atomia DNS is written in Perl and the control panel (Atomia DNS Web App) is written in Node.js, you can, however, use the programming language of your choice to talk to Atomia DNS.

This weekend I was playing around in Visual Studio and wrote a small application to manage my zones. I’d like to share a few tips on how to get started in C# and .Net.

In this blog post I will focus on the simple GetZone method, but consuming more complex methods is not harder than creating the suitable Json object and pass it to the API. For this purpose I found that James Newton-King’s Json.NET library was very helpful and I will give an example of using it.

Before we get started

First you will need an AtomiaDNS.net account, it can be created at http://atomiadns.com/demo, just enter name and e-mail and a password will be sent to you within a minute.

You will probably also want to add domain name to manage. Before adding your zone, you must first log in to the control panel of your domain name provider and set the name servers to ns1.atominadns.net and ns2.atomiadns.net. Once you are done with this, you can add it using either the AtomiaDNS.net control panel or do it using CURL from your command line. Or you can add it with the application you are about to write 😉

The code

To connect to AtomiaDNS.net API I simply used the WebClient object. Make sure to set your username and password in the header:
[sourcecode language=”csharp”]
using (var client = new WebClient())
{
client.Headers["X-Auth-Username"] = username;
client.Headers["X-Auth-Password"] = password;

[/sourcecode]
To execute an command I used the UploadString() method with the URL of the method and my Json object as a string:
[sourcecode language=”csharp”]
var json = @"[""my-domain.com""]";
var response = client.UploadString("http://api.atomiadns.net/atomiadns.json/GetZone", json);
[/sourcecode]
The response from this method will look something like this:
[sourcecode language=”javascript”]
[
{
"records" : [
{
"ttl" : "3600",
"label" : "@",
"class" : "IN",
"id" : "9",
"type" : "A",
"rdata" : "192.168.0.1"
},
{
"ttl" : "3600",
"label" : "@",
"class" : "IN",
"id" : "8",
"type" : "NS",
"rdata" : "ns1.atomiadns.net."
}
]
}
]
[/sourcecode]

Since I was working in .Net for this specific project I wanted to deserialize the Json results into objects. For this purpose I used Json.Net. The installation was dead simple. I downloaded the files from http://json.codeplex.com/releases/view/82120, dropped them in my bin folder and added a reference to them.

Then I declared the classes I needed, in this case for the Zone object:
[sourcecode language=”csharp”]
public class Zone
{
public string name;
public List<RecordList> recordList { set; get; }
}

// Each sub domain’s DNS records will go in a separate record list
public class RecordList
{
public List<Record> records { set; get; }
}

public class Record
{
public string ttl { set; get; }
public string label { set; get; }
// I had to use upper-case ‘C’ for project to build 😉
public string Class { set; get; }
public string id { set; get; }
public string type { set; get; }
public string rdata { set; get; }
}
[/sourcecode]

Now, to get the response from Atomia DNS neatly packaged into objects I just wrote the following:
[sourcecode language=”csharp”]
zone.recordList = JsonConvert.DeserializeObject<List<RecordList>>(response);
[/sourcecode]

The “full” code:
[sourcecode language=”csharp”]
var response = string.Empty;
Zone zone = new Zone();
zone.name = domainName;

using (var client = new WebClient())
{
// Set username and password
client.Headers["X-Auth-Username"] = username;
client.Headers["X-Auth-Password"] = password;
try
{
// Create Json object to send to web service
var json = String.Format(@"[""{0}""]", domainName);
response = client.UploadString("http://api.atomiadns.net/pretty/atomiadns.json/GetZone", json));

}
catch(Exception e){ //Log error }
}
// Deserialize response
zone.recordList = JsonConvert.DeserializeObject<List<RecordList>>(response);
[/sourcecode]

And there you have it! All your zone data neatly packaged into an object ready to be played with in .Net.

Good luck! Join our Google group for the latest news, tips and discussions.

Social

Follow us on Twitter

Like us on Facebook

Visit us on LinkedIn

We share the latest news about Atomia, event photos, and more.

Contact

[email protected]

+46 21 490 2620

Hamngränd 6,
721 30 Västerås,
Sweden

Work at Atomia

Would you like to join our quest to provide the ideal hosting platform? Be part of a fun, dedicated team and work with some of the coolest companies in the hosting industry. Check out our job page.