Publishing to a WordPress Blog via XML-RPC (Part 8)
Please refer to this post for some background.
Now that all the pieces are in place, a class called WordPressClient brings it all together. It has methods that correspond with some of the tasks you might try to accomplish (such as creating a new post, deleting a post, adding a category, etc.).
using System; using System.Collections.Generic; using System.Text; namespace Piccolo.Common { public class WordPressClient { private XmlRpcClient _xmlRpcClient; private string _userName; private string _password; private const string _appKey = "0123456789ABCDEF"; //Default required (not sure why!) private string _blogID = null; public WordPressClient(string xmlRpcUrl, string userName, string password) { _xmlRpcClient = new XmlRpcClient(xmlRpcUrl); _userName = userName; _password = password; } public string AddCategory(string category) { KeyValuePairs categoryCollection = new KeyValuePairs(); categoryCollection.Add("name", category); categoryCollection.Add("description", category); return WordPressXmlParser.ParseSingleStringValue(_xmlRpcClient.InvokeXmlRpcFunction("wp.newCategory", GetBlogID(), _userName, _password, WordPressXmlBuilder.BuildStruct(categoryCollection))); } public string CreatePost(WordPressBlogPost post) { string postID = WordPressXmlParser.ParseSingleStringValue(_xmlRpcClient.InvokeXmlRpcFunction("metaWeblog.newPost", GetBlogID(), _userName, _password, WordPressXmlBuilder.BuildStruct(post), "1")); List<KeyValuePairs> categoryPairList = new List<KeyValuePairs>(); foreach (string category in post.CategoryNames) { KeyValuePairs pairs = new KeyValuePairs(); pairs.Add("categoryId", AddCategory(category)); categoryPairList.Add(pairs); } if (categoryPairList.Count > 0) _xmlRpcClient.InvokeXmlRpcFunction("mt.setPostCategories", postID, _userName, _password, WordPressXmlBuilder.BuildStructArray(categoryPairList.ToArray())); else _xmlRpcClient.InvokeXmlRpcFunction("mt.setPostCategories", postID, _userName, _password); return postID; } public bool DeleteCategory(string categoryID) { return WordPressXmlParser.ParseSingleBoolValue(_xmlRpcClient.InvokeXmlRpcFunction("wp.deleteCategory", GetBlogID(), _userName, _password, categoryID)); } public bool DeletePost(int postID) { return WordPressXmlParser.ParseSingleBoolValue(_xmlRpcClient.InvokeXmlRpcFunction("metaWeblog.deletePost", _appKey, postID.ToString(), _userName, _password, "1")); } public bool EditPost(int postID, WordPressBlogPost post) { return WordPressXmlParser.ParseSingleBoolValue(_xmlRpcClient.InvokeXmlRpcFunction("metaWeblog.editPost", postID.ToString(), _userName, _password, WordPressXmlBuilder.BuildStruct(post), "1")); } public string GetBlogID() { if (_blogID == null) { KeyValuePairs values = WordPressXmlParser.ParseStructValues(_xmlRpcClient.InvokeXmlRpcFunction("metaWeblog.getUsersBlogs", _appKey, _userName, _password)); _blogID = values.GetFirstValue("blogid"); } return _blogID; } public string[] GetCategories() { KeyValuePairs pairs = WordPressXmlParser.ParseStructValues(_xmlRpcClient.InvokeXmlRpcFunction("wp.getCategories", GetBlogID(), _userName, _password)); return pairs.GetValuesMatchingKey("categoryName"); } public WordPressBlogPost GetPost(int postID) { string result = _xmlRpcClient.InvokeXmlRpcFunction("metaWeblog.getPost", postID.ToString(), _userName, _password); KeyValuePairs structVals = WordPressXmlParser.ParseStructValues(result); string[] categoryNames = GetPostCategoryNames(postID); return new WordPressBlogPost(structVals.GetFirstValue("title"), structVals.GetFirstValue("description"), categoryNames); } public KeyValuePairs GetPostCategories(int postID) { string result = _xmlRpcClient.InvokeXmlRpcFunction("mt.getPostCategories", postID.ToString(), _userName, _password); return WordPressXmlParser.ParseStructValues(result); } public string[] GetPostCategoryIDs(int postID) { return GetPostCategories(postID).GetValuesMatchingKey("categoryId"); } private string[] GetPostCategoryNames(int postID) { return GetPostCategories(postID).GetValuesMatchingKey("categoryName"); } public string[] GetSupportedMethods() { return WordPressXmlParser.ParseArray(_xmlRpcClient.InvokeXmlRpcFunction("mt.supportedMethods")); } public bool IsConnected() { try { return WordPressXmlParser.ParseSingleStringValue(_xmlRpcClient.InvokeXmlRpcFunction("demo.sayHello")) == "Hello!"; } catch { return false; } } } }
And below are some nUnit tests that illustrate how you might use this class.
using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; namespace Piccolo.Common.Tests { [TestFixture] public class WordPressClientTests { private WordPressClient _client = new WordPressClient("http://blog.address.com/xmlrpc.php", "user", "password"); [Test] public void GetSupportedMethods() { string[] expectedMethods = new string[] { "wp.getUsersBlogs", "wp.getPage", "wp.getPages", "wp.newPage","wp.deletePage","wp.editPage","wp.getPageList","wp.getAuthors", "wp.getCategories","wp.newCategory","wp.deleteCategory","wp.suggestCategories", "wp.uploadFile","wp.getCommentCount","wp.getPostStatusList","wp.getPageStatusList", "wp.getPageTemplates","wp.getOptions","wp.setOptions","blogger.getUsersBlogs", "blogger.getUserInfo","blogger.getPost","blogger.getRecentPosts","blogger.getTemplate", "blogger.setTemplate","blogger.newPost","blogger.editPost","blogger.deletePost", "metaWeblog.newPost","metaWeblog.editPost","metaWeblog.getPost","metaWeblog.getRecentPosts", "metaWeblog.getCategories","metaWeblog.newMediaObject","metaWeblog.deletePost", "metaWeblog.getTemplate","metaWeblog.setTemplate","metaWeblog.getUsersBlogs", "mt.getCategoryList","mt.getRecentPostTitles","mt.getPostCategories","mt.setPostCategories", "mt.supportedMethods","mt.supportedTextFilters","mt.getTrackbackPings","mt.publishPost", "pingback.ping","pingback.extensions.getPingbacks","demo.sayHello","demo.addTwoNumbers"}; string[] methods = _client.GetSupportedMethods(); Assert.AreEqual(expectedMethods.Length, methods.Length); for (int i = 0; i -1); WordPressBlogPost savedPost = _client.GetPost(postID); Assert.AreEqual(testPost, savedPost); Assert.AreEqual(1, savedPost.CategoryNames.Length); Assert.AreEqual("testcategory1", savedPost.CategoryNames[0]); savedPost.Title += "2"; savedPost.Text += "2"; Assert.IsTrue(_client.EditPost(postID, savedPost)); WordPressBlogPost editedPost = _client.GetPost(postID); Assert.AreEqual(savedPost, editedPost); string[] categoryIDs = _client.GetPostCategoryIDs(postID); Assert.AreEqual(1, categoryIDs.Length); Assert.IsTrue(_client.DeletePost(postID)); Assert.IsTrue(_client.DeleteCategory(categoryIDs[0])); } [Test] public void AddCategory() { string categoryID = _client.AddCategory("testcategory1"); Assert.IsTrue(int.Parse(categoryID) > 1); Assert.Contains("testcategory1", _client.GetCategories()); Assert.IsTrue(_client.DeleteCategory(categoryID)); } [Test] public void IsConnected() { Assert.IsTrue(_client.IsConnected()); } } }