Ugly Stool Rotating Header Image

WCF REST Client for SmugMug – 1 of n

I created an open source project called Obra.  Obra is a PowerShell drive for SmugMug, which basically means you can treat SmugMug as if it was your C:.  You can execute directory listing, rename files, delete files, and look at file metadata.  In the case of SmugMug metadata means the album’s description, image dimensions, EXIF, etc.

I wrote Obra to learn about technologies I have not really used like WCF and PowerShell.  I had previously written a read-only ruby client for SmugMug, but it stalled when I started to implement write-support.  I did not want to lose people’s pictures, and I couldn’t figure out a good way to test my client without re-implementing the SmugMug REST service.  (This was me, not ruby.)  Fast-forward a couple of years, and I was now employed as a C# developer and learning about Nunit and NMock.  I wanted to incorporate these tools into the project as well.

When I started Obra I could not find much material about writing WCF REST clients.  Excluding REST, most of the material seemed biased towards WCF servers, not clients.  Hopefully, this series of posts rectifies that.

The SmugMug API supports REST, JSON, serialized PHP, and provides an XML-RPC service.  In SmugMug’s case REST really means POX, which is the interface Obra uses.  (I assume SmugMug did not re-invent the wheel when they designed their API, and rather based it on the Flickr API; they are very similar.) 

Client requests are HTTP GET’s (with the exception of uploading, which is a POST).  A request encodes the method and parameters in the URI.  For example, the following URI calls a method to get the albums for the session with ID 1ac.

http://api.smugmug.com/hack/rest/1.2.0/?method=smugmug.albums.get&SessionID=1ac

Responses all follow the same basic form.  The root element is <rsp/>, the first child node is <method/> and contains the name of the method called, and the second child node is the response element.  In the case of the method smugmug.albums.get the node is <Albums/>.

<rsp stat="ok">
   <method>smugmug.albums.get</method>
   <albums>
      <album id="0" title="2007-01-01" key="smgms">
         <category id="41" name="Airplanes" />
      </album>
   </albums>
</rsp>

Other methods return different elements for the second child node, such as <Categories/>, <Images/>, and <Login/>.

If the method call resulted in an error an error response is sent instead.  An example error response is shown below.

<rsp stat="fail">
  <err code="17" msg="invalid method" />
</rsp>

I wanted to wrap up this POX API in a tidy package.  I did not want any of the library code to parse XML.  I wanted the code to only operate on strongly typed objects, i.e. Album, Image, etc.  If a method call to SmugMug resulted in an error I wanted an exception to be thrown – avoid checking flags.  More importantly, I did not want to have to write any of this.  I rightly assumed that I code use WCF (and .NET) to handle all of the heavy lifting.  I’ll cover how in the following posts.

Comments are closed.

Page optimized by WP Minify WordPress Plugin