Skip to content

Jsonfilehandler

JsonFileHandler

Attributes

logger module-attribute

logger = getLogger(__name__)

Classes

JsonFileHandler

JsonFileHandler(file_name: str)

Handle supported.json file

Source code in dod/JsonFileHandler.py
def __init__(self, file_name : str):
    self.file_name = file_name
    self.data = dict() 
    self.endpoints = dict() # EndPoints
    self.endpoints_map= dict() #MAP
Attributes
data instance-attribute
data = dict()
endpoints instance-attribute
endpoints = dict()
endpoints_map instance-attribute
endpoints_map = dict()
file_name instance-attribute
file_name = file_name
Functions
add_endpoint
add_endpoint(endpoint: str, payload: str, args=None, comment=None)

Add endpoint to Json file

Source code in dod/JsonFileHandler.py
def add_endpoint(self, endpoint : str, payload : str, args = None, comment = None):
    """
        Add endpoint to Json file
    """
    if endpoint in self.endpoints_map.keys():
        logging.info("endpoint already exists")
        return 

    skel = { 
            "API": "", 
            "args": "",
            "payload" : "",
            "__comments__" : "",
            }

    skel["API"] = endpoint
    skel["args"] = args
    skel["payload"] = payload
    skel["__comments__"] = comment

    self.data["endpoints"].append(skel)

    f = open(self.file_name, "w")
    f.write(json.dumps(self.data, indent=4))
    f.close()

    self.reload_endpoints()
create_new_supported_file
create_new_supported_file()

Create New supported enpoints file, will not create if file already exists

Source code in dod/JsonFileHandler.py
def create_new_supported_file(self):
    """
        Create New supported enpoints file, will not create if file already
        exists
    """
    f = None
    try:
        f = open(self.file_name, "x")
    except:
        logging.info(f"{self.file_name} already exists")

    if f != None:
        self.data = {
                "header": {
                    "Time": "",
                    "Status": {
                        "Status": "",
                        "StatusCode": 200
                        },
                    "LastID": 1,
                    "ErrorCode": 0,
                    "ErrorMessage": "NA",
                    "Result": {}
                    },
                "endpoints": []
                }

        f.write(json.dumps(self.data, indent=4))
        f.close()
get_endpoint_data
get_endpoint_data(endpoint: str)
Source code in dod/JsonFileHandler.py
def get_endpoint_data(self, endpoint : str):
    logging.info(f"looking for {endpoint}")
    if endpoint not in self.endpoints_map.keys():
        logging.info(f"{endpoint} does not exist")
        return None
    else:
        logging.info(f"Found {endpoint}")
        return self.endpoints[self.endpoints_map[endpoint]]['payload']
reload_endpoints
reload_endpoints()
Source code in dod/JsonFileHandler.py
def reload_endpoints(self):
    file_fd = open(self.file_name, 'r')
    self.data = json.load(file_fd)
    self.endpoints = self.data["endpoints"]

    # map endpoint with index
    endpointIndex = 0
    for endpoint in self.endpoints:
        self.endpoints_map[endpoint["API"]] = endpointIndex
        endpointIndex = endpointIndex + 1

    file_fd.close()