X

Developing Delicious applications

I am developing a little piece of software that interacts with Delicious through HTTP API. Using Delicious API puts you one responsibility – use their service carefully. Don’t flood it and don’t stress it some other way. In this posting I will show you how to get bookmarks from Delicious and how to use their service without stressing it pointlessly.

Why Delicious? Because I keep my bookmarks there. If you are interested in my selection you can always check out what new bookmarks I have. I try to add new bookmarks as often as I find something useful.

Connecting to Delicious

I am using very simple custom class to query my bookmarks at Delicious. My class connects to Delicious using my username and password. As my class is still very yound it has some not so nice things – like hardcoded UrlPrefix. But it is okay for now.

public class DeliciousClient
{
    private readonly string _userName;
    private readonly string _password;

    private string UrlPrefix = "https://api.del.icio.us/v1/";

    public DeliciousClient(string userName, string password)
    {
        _userName = userName;
        _password = password;
    }

    public IList<Bookmark> GetBookmarks(DateTime fromDate, DateTime toDate)
    {
        var url = "posts/all?";
        url += "fromdt=";
        url += fromDate.Date.ToString("yyyy-MM-ddThh:mm:ssZ");
        url += "&todt=";
        url += toDate.Date.ToString("yyyy-MM-ddThh:mm:ssZ");

        string result = RequestData(url);

        // Parse XML and return list of bookmarks
    }

    private string RequestData(string uri)
    {
        using (var client = new WebClient())
        {
            var credential = new NetworkCredential(_userName, _password);

            client.Credentials = credential;
            var url = UrlPrefix + uri;
            var result = client.DownloadData(url);

            return Encoding.Default.GetString(result);
        }
    }
}

Using this code I am able to connect to Delicious and run my queries to get data. But if I connect to often then Delicious handles my requests as flooding and stops access for hours. With little efforts we are able to avoid it.

Caching queries

To avoid real connections to Delicious we can cache query results for us. Just perform there steps:

  1. Put break-point somewhere where you are able to get pure XML response (RequestData method is good candidate).
  2. Connect to Delicious through your code.
  3. When break-point is hit copy the XML result to clipboard.
  4. Stop your program.
  5. Create new XML-file called DeliciousResponse.xml to bin\Debug folder.
  6. Paste XML to this file and save it.

Now a little trick. We have Debug and Release configurations available out-of-box in Visual Studio. Let’s use them now. Debug configuration defines DEBUG constant for preprocessor. We can therefore write a code that is executed if DEBUG is there.

If DEBUG is defined we will read data from XML-file we created previously. The code is here for GetBookmarks method.

public IList<Bookmark> GetBookmarks(DateTime fromDate, DateTime toDate)
{
    var url = "posts/all?";
    url += "fromdt=";
    url += fromDate.Date.ToString("yyyy-MM-ddThh:mm:ssZ");
    url += "&todt=";
    url += toDate.Date.ToString("yyyy-MM-ddThh:mm:ssZ");

    string result;
#if DEBUG
    using (var reader = new StreamReader(File.OpenRead("DeliciousResponse.xml")))
    {
        result = reader.ReadToEnd();
    }
#else
    result = RequestData(url);
#endif

    // Parse XML and return list of bookmarks
}

Now you can run your code using Debug configuration and avoid requests to Delicious. If you need real data and communication with Delicious you can switch your project to Release configuration. I think it is very simple and powerful solution for now (of course, I will change my mind when requirements to my software change).

That’s it. If you write something cool for Delicious then let also community to know about it.

Liked this post? Empower your friends by sharing it!
Categories: .NET C#
Related Post