Created
March 14, 2014 10:52
-
-
Save aminamid/9545612 to your computer and use it in GitHub Desktop.
Access RIAK REST API from angularJS $resource by Tornado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import json | |
import tornado | |
from tornado import websocket, web, ioloop, gen, httpclient | |
ws_client = [] | |
class IndexHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.render("index.html") | |
class CollectionHandler(tornado.web.RequestHandler): | |
@gen.coroutine | |
def get(self,coll): | |
req = "http://127.0.0.1:8098/buckets/{0}/keys".format(coll) | |
url = tornado.httputil.url_concat(req, {"keys": "true"}) | |
http_client = httpclient.AsyncHTTPClient() | |
response = yield http_client.fetch(url) | |
if response.error: | |
response.rethrouw() | |
self.set_header('content-type', 'application/json') | |
keynames = json.loads(response.body) | |
result = [] | |
res_tmp = {} | |
for entity in keynames["keys"]: | |
res_tmp[entity] = yield http_client.fetch("{0}/{1}".format(req,entity)) | |
tmplist = json.loads(res_tmp[entity].body) | |
tmplist["key"] = entity | |
result.append( tmplist ) | |
self.write(json.JSONEncoder().encode(result)) | |
self.finish() | |
def post(self): | |
self.write() | |
class ItemHandler(tornado.web.RequestHandler): | |
@gen.coroutine | |
def get(self,coll,key): | |
req = "http://127.0.0.1:8098/buckets/{0}/keys/{1}".format(coll,key) | |
http_client = httpclient.AsyncHTTPClient() | |
response = yield http_client.fetch(req) | |
if response.error: | |
response.rethrouw() | |
self.set_header('content-type', 'application/json') | |
self.write(json.JSONEncoder().encode(response.body)) | |
self.finish() | |
def delete(self,coll,key): | |
self.write() | |
app = tornado.web.Application([ | |
(r'/', IndexHandler), | |
(r'/api/([^/]+)', CollectionHandler), | |
(r'/api/([^/]+)/([^/]+)', ItemHandler), | |
], | |
debug=True, | |
template_path=os.path.join(os.path.dirname(__file__), "templates"), | |
static_path=os.path.join(os.path.dirname(__file__), "static"), | |
) | |
if __name__ == '__main__': | |
app.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-resource.min.js"></script> | |
<script language="javascript"> | |
angular.module("myApp", ["ngResource"]) | |
.controller("myCtrl", ["$scope","$resource",function($scope,$resource) { | |
var mybucket = $resource("api/mybucket"); | |
$scope.hosts = mybucket.query(); | |
}]); | |
</script> | |
</head> | |
<body> | |
<div ng-app="myApp"> | |
<div ng-controller='myCtrl'> | |
<ul> | |
<li ng-repeat='host in hosts'> | |
{{!host.id}}: {{!host.name}} | |
</li> | |
</ul> | |
<hr> | |
<pre>{{!hosts|json}}</pre> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment