Skip to content

JsonFileHandler

JsonFileHandler

Handle supported.json file

Source code in dod/JsonFileHandler.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class JsonFileHandler:
    """
        Handle supported.json file
    """

    def __init__(self, file_name : str):
        self.file_name = file_name
        self.data = dict() 
        self.endpoints = dict() # EndPoints
        self.endpoints_map= dict() #MAP

    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()


    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()

    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']

    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()

add_endpoint(endpoint, payload, args=None, comment=None)

Add endpoint to Json file

Source code in dod/JsonFileHandler.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 enpoints file, will not create if file already exists

Source code in dod/JsonFileHandler.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()