RESTProxy, a proxy for REST API’s

opsview-blog


So yeah, what’s the greatest disadvantage of using ajax calls in javascript? you must send your request to the same domain. That can be sometimes a pain in the ass. The same goes for my javascript REST API client for Opsview. I wanted to host my code on another server to keep de load to my opsview server as low as possible. so I wrote a RESTProxy. the functionality is quite simple. Let me explain it to you by using my javascript client:

opsview = new opsviewrestapi('my.opsview.server',3000)
opsview.proxy('/RESTProxy.php');
status = new opsview.api('status');
status.ops.state = '2';
status.ops.filter = 'unhandled';
status.get(function(data){
  console.log(data);
});

So what did I do here?First, I defined my server in the opsviewrestapi and his port. Then I defined the Proxy page.What happens in the background?All request are send to the proxy php page instead of going directly to our opsview server. Everything is the same, except the client class adds 2 headers extra with the request. Take a look at the request:

request-URL:http://my.web.server/RESTProxy.php/rest/status/service?state=2&filter=unhandled
requestmethod:GET
Statuscode:200 OK
request headers:
 Accept:application/json
 Content-Type:application/json
 DNT:1
 Referer:http://my.web.server/opsview/index.html
 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10
 X-Opsview-Token:cdb5c6d0c66e3b7ff331d872ceecfaa3c9f7f62b
 X-Opsview-Username:admin
 X-Requested-With:XMLHttpRequest
 x-RESTProxy-Host:my.opsview.server
 x-RESTProxy-Port:3000

See what happened here? the REST API location path is just added to the RESTProxy.php location (apache supports this out of the box), the whole request is just the same as directly to the opsview server except for the last two headers:

x-RESTProxy-Host
x-RESTProxy-Port

These headers are captured by our php rest proxy, deleted of the header list and the whole request is forwarded to the defined server with curl.

The output of the curl request is then returned to our javascript client class just as we expect it with a few differences:

Connection:Keep-Alive
Content-Length:989
Content-Type:application/json
Date:Sun, 26 Feb 2012 11:48:19 GMT
Keep-Alive:timeout=15, max=98
Server:Apache/2.2.14 (Ubuntu)
X-Powered-By:RESTProxy - PHP/5.3.2-1ubuntu4.14
x-RESTProxy-connect-time:0.000108
x-RESTProxy-download-content-lenght:989
x-RESTProxy-namelookup-time:2.8E-5
x-RESTProxy-redirect-time:0
x-RESTProxy-size-download:989
x-RESTProxy-size-upload:0
x-RESTProxy-speed-download:5460
x-RESTProxy-speed-upload:0
x-RESTProxy-start-transfer-time:0.181103
x-RESTProxy-total-time:0.181115
x-RESTProxy-upload-content-lenght:0
x-RESTProxy-url:http://my.opsview.server:3000/rest/status/service?state=2&filter=unhandled

There are some debug headers added if there is a problem with the proxy.

So there you go! take a look at the code on github: http://github.com/subutux/RESTProxy

subutux

subutux wrote 34 posts

Post navigation


Comments

Leave a Reply