-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPRouting.py
More file actions
78 lines (59 loc) · 2.49 KB
/
HTTPRouting.py
File metadata and controls
78 lines (59 loc) · 2.49 KB
1
2
3
4
5
6
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
class RouteTrieNode:
def __init__(self, handler = None):
self.handler = handler
self.children = {}
def insertNode(self, path):
self.children[path] = RouteTrieNode()
class Trie:
def __init__(self):
self.root = RouteTrieNode()
def insert(self, pathArray, handler):
currentNode = self.root
for path in pathArray:
if path not in currentNode.children.keys():
currentNode.insertNode(path)
currentNode = currentNode.children[path]
currentNode.handler = handler
def find(self, pathArray):
currentNode = self.root
for path in pathArray:
if path not in currentNode.children.keys():
return None
else:
currentNode = currentNode.children[path]
return currentNode.handler
class Router:
def __init__(self, handler):
self.trie = Trie()
self.trie.root.handler = handler
def addHandler(self, url, handler):
if url == "" or handler == "" :
print("Error! Invalid URL / Handler")
return
pathArray = url.split("/")
if pathArray[-1] == "":
pathArray.pop()
self.trie.insert(pathArray,handler)
def lookup(self, url):
if url == "/":
return self.trie.root.handler
pathArray = url.split("/")
if pathArray[-1] == "":
pathArray.pop()
return self.trie.find(pathArray)
# Here are some test cases and expected outputs you can use to test your implementation
# create the router and add a route
router = Router("root handler") # remove the 'not found handler' if you did not implement this
router.addHandler("/home/about", "about handler") # add a route
# some lookups with the expected output
print(router.lookup("/")) # should print 'root handler'
print(router.lookup("/home")) # should print 'not found handler' or None if you did not implement one
print(router.lookup("/home/about")) # should print 'about handler'
print(router.lookup("/home/about/")) # should print 'about handler' or None if you did not handle trailing slashes
print(router.lookup("/home/about/me")) # should print 'not found handler' or None if you did not implement one
print()
# EDGE CASES
router.addHandler("", "") # expect error message
print(router.lookup("")) # expect 'root handler'
router.addHandler("/home/about", "new about handler") # change handler for url
print(router.lookup("/home/about")) # expect 'new about handler'