0

I make WCF REST api calls from a client. For this, I use a dedicated object of type WebChannelFactory<IRestApi>, where IRestApi is a ServiceContract.

Then, I can just call methods of IRestApi through a channel I use (created by CreateChannel()).

My question is: Can I add constant parameter to this channel (in my case a version number) so I will not have to pass it over and over in each of the calls?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
user1028741
  • 2,745
  • 6
  • 34
  • 68
  • You can't add `Version` property to `IRestApi`? – DavidG Oct 07 '14 at 11:56
  • @DavidG, I'm not sure what you mean. IRestApi is an interface defining the rest contract between the client and server. IRestApi has many methods. I can add a "version" parameter to each of these method, but I thought maybe I have a better and more elegant way to do so, because the version property is constant (so I would like to load it to the cannel)... – user1028741 Oct 07 '14 at 12:03
  • Are you accessing the API over HTTP? You could add a header there. – DavidG Oct 07 '14 at 12:04
  • @DavidG, you are right, I thought about this option and it should work. I don't do it, because I need to be able to call this service directly from the browser sometimes (and I cannot add headers to the call from the browser...) – user1028741 Oct 07 '14 at 12:19
  • @thecoshman can you perhaps not mass-edit "rest" into "REST" on low-view, zero-vote, half-year-old question? – CodeCaster Aug 19 '15 at 10:25

1 Answers1

0

I'm not sure how exactly your objects/interfaces are composed together, nor are you very clear on how you want to send this version to the server. The normal approach with REST API versions is to put the version into the URI, sort of like example.com/service/v8/....

In that case, maybe you can use a decorator object that that can wrap this WebChannel (I am presuming that is the type you would call something like .get("/some/url") on). Your decorator would be able to 'inject' additional data.

You call decoratedObject.setURLPreFix("/some_serivce/v3"), then decoratedObject.get("/some/url"), the decorator function would be implemented something like...

class WebChannelDecorator{
  WebChannel channel;
  String urlPrefix = "";
  WebChannelDecorator(WebChannel c){
    channel = c;
  }
  /* methods you want to work just the same */
  T foo(args){
    return channel.foo(args);
  }
  /* methods you want to 'decorate' */
  T get(String url){
    return channel.get(urlPrefix + url);
  }
  void setURLPrefix(String prefix){
    urlPrefix = prefix;
  }
}

This may not actually be called the decorator pattern, but the idea is what matters, not what you call it.

thecoshman
  • 8,394
  • 8
  • 55
  • 77