def text_response(response_text, api_name, request_type, status_code=200): """Create a rest response of content-type text/plain. Parameters: ---------- response_text : string Response text. api_name: string Name of the api. request_type: string Type of the request. status_code : integer, optional (the default is 200) Returns ------- flask.Response """ app.logger.info("Response from '%s/%s': '%s'", api_name, request_type, response_text) response = _make_response(response_text) response.headers['content-type'] = "text/plain" response.status_code = status_code return response
def json_response(json_object, api_name, request_type, status_code=200): """Create a rest response of content-type application/json. Parameters: ---------- json_object : dict|list Response json object. api_name: string Name of the api. request_type: string Type of the request. status_code : int (the default is 200) Returns ------- flask.Response """ json_string = _json.dumps(json_object, indent=2) app.logger.info("Response from '%s/%s': %s", api_name, request_type, json_string) response = _make_response(json_string) response.headers['content-type'] = "application/json" response.status_code = status_code return response
def make_response(ret, code): resp = _make_response(ret, code) resp.mimetype ='text/xml; charset=utf-8' return resp
def make_response(ret, code): resp = _make_response(ret, code) resp.mimetype = 'text/xml; charset=utf-8' return resp
def wm_INTERNAL_redir (self, path): self.wm_debug_log ("--- redirect received ---") self.wm_debug_log ("checking hostname") hostname = get_url_hostname (_request.base_url) self.wm_debug_log ("target hostname is {}".format (hostname)) found_app = False for appid in self.wm_enableds.keys (): if hostname in self.wm_config["apps"][appid]["hostnames"]: found_app = True app = self.wm_enableds[appid] self.wm_debug_log ("hostname {0} matches app {1}".format (hostname, app)) break if not found_app: self.wm_debug_log ("hostname {} doesn't match any app!".format (hostname)) ret = self.wm_config["not_found_response"] return ret base_path = self.wm_config["apps"][appid]["base_path"] old_abspath = getcwd () self.wm_debug_log ("changing cwd to {}".format (base_path)) chdir (base_path) self.wm_debug_log ("cwd is now {}".format (getcwd ())) old_path = relpath (old_abspath) if app["first_request"]: app["first_request"] = False for before_first_request_func in app["app"].before_first_request_funcs: before_first_request_func () # TODO add blueprint support for blueprint, before_request_func_list in app["app"].before_request_funcs.items (): for before_request_func in before_request_func_list: before_request_func () self.wm_debug_log ("done calling before functions") # print (print_object (app.url_map)) # set the module's request object to the actual request object # so the route function can access the actual request information app["module"].request = _request self.wm_debug_log ("bound to actual request") # bind the app's url map to the actual request urls = app["app"].url_map.bind_to_environ (_request) self.wm_debug_log ("bound to real environment") # get the endpoint and arguments for the actual request self.wm_debug_log ("matching") error = False try: endpoint, args = urls.match () except NotFound: print_exc () error = True ret = "Not Found", 404 endpoint, args = None, None except MethodNotAllowed: print_exc () error = True ret = "Method Not Allowed", 405 endpoint, args = None, None except RequestRedirect as redir: print_exc () error = True ret = _redirect (redir.new_url) endpoint, args = None, None except: print_exc () error = True endpoint, args = None, None if error: self.wm_debug_log ("error return value is {}".format (ret)) self.wm_debug_log ("matched") print ("### ENDPOINT: {0}, ARGS: {1}".format (endpoint, args)) if not error: for function_name, view_function in app["app"].view_functions.items (): if function_name == endpoint: self.wm_debug_log ("calling {}".format (function_name)) try: ret = _make_response (view_function (**args)) except HTTPException as exc: print_exc () self.wm_debug_log ( "error when calling view function -- code: {0}, desc: {1}, name: {2}, response: {3} ###".format ( exc.code, exc.description, exc.name, exc.response) ) ret = exc.get_response (environ = _request) # WARNING: any other non-abort () exceptions will be raised # and will call the Flask debugger without calling the below code! else: self.wm_debug_log ("not calling view function {}".format (function_name)) # TODO also add blueprint support for blueprint, after_request_func_list in app["app"].after_request_funcs.items (): for after_request_func in after_request_func_list: self.wm_debug_log ("Response before calling {0}: {1}".format (after_request_func.__name__, ret)) after_request_ret = after_request_func (_make_response (ret)) self.wm_debug_log ("Response after calling {0}: {1}".format (after_request_func.__name__, after_request_ret)) ret = after_request_ret self.wm_debug_log ("changing dir back to {}".format (old_path)) chdir (old_path) self.wm_debug_log ("--- redirect completed, returning ---") return ret