Search code examples
.netrestlanguage-agnostic

Guidance for writing a wrapper for a REST API


I've written a few very casual wrappers around REST and less structured web interfaces, but all just for fun, with very little attention to error detection and handling, timeouts, etc.

Can somebody please give me some pointers, either on practices, or to resources, for developing a solid, professional .NET (or other platform) wrapper for a REST API?

Things that cross my mind are:

  1. Interpreting HTTP error codes and filtering protocol errors from API errors.
  2. Building URL based requests, possibly with pattern matching and token substitution.
  3. Matching post-backs to requests.
  4. How to map an OO model to the REST model. Some requests suit static methods, e.g. getting lists, and others hang in the middle between static and instance, maybe a plain provider pattern with all static methods?

NEW: I've since found this almost too basic guide to making Yahoo REST calls, but it serves as a starting point.

IMPORTANT: A very complex and seldom covered aspect is how to deal with forms authentication on the REST API. Separate login requests, cookie storage, etc.


Solution

  • Although I have wrapped countless web services in an OOP-ish way, I find it difficult to come up with a good answer to your question.

    A couple of Yahoo API examples can be found here: http://curlobjects.com/trac/browser/trunk/lib/YahooApi

    1 Interpreting HTTP error codes and filtering protocol errors from API errors.

    My base http class has an http error callback that can be overriden.

    2 Building URL based requests, possibly with pattern matching and token substitution.

    I prefer string concatenation.

    4 How to map an OO model to the REST model. Some requests suit static methods, e.g. getting lists, and others hang in the middle between static and instance, maybe a plain provider pattern with all static methods?

    Sometime you need one big static factory/provider, sometimes you want a class for each method, sometimes a class for each group of related methods... There really isn't one proper way to go about this. I always spend some time getting a feel of the service's structure and then try to find an elegant way to abstract the repetitive parts.

    If you have any more specific questions regarding login forms, cookies, etc, I'd be glad to answer them.

    Edit:

    HttpWebRequest was mentioned and I'm guessing you either need to set the ClientCertificate property and use SSL properly, or you can ignore all SLL errors with a CertificatePolicy.