def my_module2_mock(url, request): body = Request.from_json(request.body) m = {'relevance': 0.3, 'accuracy': 1} response = Response('en', body.tree, m, [TraceItem('module2', body.tree, m)]) return {'status_code': 200, 'content': '[%s]' % response.as_json()}
def testFromJson(self): j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [], 'tree': {'type': 'resource', 'value': 'foo'}} self.assertEqual(Request('1', 'en', Resource(value='foo'), {}, []), Request.from_dict(j)) self.assertEqual(Request('1', 'en', Resource(value='foo'), {}, []), Request.from_json(json.dumps(j))) self.assertEqual(json.loads(Request.from_dict(j).as_json()), j)
def my_module2_mock(url, request): r = Request.from_json(request.body) if r.tree == Resource('two'): c = '"measures": {"accuracy": 1, "relevance": 2}, "tree": {"type": "resource", "value": "three"}' return {'status_code': 200, 'content': '[{"language": "en", %s, ' '"trace": [{"module": "module1", %s}]}]' % (c, c)} else: return {'status_code': 200, 'content': '[]'}
def my_module2_mock(url, request): r = Request.from_json(request.body) if r.tree == Resource('two'): c = '"measures": {"accuracy": 1, "relevance": 2}, "tree": {"type": "resource", "value": "three"}' return { 'status_code': 200, 'content': '[{"language": "en", %s, ' '"trace": [{"module": "module1", %s}]}]' % (c, c) } else: return {'status_code': 200, 'content': '[]'}
def testFromJson(self): j = { 'id': '1', 'language': 'en', 'response-language': 'en', 'measures': {}, 'trace': [], 'tree': { 'type': 'resource', 'value': 'foo' } } self.assertEqual(Request('1', 'en', Resource(value='foo'), {}, []), Request.from_dict(j)) self.assertEqual(Request('1', 'en', Resource(value='foo'), {}, []), Request.from_json(json.dumps(j))) self.assertEqual(json.loads(Request.from_dict(j).as_json()), j)
def process_request(self, request): """Processes a request.""" try: request = Request.from_json(request.read().decode()) except ValueError: raise ClientError('Data is not valid JSON.') except KeyError: raise ClientError('Missing mandatory field in request object.') except AttributeNotProvided as exc: raise ClientError('Attribute not provided: %s.' % exc.args[0]) (start_wall_time, start_process_time) = self._get_times() answers = self.router_class(request).answer() self._add_times_to_answers(answers, start_wall_time, start_process_time) answers = [x.as_dict() for x in answers] return self.make_response('200 OK', 'application/json', json.dumps(answers) )