zimki | all bloggers | admin login | recent entries for Tom Insam
 
Implementing MetaWeblog in Zimki

Tom Insam on Mon Feb 26 2007 13:37:33 GMT+0000 (BST)

Last week I demonstrated writing an XMLRPC server in Zimki. Next I needed an implementation of the MetaWeblog API on top of it, so I could edit blog posts in nice GUI clients.

http://blog.realm.zimki.com/pics/metaweblog.png

If you're implementing this for a complicated blog with excerpts and keywords then there are all sorts of annoyances you'll run into. Fortunately, I got to ignore almost all of this. The Zimki blog is a very simple app at the moment, as it's also intended as a demo and learning exercise for Zimki (you can download its source code and use it as the basis for your own blog). I wanted my MetaWeblog implementation to serve more as a nice example than as a fully-featured codebase.

I've only had to implement six functions (you can see the implementation here, or you can upload the source code into your own Zimki account and look at it there). The first half of the code is two simple utility functions, the second half is the implementation of the server - in total, a mere 110 lines of code for the whole thing.

First the utility functions - 'post_struct' just takes an instance of the 'Post' class and returns the structure that the MetaWeblog API expects to see. 'authenticated' takes an anonymous function, and wraps it in another function that extracts the required 'username' and 'password' parameters, authenticates the call against the built-in Zimki user database, then passes control to the wrapped function. Because of this, none of the methods below need to worry about logging the user in or setting the ownership of created object, they can just use the zimki.session object like all the other blog code.

The second half of the file is the actual interface. This is where it's really nice to have such a high-level persistency framework and language - every API method we implement here is a mere 2 or 3 JavaScript statements. The getRecentPosts call in particular illustrates why I love JavaScript:

    var posts = Post.search(
        { author:zimki.session.user },
        { order_by:'-creation_date', per_page:count }
    );
    return posts.map( post_struct );

"Get a list of 'count' posts owned by the current user, in date order, then return the result of 'post_struct' called on each of them in turn".

With this code in the blog app, I can point my GUI editor at https://blog.zimki.com/xmlrpc, and edit blog posts locally, uploading them when I'm done. I think it took me about two hours to get an initial working implementation (although I have done it before) and another couple to polish it off, which really isn't bad.

leave a comment

name
email
comment