forked from ljm625/srv6-vpp-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_api.py
53 lines (39 loc) · 1.44 KB
/
controller_api.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
import json
import aiohttp
class ControllerApiHelper(object):
def __init__(self,hostname,port):
self.hostname=hostname
self.port=port
pass
async def get(self,path):
def build_url():
return "http://{}:{}/api/v1/{}".format(self.hostname,self.port,path)
async with aiohttp.ClientSession() as session:
async with session.get(build_url()) as resp:
if resp.status>=300:
raise Exception("Error")
else:
result = await resp.json()
return result
async def post(self,path,data):
def build_url():
return "http://{}:{}/api/v1/{}".format(self.hostname,self.port,path)
async with aiohttp.ClientSession() as session:
async with session.post(build_url(), json=data) as resp:
if resp.status>=300:
raise Exception("Error")
else:
result = await resp.text()
return json.loads(result)["sid_list"]
async def get_device_list(self):
device_list = await self.get("devices")
return device_list
async def calculate_path(self,source,dest,method,extra={}):
payload ={
"source":source,
"dest":dest,
"method":method,
"extra":extra
}
result = await self.post("calculate",payload)
return result