def loop(self, client_socket, address): response = {} protocol = HttpProtocol(2048) protocol.set_response(response) response['body'] = dump_json(RESPONSE) client_socket.send(prepare_response(response)) client_socket.close()
def on_status_request(client_socket, session: Session, status: dict): """Send a the static frontend to the client.""" protocol = HttpProtocol() protocol.set_response(session.response) session.response['body'] = status session.response['body'] = dump_json(session.response['body'], indent=2) prepared_response = prepare_response(session.response) client_socket.send(prepared_response)
def error_send(self, client_socket, exc: Exception): """Send internal server error to the client.""" response = {} protocol = self.get_protocol() protocol.set_response(response) response['body'] = dump_json({'error': repr(exc)}, indent=2) response['status'] = 500 client_socket.send(prepare_response(response))
def on_proxy_error(client_socket, exc: Exception): """Send internal server error to the client.""" session = Session() protocol = HttpProtocol() protocol.set_response(session.response) session.response['body'] = dump_json({'error': repr(exc)}, indent=2) session.response['status'] = 500 prepared_response = prepare_response(session.response) client_socket.send(prepared_response)
def status_send(self, client_socket, request): """Send a the static frontend to the client.""" response = {} protocol = self.get_protocol() protocol.set_response_parser() protocol.set_response(response) response['body'] = self.status response['body'] = dump_json(response['body'], indent=2) client_socket.send(prepare_response(response))
def loop(self, client_socket, address): response = Response() response.body = dump_json([ {'id': 7, 'body': 'a choice'}, {'id': 23, 'body': 'another choice'}, {'id': 24, 'body': 'a third choice'}, {'id': 3, 'body': 'notha one'}, {'id': 4, 'body': 'banana 🍌'}, ]) client_socket.send(response.prepare(Request())) client_socket.close()
def reorder_response(self, request, response, ranks): """Reorder Elasticsearch hits""" body = load_json(response.body) body['_nboost'] = '⚡NBOOST' body['hits']['hits'] = [body['hits']['hits'][rank] for rank in ranks] jkwargs = {'ensure_ascii': False} if 'pretty' in request.url.query: jkwargs.update({'indent': 2}) response.body = dump_json(body, **jkwargs)
def multiply_request(self, request: Request) -> Tuple[int, List[str]]: """Multiply size of Elasticsearch query""" body = load_json(request.body) topk = request.url.query.pop('size', None) correct_cids = request.url.query.pop('nboost', None) # search for topk in body if body: correct_cids = body.pop('nboost', correct_cids) topk = body.pop('size', topk) topk = 10 if topk is None else int(topk) if body: body['size'] = topk * self.multiplier request.body = dump_json(body) else: request.url.query['size'] = str(topk * self.multiplier) correct_cids = correct_cids.split(',') if correct_cids else None return topk, correct_cids
def on_client_response(session: Session, client_socket): """Send the ranked results to the client""" kwargs = dict(indent=2) if 'pretty' in session.request['url']['query'] else {} session.response['body'] = dump_json(session.response['body'], **kwargs) prepared_response = prepare_response(session.response) client_socket.send(prepared_response)
def reorder_response(self, request, response, ranks): response.body = dump_json(load_json(response.body)[:len(ranks)])
from nboost.types import Request, Response, URL from nboost.helpers import dump_json, load_json from nboost.codex.es import ESCodex import unittest REQUEST_URL = URL(b'/test/_search?pretty&q=message:testing&size=15') REQUEST_BODY = dump_json({ "size": 100, "query": { "match": { "passage": { "query": "this is a test" } } } }) RESPONSE_BODY = dump_json({ "took": 5, "timed_out": False, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq"
def run(self): """Same as socket server run() but logs""" self.logger.info(dump_json(self.config, indent=4).decode()) super().run()
def client_send(request: dict, response: dict, client_socket): """Send the ranked results to the client""" kwargs = dict(indent=2) if 'pretty' in request['url']['query'] else {} response['body'] = dump_json(response['body'], **kwargs) client_socket.send(prepare_response(response))
def server_send(server_socket: socket.socket, request: dict): """Send magnified request to the server""" request['body'] = dump_json(request['body']) request['headers']['content-type'] = 'application/json; charset=UTF-8' server_socket.send(prepare_request(request))
def loop(self, client_socket, address): session = Session() session.response['body'] = dump_json(RESPONSE) prepared_response = prepare_response(session.response) client_socket.send(prepared_response) client_socket.close()