views:

480

answers:

2

I work on a couple of projects that connect with external services like Facebook and Netflix. At this moment most libraries I'm using to access these APIs ( including the ones I've written myself ) have single methods so call specific API functions yet always seem to call some sort of base method to make the request. Something like this:

public class ExternalApi
{
    public string SendMessage( criteria )
    {
         //do something unique to this method with criteria like
         //like generating an xml statement or fql query

         return SendRestRequest( modifiedCriteria );
    }

    public string GetData( criteria )
    {
         //do something unique to this method with criteria like
         //like generating an xml statement or fql query

         return SendRestRequest( modifiedCriteria );
    }

    public string SendRestRequest( modifiedCriteria )
    {
         //add global things to modifiedCriteria like authentication bits
         //or wrapping the criteria in some xml or json shell

        var request = new HttpRequest();
        //make the request, return data
    }
}

So my question is there a better pattern or OO principal to use here so in each singular API call method I'm not explicitly calling a base method every time?

Is what I'm looking for some kind of invocation interception pattern, like the ASP.NET MVC framework and ActionResults?

Edit 1: I'm not looking to use the features of any other service or library like Wcf. For these projects I'm only using 1-5% of these API's capabilities and prefer to roll my own code for these things.

+3  A: 

Here is a good sample:

REST and POX at MSDN

Also take a look at WCF REST Starter Kit Preview 2 at CodePlex, there should be coding samples related to REST client

And here: REST in Windows Communication Foundation (WCF)

This one could also be helpfull: Sample Client Libraries for REST APIs

Koistya Navin
Useful links, thanks!
Steve Haigh
@Steve, you're welcome! :-)
Koistya Navin
A: 

I had to put together a REST Client for one of my degree assignments. I went with a tiered approach and tried to apply the facade pattern to abstract the details

interface HTTPRequest{
  public void get();
  public void post();
  public void put();
  public void delete();
}

HTTPRequest provided the basic HTTP functionality, i then created RestClient

interface RestClient{
  public void create();
  public void read();
  public void update();
  public void delete();
}

This gave me a more CRUD like interface. What i then did was to construct a basic RestClient class that could be subclassed to add custom functionality. Each function had a respective callback function that it called before it returned it's data to the caller. This was named something like "createResultProcessor", this could over written in a subclass of RestClient, to provide Custom Result parsing etc.

Never thought i would say this as i've made enough UML diagrams for a lifetime, but SO could really do with some UML diagram facilities!

Jonathan