Hi, I'm Harlin and welcome to my blog. I write about Python, Alfresco and other cheesy comestibles.

Python + Alfresco CMIS Rest API

I mentioned about a week and a half or so ago that I joined Formtek as a consultant and will continue to be working with Alfresco the software. If you've read this blog, you know that I also enjoy working with Python -- obviously right? For the moment at Formtek though, I am involved in learning the ropes, getting to know the customers and assisting with some upgrades. So, I haven't had much time to spend on Python but I did want to let you know about a project I am working on the side.

It's called Alfresco API Python (or alfresco-api-python). I wrote a post a few months back about CMIS (cmislib to be exact). For now, it appears that Alfresco is content to release their CMIS Rest API and are trying more and more to create a solid public api.

You can see the API here. It is very well put together and it is very simple to work with. With Kenneth Reitz's fine Python module, python-requests, it is a snap to write Python code that interacts with it. The CMIS Rest API is fine as is and all but I wanted to write some kind of Python wrapper to marry up the response content in json to Python objects.

Of course, how I do this over time will probably change a bit but for now, I envision using something like a 3-layer abstraction. The first layer will include the most basic http client requests using the requests module. In this layer, I am writing the request parts like:

  • GET (a get one and a get all)
  • POST
  • PUT
  • DELETE

These are fairly basic and will work against very simple urls like:

/people
/sites
/nodes

etc.

Since there are some other rest api urls that are a little more complex like:

/people/{peopleId}/avatar
/sites/{siteId}/containers/{containerId}

etc.

I also have some other requests that will handle unique object and object id urls concatenations. Each of these requests will be their own classes which can be subclassed by Alfresco requests.

In the foundational layer also, the core client parameters are set like: Alfresco host, username, password and the base url:

URI = 'alfresco/api/-default-/public/alfresco/versions/1'

The next layer up is in the alfresco_requests.py module. There is something of a shell class for each of the objects like:

People
Sites
Activities
Groups
Nodes

etc.

These will each inherit the http request type classes mentioned in the first layer. So, some of these won't be able to do DELETE or PUT (it doesn't make sense for some objects) but these are very easy to put together. I can simply do something like this:

class PeopleRequest(
    ModelRequestLIST, ModelRequestGET, ModelRequestPUT, ModelRequestPOST
):
    """ Users in an Alfresco repository."""
    url_path = 'people'

The PeopleRequest as it is can handle GET, PUT and POST methods. There is also a special method called LIST but is underneath the covers a GET that pulls in all results for an object.

While this object request can handle a number of methods, some other Alfresco requests may only have ModelRequestGET. This makes the object requests very simple to construct as the derived request classes can be modified on the fly. This makes the development process fairly simple.

I have all these Alfresco requests that I'm going to use and am now working on writing unit tests. This has been a little laborious and tedious but I think it's very necessary so that when I begin writing the next layer, things will go a lot more smoothly.

I think my reason for writing this was to log my activity on this project as I go and describe what I'm thinking as I create this library. My intention is to be able to use this in a web app like Django or maybe in a cli app. I also want to describe the steps involved as I go. A new programmer may want to tackle a project like this from a monolithic design but I feel like it will be better to layer the architecture and give this project the ability to allow anyone to write code on top of the library itself.

If you think about it, this is what the developers at Alfresco have done. That is, they have created a solid document repository as a black box of sorts. Sure, Alfresco is open source and anyone can make changes to the repository code if they want to. But, if you're writing derived applications on top of the repository, there should be no reason really to go back and muck around with the core source code. Alfresco's repo is very solid and mature. So, the recommendation now is to write layers of functionality on top and leave the core to Alfresco's developers. Like that, I am hoping to offer my library in the future for anyone who has an interest to do the same with Alfresco.

Going forward, I do plan to write a few more articles on this project. Hopefully it will give some insight into how this sort of thing is done but I also want to welcome any feedback either as comments on this blog or at github where this project is being hosted. I figure I can learn something from you if you are interested in helping in some way.

Well, the holidays are only a few days away. I plan to do a little bit of work on this but not too much. Hope you enjoy your holidays with your family. Talk to you soon.

Any Comments, Always Welcome!