def do_list_category(args, config): """ Lists out all the state associating with the UUIDs in the Transaction Family : Category Args: config (ConfigParser): ConfigParser which contains the default url Returns: type: str String representing JSON object which allows the client to know that the call was either a success or a failure. Raises: CategoryException: * If failed to retrieve the list """ b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) category_list = client.list_category() if category_list is not None: category_list.sort(key=lambda x: x["timestamp"], reverse=True) result = json.dumps(category_list) output = ret_msg("success", "OK", "ListOf:CategoryRecord", result) print(output) else: raise CategoryException("Could not retrieve category listing.")
def do_amend_category(args, config): """ Amends the state associating with the UUID in the Transaction Family : Category Args: args (ArgumentParser): ArgumentParser object containing required parameters config (ConfigParser): ConfigParser which contains the default url Returns: type: str String representing JSON object which allows the client to know that the call was either a success or a failure. """ category_id = args.category_id category_name = args.category_name description = args.description private_key = args.private_key public_key = args.public_key payload = "{}" key = json.loads(payload) key["publickey"] = public_key key["privatekey"] = private_key key["allowedrole"] = [{"role": "admin"}, {"role": "member"}] payload = json.dumps(key) headers = {"content-type": "application/json"} response = requests.post("http://127.0.0.1:818/api/sparts/ledger/auth", data=json.dumps(key), headers=headers) output = response.content.decode("utf-8").strip() statusinfo = json.loads(output) if statusinfo.get("status") and statusinfo.get("message"): status = statusinfo["status"] message = statusinfo["message"] if status == "success" and message == "authorized": b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) response = client.amend_category(category_id, category_name, description, private_key, public_key) print_msg(response, "amend") else: print(output) else: print(output)
def do_retrieve_category(args, config): category_id = args.category_id url = config.get('DEFAULT', 'url') client = CategoryBatch(base_url=url) data = client.retreive_category(category_id) if data is not None: result = filter_output(str(data)) output = ret_msg("success", "OK", "CategoryRecord", result) print(output) else: raise CategoryException("Category not found: {}".format(category_id))
def api_do_amend_category(args, config): """ API version of "do_amend_category" function. """ param_check = _payload_check_(args) if param_check[0]: return ret_msg("failed", param_check[1], "EmptyRecord", "{}") category_id = args["category"]["uuid"] category_name = _null_cast(args["category"], "name") description = _null_cast(args["category"], "description") private_key = args["private_key"] public_key = args["public_key"] payload = "{}" key = json.loads(payload) key["publickey"] = public_key key["privatekey"] = private_key key["allowedrole"] = [{"role": "admin"}, {"role": "member"}] payload = json.dumps(key) headers = {"content-type": "application/json"} response = requests.post("http://127.0.0.1:818/api/sparts/ledger/auth", data=json.dumps(key), headers=headers) output = response.content.decode("utf-8").strip() statusinfo = json.loads(output) if statusinfo.get("status") and statusinfo.get("message"): status = statusinfo["status"] message = statusinfo["message"] if status == "success" and message == "authorized": b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) response = client.amend_category(category_id, category_name, description, private_key, public_key) return print_msg(response, "amend") else: return output else: return output
def do_list_category(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') auth_user, auth_password = _get_auth_info(args) client = CategoryBatch(base_url=url, keyfile=key_file) category_list = client.list_category(auth_user=auth_user, auth_password=auth_password) if category_list is not None: output = refine_output(str(category_list)) print(output) else: raise CategoryException("Could not retrieve category listing.")
def do_list_category(args, config): b_url = config.get('DEFAULT', 'url') client = CategoryBatch(base_url=b_url) category_list = client.list_category() if category_list is not None: if str(category_list) != "[]": result = refine_output(str(category_list)) output = ret_msg("success","OK","ListOf:CategoryRecord",result) else: output = ret_msg("success","OK","ListOf:CategoryRecord",str(category_list)) print (output) else: raise CategoryException("Could not retrieve category listing.")
def do_create_category(args, config): category_id = args.category_id category_name = args.category_name description = args.description url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') auth_user, auth_password = _get_auth_info(args) client = CategoryBatch(base_url=url, keyfile=key_file) response = client.create_category(category_id, category_name, description, auth_user=auth_user, auth_password=auth_password) print_msg(response)
def do_retrieve_category(args, config): category_id = args.category_id url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') auth_user, auth_password = _get_auth_info(args) client = CategoryBatch(base_url=url, keyfile=key_file) data = client.retreive_category(category_id, auth_user=auth_user, auth_password=auth_password) if data is not None: output = filter_output(str(data)) print(output) else: raise CategoryException("Category not found: {}".format(category_id))
def do_retrieve_category(args, config): """ Retrieves the state associating with the UUID in the Transaction Family : Category Args: args (ArgumentParser): ArgumentParser object containing required parameters config (ConfigParser): ConfigParser which contains the default url Returns: type: str String representing JSON object which allows the client to know that the call was either a success or a failure. Raises: CategoryException: * If failed to retrieve the uuid """ all_flag = args.all range_flag = args.range category_id = args.category_id if range_flag != None: all_flag = True b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) data = client.retreive_category(category_id, all_flag, range_flag) if data is not None: if all_flag == False: output = ret_msg("success", "OK", "CategoryRecord", data.decode()) else: output = ret_msg("success", "OK", "CategoryRecord", data) print(output) else: raise CategoryException("Category not found: {}".format(category_id))
def api_do_list_category(config): """ API version of "do_list_category" function. """ b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) category_list = client.list_category() if category_list is not None: category_list.sort(key=lambda x: x["timestamp"], reverse=True) result = json.dumps(category_list) output = ret_msg("success", "OK", "ListOf:CategoryRecord", result) return output else: return ret_msg( "failed", "CategoryException : Could not retrieve category listing.", "CategoryRecord", "{}")
def do_create_category(args, config): category_id = args.category_id category_name = args.category_name description = args.description private_key = args.private_key public_key = args.public_key # context = create_context('secp256k1') # # # private_key = context.new_random_private_key() # public_key = context.get_public_key(private_key) # # payload = "{}" key = json.loads(payload) key["publickey"] = public_key key["privatekey"] = private_key key["allowedrole"]=[{"role":"admin"},{"role":"member"}] payload = json.dumps(key) headers = {'content-type': 'application/json'} response = requests.post("http://127.0.0.1:818/api/sparts/ledger/auth",data=json.dumps(key),headers=headers) output = response.content.decode("utf-8").strip() statusinfo = json.loads(output) if statusinfo.get('status')and statusinfo.get('message'): status = statusinfo['status'] message = statusinfo['message'] if status == 'success' and message == 'authorized': b_url = config.get('DEFAULT', 'url') client = CategoryBatch(base_url=b_url) response = client.create_category(category_id,category_name,description,private_key,public_key) print_msg(response) else: print(output) else: print(output)
def do_retrieve_category(args, config): all_flag = args.all range_flag = args.range category_id = args.category_id if range_flag != None: all_flag = True b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) data = client.retreive_category(category_id, all_flag, range_flag) if data is not None: if all_flag == False: output = ret_msg("success", "OK", "CategoryRecord", data.decode()) else: output = ret_msg("success", "OK", "CategoryRecord", json.loads(data)) print(output) else: raise CategoryException("Category not found: {}".format(category_id))
def do_list_category(args, config): b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) category_list = client.list_category() if category_list is not None: if str(category_list) != "[]": result = ("[" + str(category_list)[3:-2] + "]").replace("b'", "")\ .replace("'", "") result = json.loads(result) result.sort(key=lambda x:x["timestamp"], reverse=True) result = json.dumps(result) output = ret_msg("success", "OK", "ListOf:CategoryRecord", result) else: output = ret_msg("success", "OK", "ListOf:CategoryRecord", str(category_list)) print (output) else: raise CategoryException("Could not retrieve category listing.")
def do_update_category(args, config): category_id = args.category_id category_name = args.category_name description = args.description private_key = args.private_key public_key = args.public_key payload = "{}" key = json.loads(payload) key["publickey"] = public_key key["privatekey"] = private_key key["allowedrole"]=[{"role":"admin"},{"role":"member"}] payload = json.dumps(key) headers = {"content-type": "application/json"} response = requests.post("http://127.0.0.1:818/api/sparts/ledger/auth", data=json.dumps(key), headers=headers) output = response.content.decode("utf-8").strip() statusinfo = json.loads(output) if statusinfo.get("status") and statusinfo.get("message"): status = statusinfo["status"] message = statusinfo["message"] if status == "success" and message == "authorized": b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) response = client.update_category(category_id, category_name, description, private_key, public_key) print_msg(response) else: print(output) else: print(output)
def api_do_retrieve_category(category_id, config, all_flag=False, range_flag=None): """ API version of "do_retrieve_category" function. """ if range_flag != None: all_flag = True b_url = config.get("DEFAULT", "url") client = CategoryBatch(base_url=b_url) data = client.retreive_category(category_id, all_flag, range_flag) if data is not None: if all_flag == False: output = ret_msg("success", "OK", "CategoryRecord", data.decode()) else: if range_flag == None: output = ret_msg("success", "OK", "CategoryRecord", data) else: if len(data) != 0: output = ret_msg("success", "OK", "CategoryRecord", json.dumps(data[0])) else: output = ret_msg("success", "OK", "CategoryRecord", "{}") return output else: return ret_msg( "failed", "CategoryException : UUID {} does not exist." \ .format(category_id), "CategoryRecord", "{}" )