|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Contents
IntroductionI was looking for a Blogger.com client for my Pocket PC, but unfortunately, I was not satisfied with any of the applications that I had found. So, I decided to develop one myself. I started by developing an independent .NET API for interacting with Blogger.com. This is presented here. This component, which I have named NBlog, allows any .NET application to simply perform different operations against Blogger.com without any knowledge of the underlying communication protocols. NBlog uses the new Blogger Atom API as the main communication protocol. However, being designed with extensibility in mind, it allows for implementations of any other Blogger-supported protocol to be added. These new implementations can then be plugged into the existing applications by using a simple configuration setting. To show how NBlog can be used within an application, I've also developed a simple Windows Forms Blogger client. The image at the top is a screenshot from this application. Overview of the AtomAPIAs mentioned above, NBlog uses the Blogger Atom API as the main protocol for communication with the Blogger. The Blogger Atom API is based on the AtomAPI, which is an application level protocol for publishing and editing web resources. The protocol at its core is the HTTP transport of XML payloads. In this protocol different operations are specified with different HTTP methods (GET, PUT, POST, ...). The input data for each operation is provided as Atom formatted XML in the HTTP request body, and the output data is returned as Atom formatted XML data in the HTTP response body. For example, the following AtomAPI request can be used for receiving the list of a user's blogs from the Blogger: GET /atom HTTP/1.1
Host: www.blogger.com
Authorization: BASIC b9Oj4vdRldmVqGFzc5dvcmQyZVFdcGF=
which will return: <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xmlns="http://purl.org/atom/ns#">
<userid xmlns="http://www.blogger.com/atom/ns#">2114778</userid>
<link href="https://www.blogger.com/atom/29497767" rel="service.post"
title="A Sample Blog" type="application/atom+xml"/>
<link href="https://www.blogger.com/atom/29497767" rel="service.feed"
title="A Sample Blog" type="application/atom+xml"/>
...
</feed>
To know more about the Blogger Atom API and the AtomAPI visit the following sites: NBlog's core designIn the design of NBlog, I mainly focused on achieving the following:
The advantage of having a protocol independent interface is that the applications using it can be seamlessly configured to use different implementations of any other Blogger-supported technology. The following diagram shows the main classes and interfaces that build up NBlog's core:
The "IBloggerProvider" interfaceThe The following code shows the definition of this interface: public interface IBloggerProvider
{
ICredentials Credentials {get;set;}
IWebProxy Proxy {get;set;}
UserProfile GetUserProfile();
void UpdateUserProfile(UserProfile userProfile);
BlogCollection GetUserBlogs();
Blog GetBlog(string blogUri);
void UpdateBlog(Blog blog);
Post NewPost();
PostCollection GetBlogPosts(string blogUri);
Post GetPost(string postUri);
void CreatePost(string blogUri, Post post);
void UpdatePost(Post post);
void DeletePost(string postUri);
Comment NewComment();
CommentCollection GetPostComments(string postUri);
void CreateComment(string postUri, Comment comment);
void UpdateComment(Comment comment);
void DeleteComment(string commentUri);
}
The "ProviderFactory" classThe The following code shows the public class ProviderFactory
{
public static IBloggerProvider GetProvider()
{
// Load provider from config. or use the default provider
IBloggerProvider provider = LoadProvider();
if (provider == null)
provider = new NBlog.Atom.AtomProvider();
// Setup and return provider
provider.Credentials = LoadCredentials();
provider.Proxy = LoadProxy();
return provider;
}
...
}
The The following configuration settings can be specified in the App.config or Web.config files: <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="providerSettings"
value="assembly file name, fully-qualified type-name" />
<add key="credentials"
value="blogger username, blogger password" />
<add key="proxySettings"
value="address(ip), port, username, password" />
</appSettings>
</configuration>
Entity classesThese classes represent the entities involved in the Blogger. The There are also some corresponding collection classes for some of the entities, which facilitate the task of handling groups of entities. These collection classes are defined by extending .NET Framework's
The AtomAPI implementationThe current version of NBlog provides an AtomAPI-based implementation of the
The "AtomProvider" classThe As noted earlier, the AtomAPI uses HTTP as its transport protocol, and XML as its data format. As a result, implementing the protocol involves handling HTTP requests and responses, as well as converting data to and from XML. The The "IAtomEntity" interfaceAs mentioned above, the core NBlog entities are defined as abstract. Therefore, different implementations must extend these entities, and optionally add some protocol specific logic to them. In the AtomAPI implementation, the extended versions of the entities implement the public interface IAtomEntity
{
string GetXml();
void SetXml(XmlNode xmlNode);
}
As you can see, The "AtomConverter" classThe Using the codeAs mentioned in the introduction, there is a sample Blogger client provided as the "WinClient" project in the solution. Although this sample can be a good example of using NBlog in a real application, I've provided a simpler and a more readable sample code below to show a few steps in using the component: using System;
using System.Collections;
using System.Net;
namespace NBlog.Test
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
IBloggerProvider provider = ProviderFactory.GetProvider();
provider.Credentials = new NetworkCredential("user", "pass");
BlogCollection blogs = provider.GetUserBlogs();
foreach (Blog b in blogs)
{
PostCollection posts = provider.GetBlogPosts(b.Uri);
Console.WriteLine("{0}: {1} post(s)\n",
b.Title, posts.Count);
foreach (Post p in posts)
{
Console.WriteLine("\t{0}
The code simply dumps the recent posts from all blogs of the specified user. The first step is to get an instance of the IBloggerProvider provider = ProviderFactory.GetProvider();
The Next, the provider.Credentials = new NetworkCredential("user", "pass");
The following code will receive a list of the user's blogs: BlogCollection blogs = provider.GetUserBlogs();
A foreach (Blog b in blogs)
{
...
}
In the body of the PostCollection posts = provider.GetBlogPosts(b.Uri);
Finally, the following code will dump each blog's content to the console: Console.WriteLine("{0}: {1} post(s)\n", b.Title, posts.Count);
foreach (Post p in posts)
{
Console.WriteLine("\t{0}
There's one more important task which is not covered in the previous sample, and that's creating a new post. The following code shows the proper way of creating a new post: Post newPost = provider.NewPost();
newPost.Title = "Sample Post";
newPost.Content = "...";
provider.CreatePost(blogUri, newPost);
SummaryAs mentioned earlier, the main motivation for developing NBlog was to create a Blogger API that can be used in a Pocket PC Blogger client. But I believe, NBlog can be quite useful as a standalone component, and it's probably worth presenting here. Hopefully, I'll be able to develop the Pocket PC client very soon, and post it here as well.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||