REST API Versioning in Header
I have seen this post and appreciate that discussion: API RESTful Version | InterSystems Developer Community | Business Service
However our org requirement is for a caller to provide the API version in the HTTP request header. I am finding the cleanest way to route to the correct class using the header version. My classes are setup as
API.Service.v1
API.Service.v2
Of course only v1 exists now but when v2 goes live on future day, API.Service.v2 will extend API.Service.v1 so we only have to override the method that changes or add a new method if needed.
I thought I might be able to modify %reqeust.URL before the dispatch method takes over. OnPreDispatch takes pUrl by value and it's already set from %request.URL way ahead of that method. Same with AccessCheck(). I tried OnPreHTTP() as well but that method doesn't even seem to be firing.
My understanding is that passing the version in the path is just as common as passing in the header, so I'm wondering if someone has solved this.
Other ideas swimming in my head:
- Just route to one class and do logical switches in each method based on the version in the header (this might get messy if we do a ton of versions but might be fine if we don't have a ton of versions).
- I thought I could do a "preroute" and modify %request.URL before it went to the proper router, but testing that out, I think I'm nested in the stack somewhere where I can't figure and it's not working as I want it to.
Any ideas?
Hi @Michael Davidovich - it's been a while! Here's a quick sample for how I'd do this:
Thanks, @Timothy Leavitt!
This was incredibly helpful. I modified it a bit as that $CASE statement would have grown over time into something not fun to manage.
I created the main dispatch class and prepended the version to the pUrl parameter in the OnPredispatch method and then passed that into a main router class using DispatchRequest similar do what you did there. We can maintain the routemap in that main router class to keep things more organized and standard and from there it simply acts as normal.
THANK YOU!