📜 ⬆️ ⬇️

MetaWeblog API in .NET

MetaWeblog , I tell you, is a rather amusing thing. Simple and clear aki doors. When you understand everything there.

About protocols


I first encountered this interface while screwing an API to my wiki engine to edit these same wiki pages and blog posts. The Blogger API didn’t work due to the fact that the guys from Google, engaged in developing the GData API based on the Atom Publishing Protocol , turned away from it, and the APP itself was not yet standardized. There was no intelligible documentation on the Movable Type API, and there was somehow no others.

About Russian programmers


Anyway, I stopped at the MetaWeblog API. Since it is built on top of XML-RPC , a need arose for a library that would take all these low-level details on itself. That was found: XML-RPC.NET (by Charles Cook ). A good, in general, library, but not without drawbacks. Firstly, it is too heavy - there you will have both code generation, Remoting support, and the ability to create your own stand-alone XML-RPC server, and much more. Thirdly, with the documentation there is not so hot. And, finally, when serializing structures, only publicly accessible fields were taken into account, which may not be so bad, but it did not suit me.

In short, it was decided to write his own library (oh yeah, “ they didn't write it ” is also an important factor). The result is available on Google Code .
')

octalforty Brushie Web


And so he called, yes.

The most important class is XmlRpcService ; abstract and implementing the widely known System.Web.IHttpHandler . To create your own XML-RPC service, it is enough to inherit from this class and mark the methods XmlRpcServiceMethodAttribute attribute:
public class MathXmlRpcService : octalforty.Brushie.Web.XmlRpc.XmlRpcService
{
[XmlRpcServiceMethod("add")]
public int Add(int x, int y)
{
return x + y;
}
}

For primitive types (which int is) this is enough. If you need to transfer structures (the word “structure” I use in relation to XML-RPC, and not to .NET), then the class (here we are already talking about .NET) must be further marked:
[XmlRpcStructure()]
public class BlogPostCategory
{
private string title = String.Empty;

[XmlRpcMember("title")]
public string Title
{
get { return title; }
set { title = value; }
}
}

But in general, that's all. There is nothing more to know about XML-RPC.

MetaWeblog API


Now is the turn of the implementation of the API itself. Having in the hands of octalforty.Brushie.Web.XmlRpc.XmlRpcService and all the related infrastructure, it is up to you to understand the rather vague specification and write the appropriate code. Taking Fiddler in one hand and Windows Live Writer in the other, I began research. As a result, came out such a stone flower .

Now the implementation of the API has become a matter of obscenely simple: inherit the class of service from the XmlRpcService and implement IMetaWeblogService .

Curtain ...


... an example of how the NewMediaObject implementation looks like.
namespace octalforty.Kudos.Web.Api.Wiki
{
/// /// A MetaWeblog XML-RPC service for editing wiki pages.
///
public class MetaWeblogService : XmlRpcService, IMetaWeblogService, IPageManagerServiceDependency,
ISpaceManagerServiceDependency, INavigationServiceDependency,
IGlobalTagManagerServiceDependency, ISecurityServiceDependency,
IUserManagerServiceDependency, IPageAttachmentManagerServiceDependency
{
public MediaObjectInfo NewMediaObject(string blogID, string login, string password, MediaObject mediaObject)
{
if(!SecurityService.IsValid(login, password))
throw new SecurityException();

//
// Parsing media object name.
if(mediaObject.Name.IndexOf("^") < 0)
throw new ArgumentException();

string pageName = mediaObject.Name.Substring(0, mediaObject.Name.IndexOf("^"));
string attachmentName = mediaObject.Name.Substring(mediaObject.Name.IndexOf("^") + 1);

Space space = SpaceManagerService.GetSpaceByID(Convert.ToInt64(blogID));
Page page = PageManagerService.GetPageBySpaceKeyAndPageName(space.Key, pageName);

PageAttachment pageAttachment = PageAttachmentManagerService.GetPageAttachmentByName(page, attachmentName);
if(pageAttachment == null)
{
pageAttachment = new PageAttachment(attachmentName, mediaObject.MimeType);
page.AddAttachment(pageAttachment);
} // if

pageAttachment.AddRevision(new PageAttachmentRevision(UserManagerService.GetUserByLogin(login), mediaObject.Content));

PageManagerService.SavePage(page);

return new MediaObjectInfo(NavigationService.ResolveVirtualUrl(NavigationService.GetPageAttachmentUrl(pageAttachment)));
}
}
}

Source: https://habr.com/ru/post/24792/


All Articles