def setUp(self): contract = barrister.Contract(parse(idl)) self.user_svc = UserServiceImpl() self.server = barrister.Server(contract) self.server.add_handler("UserService", self.user_svc) transport = barrister.InProcTransport(self.server) self.client = barrister.Client(transport)
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)
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(): return v2_server.call_json(request.body.read())