def start_worker(): # create a thread local that we can use to store request headers req_context = threading.local() contract = barrister.contract_from_file("../redis-msgpack.json") server = barrister.Server(contract) server.add_handler("ContactService", ContactService(req_context)) redis_client = redis.StrictRedis("localhost", port=6379) while True: raw_msg = redis_client.brpop([ "contact" ], timeout=1) if raw_msg: (headers, req) = load_msg(raw_msg[1]) if headers.has_key("reply_to"): tls = threading.local() # set the headers on the thread local req_context req_context.headers = headers resp = server.call(req) redis_client.lpush(headers["reply_to"], dump_msg(headers, resp)) req_context.headers = None
#!/usr/bin/env python from flask import Flask, request, make_response import barrister import sys class Calculator(object): def add(self, a, b): return a+b def subtract(self, a, b): return a-b app = Flask(__name__) server = barrister.Server(barrister.contract_from_file(sys.argv[1])) server.add_handler("Calculator", Calculator()) @app.route("/calc", methods=["POST"]) def calc(): json_resp = server.call_json(request.data) resp = make_response(json_resp) resp.headers['Content-Type'] = 'application/json' return resp app.run(host="127.0.0.1", port=8080)
existing = self.getPage(id) if existing: if existing["version"] == version: del self.pagesById[id] return True else: raise barrister.RpcException(30, "Version is out of date") else: return False def getPage(self, id): if self.pagesById.has_key(id): return self.pagesById[id] else: return None contract = barrister.contract_from_file("../validation.json") server = barrister.Server(contract) server.add_handler("ContentService", ContentService()) app = Flask(__name__) @app.route("/content", methods=["POST"]) def content(): resp_data = server.call_json(request.data) resp = make_response(resp_data) resp.headers['Content-Type'] = 'application/json' return resp app.run(host="127.0.0.1", port=7667)
class TodoManager(object): def __init__(self, store): self.store = store def readTodos(self): return self.store.get_all() def createTodo(self, properties): return self.store.save(properties) def updateTodo(self, todo): return self.store.update(todo['id'], todo) def deleteTodo(self, todo): return self.store.delete(todo['id']) store = Store() todo_manager = TodoManager(store) contract = barrister.contract_from_file('../../todo_manager.v1.json') server = barrister.Server(contract) server.add_handler('TodoManager', todo_manager) @post('/v1/todos') def todos(): return server.call_json(request.body.read()) run(host='localhost', port=3000)
from flask import Flask, request, make_response import barrister # Our implementation of the 'Calculator' interface in the IDL class Calculator(object): # Parameters match the params in the functions in the IDL def add(self, a, b): return a+b def subtract(self, a, b): return a-b contract = barrister.contract_from_file("./calc.json") server = barrister.Server(contract) server.add_handler("Calculator", Calculator()) app = Flask(__name__) @app.route("/calc", methods=["POST"]) def calc(): resp_data = server.call_json(request.data) resp = make_response(resp_data) resp.headers['Content-Type'] = 'application/json' return resp app.run(host="127.0.0.1", port=8080)
#!/usr/bin/env python from flask import Flask, request, make_response import barrister class Echo(object): def echo(self, s): if s == "err": raise barrister.RpcException(99, "Error!") else: return s contract = barrister.contract_from_file("../batch.json") server = barrister.Server(contract) server.add_handler("Echo", Echo()) app = Flask(__name__) @app.route("/batch", methods=["POST"]) def batch(): resp_data = server.call_json(request.data) resp = make_response(resp_data) resp.headers['Content-Type'] = 'application/json' return resp app.run(host="127.0.0.1", port=7667)
# Our single URL handler for this interface # @post('/contact') def contact(): # Read the raw POST data data = request.body.read() # Hand the request to the Barrister Server object # It will call the correct function on the ContactService resp_json = server.call_json(data) # Set the MIME type, and return the response response.content_type = 'application/json' return resp_json if __name__ == '__main__': # initialize the Python logger. totally optional. logging.basicConfig() # load the IDL JSON and create a barrister.Contract object contract = barrister.contract_from_file("../contact.json") # create a Server that will dispatch requests for this Contract server = barrister.Server(contract) # register our class with the "ContactService" interface server.add_handler("ContactService", ContactService()) # Starts the Bottle app run(host='localhost', port=7186, quiet=True)
from Beta import * from flask import Flask, request, make_response import barrister # Implementation of the Alpha interface in the IDL class Calculator(object): def add(self, p_cParStr): print p_cParStr print p_cParStr['clase'] return "" contract = barrister.contract_from_file("interface_structura.json") server = barrister.Server(contract) server.add_handler("Calculator", Calculator()) app = Flask(__name__) @app.route("/william", methods=["POST"]) def get_Alpha(): resp_data = server.call_json(request.data) resp = make_response(resp_data) resp.headers['Content-Type'] = 'application/json' return resp app.run(host="127.0.0.1", port=7667) #app.run(host="10.0.159.15", port=7667)
else: from smtpservice import SMTPService from cloud import CloudFilesService hosting_service = CloudFilesService() mail_service = SMTPService(smtp_user, smtp_password, smtp_server, smtp_port) db = Db(db_url) repository = Repository(db, mail_service) projectService = ProjectService(env_domain, mail_service, repository) authService = AuthService(db) ################################################################# # barrister # ############# contract = barrister.contract_from_file('sms.json') server = barrister.Server(contract) server.add_handler('AuthService', authService) server.add_handler('ProjectService', projectService) ################################################################# # request handlers # #################### @app.route('/api', methods=['POST']) def api(): if __authenticated(flask.request.data): return server.call_json(flask.request.data) else: raise barrister.RpcException(1000, 'User is not logged in')
import barrister from service.ReadabilityService import ReadabilityService sys.path.append('./') # import the root directory @route('/hello2/<name>') def index(name): return template('<b>Hello {{name}}</b>!', name=name) @post('/readability') def read(): resp_data = server.call_json(request.body.getvalue()) response.content_type="application/json" return resp_data @post('/hello') def hello(): print request.json return "Hello World!" fn = os.path.join(os.path.dirname(__file__), './idl/readability.json') contract = barrister.contract_from_file(fn) server = barrister.Server(contract) server.add_handler("ReadabilityService", ReadabilityService()) if __name__ == "__main__": run(host='localhost', port=8080, debug=True)
def __init__(self): self.users = { } self.users['scott'] = { "username" : "scott", "firstName" : "Scott", "lastName" : "Smith" } def login(self, username, passwd): if username == "scott" and passwd == "tiger": return self.users['scott'] else: raise barrister.RpcException(100, "Invalid username/password") def updateUser(self, user): self.users[user['username']] = user return True contract = barrister.contract_from_file("idl/auth.json") auth_svr = barrister.Server(contract) auth_svr.add_handler("AuthService", AuthService()) ######################################## JS_LIBS = """ <script src="/static/js/jquery-1.7.2.min.js"></script> <script src="/static/js/json2.min.js"></script> <script src="/static/js/mustache.min.js"></script> <script src="/static/js/underscore-min.js"></script> <script src="/static/js/backbone-min.js"></script> <script src="/static/js/barrister.browser.min.js"></script> <script src="/static/js/build/app-full.js"></script> """
def updateTodo(self, todo): return self.store.update(todo['id'], todo) def deleteTodo(self, id): return self.store.delete(id) class TodoManagerV1Adapter(TodoManager): def deleteTodo(self, todo): return TodoManager.deleteTodo(self, todo['id']) store = Store() v1_contract = barrister.contract_from_file('../../todo_manager.v1.json') v1_server = barrister.Server(v1_contract) v1_server.add_handler('TodoManager', TodoManagerV1Adapter(store)) v2_contract = barrister.contract_from_file('../../todo_manager.v2.json') v2_server = barrister.Server(v2_contract) v2_server.add_handler('TodoManager', TodoManager(store)) @post('/v1/todos') def todos_v1(): return v1_server.call_json(request.body.read()) @post('/v2/todos') def todos_v2():