HTTP API to HTTP API data mangling/translation module

Status
Not open for further replies.

Arbelac

Ars Tribunus Angusticlavius
7,718
This might seem odd, but I've got the following situation, and Google isn't being helpful...

I have an application that calls an API via HTTP, either as a GET or POST with a number of input items. I need to change the backend provider (which uses a different format) for the API but can't modify the application itself right now.

I'm thinking that an intermediate layer that takes the original API call format, reformats the request to the new backend, sends the request to the new backend, gets the data back, and then reformats the output into the order expected by the application would work.

What I'm not sure of is what to actually use here. I was originally thinking an ISAPI (current solution is IIS-based) or Apache module would be suitable, as it's all web-based, but perhaps there is a better solution? Can I even do processing like this in a module (receive request, process request, send additional request, reformat returned data, return data to original client requestor)?
 

krimhorn

Ars Legatus Legionis
39,865
I have an application that calls an API via HTTP, either as a GET or POST with a number of input items. I need to change the backend provider (which uses a different format) for the API but can't modify the application itself right now.
Elaborate a bit on this please. What technology is the API implemented with? Is it a RESTful API or something more like WCF? You say the application isn't going to change (so the parameters being sent to the API are going to be the same) and that it's a "backend provider" (so I'm thinking data connection, correct me if I'm wrong) change so why do you need to touch the API interface at all?
 

Arbelac

Ars Tribunus Angusticlavius
7,718
The existing "API" is currently implemented as an Apache module on Windows, and calls a local Windows service to return requested data as per the HTTP GET/POST.

This "API" is called by a legacy application via HTTP, and the data request items are formatted in a certain way. We have a new provider for the actual data we need returned, but the format is different. I need to take the input HTTP request, grab the input items, reorder them/reformat them, send it to the new provider, and then reverse the flow on the way out to format the response in the order/format the legacy app requires.

Current:
Application -> Request to component (HTTP) -> Process request -> Generate Output -> Return Output (HTTP)

Proposed:
Application -> Request to Translation Layer (HTTP) -> Reformat Request -> Forward to new "processor" -> Receive Output -> Reformat Output -> Return to Client via Translation layer

I know this is horrible but changing the app just isn't in scope.
 

krimhorn

Ars Legatus Legionis
39,865
[url=http:/civis/viewtopic.php?p=30842171#p30842171:2cnwesnv said:
Arbelac[/url]":2cnwesnv]I know this is horrible but changing the app just isn't in scope.
No, no, that's fine. I just wanted to make sure we were on the same wavelength.

So when I think about this kind of problem I usually approach it from the perspective of "legacy+". That is a 2-step process. The first is to simply build the new API as a new version, using your new input parameters and make sure that it's documented as The Way New Things Talk Ask For This Stuff (if you can restrict the old API in some way to only currently existing applications, all the better). The second would be to actually update the old API. You've already got a conversation path between the application and the API which works (and won't be changing) so, if it's at all possible, just perform the transformations to the new provider within the existing API. You'll want to make sure that you have integration test coverage on the API as-is and then apply those same tests to the updated implementation that talks to the new provider.

I would expect that would be the simplest way to do it, but if you're not in a position to be able to modify the existing code it's, obviously, not an option. In that case you could use a lightweight WebAPI controller that replaces the Apache module endpoint and uses the same public API as the Apache module. You'd need to modify the WebAPI route config such that it's pointing to the correct route (instead of the default /api/<controller>/<verb>/<id> route) but that's not difficult at all. You'll still want a second endpoint that takes the new parameters for future use (both for when you can update the application and for new applications that need the same data). You can simplify your work by implementing the communication with the provider in a shared backend model and only use the API endpoints as the communication (and translation in the case of the legacy parameter endpoint) channel.

In the event you can't have the new API endpoints using the old route you should be able to use the Apache rewrite module to transparently redirect requests to the new endpoints. I'm not too familiar with Apache but my guess is that you'll have to pass the parameters as-is (i.e. no translating within the rewrite/redirect).
 

Arbelac

Ars Tribunus Angusticlavius
7,718
Kind of hate to say it, but this is basically what XSLT was made for.

The source input isn't even close to XML though. It's literally a HTTP POST request (most of the time) with the input items in the query. Ex. http://service:port/<item1>&<item2>&<item3>...

If I was submitting JSON or XML it would probably be easier to use an XML transformation language.
 

Arbelac

Ars Tribunus Angusticlavius
7,718
In case anyone is interested, I realized that I didn't need to write a full filter module for IIS, nor the overhead of running IIS. I instead wrote a Windows service in C#/.NET using HttpListener to open the port and then some code to split up the request and make the additional calls to the new service provider (via HttpWebRequest). The new provider returns JSON formatted data, so I used JSON.NET functions to deserialize the data, and then processed it into the response format required.

Also, http://jsonclassgenerator.codeplex.com/ is a really handy tool. Take a sample JSON output, plunk it in, and it'll generate a C# class file for you. Saved me hours of work.
 
Status
Not open for further replies.