def format_output(res, default_type="text/plain", field_name=None, list_field_name=None): """ Format the output of the API responses """ accept = get_media_type('Accept') if accept: content_type = None for accept_item in accept: if accept_item in ["application/json", "application/*"]: if isinstance(res, RADL): if field_name: res_dict = {field_name: radlToSimple(res)} info = json.dumps(res_dict) else: info = dump_radl_json(res, enter="", indent="") # This is the case of the "contains" properties elif isinstance(res, dict) and all(isinstance(x, Feature) for x in res.values()): features = Features() features.props = res res_dict = featuresToSimple(features) if field_name: res_dict = {field_name: res_dict} info = json.dumps(res_dict) else: # Always return a complex object to make easier parsing # steps info = format_output_json(res, field_name, list_field_name) content_type = "application/json" break elif accept_item in [default_type, "*/*", "text/*"]: if default_type == "application/json": info = format_output_json(res, field_name, list_field_name) else: if isinstance(res, list): info = "\n".join(res) else: info = str(res) content_type = default_type break if content_type: bottle.response.content_type = content_type else: return return_error(415, "Unsupported Accept Media Types: %s" % ",".join(accept)) else: if default_type == "application/json": info = format_output_json(res, field_name, list_field_name) else: if isinstance(res, list): info = "\n".join(res) else: info = str(res) bottle.response.content_type = default_type return info
def format_output(res, default_type="text/plain", field_name=None, list_field_name=None): """ Format the output of the API responses """ accept = get_media_type('Accept') if accept: content_type = None for accept_item in accept: if accept_item in ["application/json", "application/*"]: if isinstance(res, RADL): if field_name: res_dict = {field_name: radlToSimple(res)} info = json.dumps(res_dict) else: info = dump_radl_json(res, enter="", indent="") # This is the case of the "contains" properties elif isinstance(res, dict) and all(isinstance(x, Feature) for x in res.values()): features = Features() features.props = res res_dict = featuresToSimple(features) if field_name: res_dict = {field_name: res_dict} info = json.dumps(res_dict) else: # Always return a complex object to make easier parsing # steps info = format_output_json(res, field_name, list_field_name) content_type = "application/json" break elif accept_item in [default_type, "*/*", "text/*"]: if default_type == "application/json": info = format_output_json(res, field_name, list_field_name) else: if isinstance(res, list): info = "\n".join(res) else: info = str(res) content_type = default_type break if content_type: bottle.response.content_type = content_type else: return return_error(415, "Unsupported Accept Media Types: %s" % ",".join(accept)) else: if default_type == "application/json": info = format_output_json(res, field_name, list_field_name) else: if isinstance(res, list): info = "\n".join(res) else: info = str(res) bottle.response.content_type = default_type return info
def get_json_radl(self): """ Get the RADL of this Infrastructure in JSON format to send it to the Ansible inventory """ radl = self.radl.clone() res_radl = RADL() res_radl.systems = radl.systems res_radl.networks = radl.networks res_radl.deploys = radl.deploys json_data = [] # remove "." in key names for elem in radlToSimple(res_radl): new_data = {} for key in elem.keys(): new_data[key.replace(".", "_")] = elem[key] json_data.append(new_data) return json.dumps(json_data)