def _handler(handler, request): logging.info('Request received: %s', request.full_path) kw = request.args.to_dict() # The frontend needs "simple" data (e.g. NumPy arrays converted to lists), # but for requests from Python we may want to use the invertible encoding # so that datatypes from remote models are the same as local ones. response_simple_json = utils.coerce_bool( kw.pop('response_simple_json', True)) data = serialize.from_json(request.data) if len(request.data) else None outputs = fn(data, **kw) response_body = serialize.to_json(outputs, simple=response_simple_json) return handler.respond(request, response_body, 'application/json', 200)
def _handler(app: wsgi_app.App, request, environ): kw = request.args.to_dict() # The frontend needs "simple" data (e.g. NumPy arrays converted to lists), # but for requests from Python we may want to use the invertible encoding # so that datatypes from remote models are the same as local ones. response_simple_json = utils.coerce_bool( kw.pop('response_simple_json', True)) data = serialize.from_json(request.data) if len( request.data) else None # Special handling to dereference IDs. if data and 'inputs' in data.keys() and 'dataset_name' in kw: data['inputs'] = self._reconstitute_inputs( data['inputs'], kw['dataset_name']) outputs = fn(data, **kw) response_body = serialize.to_json(outputs, simple=response_simple_json) return app.respond(request, response_body, 'application/json', 200)
def test_coerce_bool(self): self.assertTrue(utils.coerce_bool(True)) self.assertTrue(utils.coerce_bool(1)) self.assertTrue(utils.coerce_bool(2.2)) self.assertTrue(utils.coerce_bool(True)) self.assertTrue(utils.coerce_bool([0])) self.assertTrue(utils.coerce_bool({"a": "hi"})) self.assertTrue(utils.coerce_bool("this is true")) self.assertFalse(utils.coerce_bool("")) self.assertFalse(utils.coerce_bool(0)) self.assertFalse(utils.coerce_bool("0")) self.assertFalse(utils.coerce_bool(False)) self.assertFalse(utils.coerce_bool("false")) self.assertFalse(utils.coerce_bool("False")) self.assertFalse(utils.coerce_bool({})) self.assertFalse(utils.coerce_bool([]))