-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.py
87 lines (69 loc) · 2.11 KB
/
catalog.py
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
76
77
78
79
80
81
82
83
84
85
86
87
import json
import cherrypy
import sys
sys.path.insert(0, "../")
class serviceCatalogConfiguration(object):
def __init__(self, fileName="catalog.json"):
self.devices = None
self.services = None
with open(fileName, "r") as jsonfile:
self.content = json.load(jsonfile)
self.devices = self.content["devices"]
self.services = self.content["services"]
def get_device(self, dev_id):
for dev in self.devices:
print(dev)
if dev["dev_id"] == dev_id:
return dev
return None
def add_device(self, dev_id):
pass
def get_services(self):
return self.services
@cherrypy.expose
class scWebservice(object):
def __init__(self):
self.sc = serviceCatalogConfiguration()
pass
def GET(self, *uri, **params):
'''
- GET: catalog/device/<device_id>
- GET: catalog/services
'''
res = None
try:
if uri[0] == 'device' and len(uri) == 2:
# Find device by type and id
device_id = str(uri[1])
res = self.sc.get_device(device_id)
elif uri[0] == 'services':
res = self.sc.get_services()
else:
res = None
except Exception as e:
res = str(e)
if res:
return json.dumps(res)
else:
return self._responsJson('Bad Request', False)
def POST(self, *uri, **params):
'''
- catalog/device/<device_id>
- catalog/services
'''
return True
def _responsJson(self, result, success):
tempJson = {'result': result, 'success': success}
return json.dumps(tempJson)
if __name__ == '__main__':
conf = {
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.sessions.on': True
}
}
cherrypy.tree.mount(scWebservice(), '/catalog', conf)
cherrypy.server.socket_host = "0.0.0.0"
cherrypy.server.socket_port = 8181
cherrypy.engine.start()
cherrypy.engine.block()