-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsumo_api.py
67 lines (57 loc) · 2.09 KB
/
sumo_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import math
import requests
from requests import Response
class SumoAPI:
def __init__(self, url: str = "http://localhost:8080/sigmarest/resources"):
self.url: str = url
def init(self) -> bool:
try:
self._get('init')
except:
return False
return True
def reset(self) -> bool:
try:
self._get('reset')
except:
return False
return True
def tell(self, statement: str) -> bool:
try:
self._get('tell', {'statement': statement})
except:
return False
return True
def tell_all(self, statements: list[str]) -> bool:
success = True
for statement in statements:
success &= self.tell(statement)
return success
def ask(self, query: str, timeout: int =10) -> dict[str, str]:
resp = self._get('ask', {'query': query, 'timeout': timeout})
return resp.json()
def ask_max(self, query: str, timeout: int = 10, max_answers: int = 2) -> list[dict[str, str]]:
ret_hash = []
if max_answers < 0:
max_answers = math.inf
while len(ret_hash) < max_answers:
resp = None
resp = self._get('ask', {'query': query, 'timeout': timeout}).json()
if 'error' in resp:
break
bindings = resp['bindings']
ret_hash.append(bindings)
new_str = '(and %s' % query
for free_var in bindings:
new_str += ' (not (equal %s %s))' % (free_var, bindings[free_var])
new_str += ')'
query = new_str
new_str = ''
return ret_hash
def _get(self, endpoint: str, params: dict | None = None) -> Response:
if params != ():
return requests.get(f"{self.url}/{endpoint}", params=params)
return requests.get(f"{self.url}/{endpoint}")
def _post(self, endpoint: str, payload: dict | list) -> Response:
headers = {"Content-Type": "application/json"}
return requests.post(f"{self.url}/{endpoint}", json=payload, headers=headers)