-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
87 lines (73 loc) · 3.82 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
import json
class ProvideQApi:
def __init__(self, base_url, print_requests=False):
self.base_url = base_url
self.print_requests = print_requests
def get(self, endpoint):
if self.print_requests:
print(f"GET {self.base_url}{endpoint}")
response = requests.get(f"{self.base_url}{endpoint}")
if response.status_code == 200:
return response.json()
else:
print(f"Error {response.status_code}: {response.text}")
return None
def post(self, endpoint, data):
if self.print_requests:
print(f"POST {self.base_url}{endpoint}")
headers = {'Content-Type': 'application/json'}
response = requests.post(f"{self.base_url}{endpoint}", headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json()
else:
print(f"Error {response.status_code}: {response.text}")
return None
def patch(self, endpoint, data):
if self.print_requests:
print(f"PATCH {self.base_url}{endpoint}")
headers = {'Content-Type': 'application/json'}
response = requests.patch(f"{self.base_url}{endpoint}", headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json()
else:
print(f"Error {response.status_code}: {response.text}")
return None
def get_setting(self, settings_per_solver_id, solver_id):
setting = dict.get(settings_per_solver_id, solver_id)
if setting:
return setting()
return None
def create_problem(self, type, input, solverId, settings_per_solver_id):
data = {'input': input, 'solverId': solverId, 'solverSettings': self.get_setting(settings_per_solver_id, type)}
response = self.post(f"/problems/{type}", data)
if response:
return response['id']
return None
def solve(self, problem_type, input, initial_solver_id, solver_per_type, settings_per_solver_id):
starting_problem_id = self.create_problem(problem_type, input, initial_solver_id, settings_per_solver_id)
problems = [(starting_problem_id, problem_type)]
while True:
start_problem = self.get(f"/problems/{problem_type}/{starting_problem_id}")
if start_problem['state'] == "SOLVED":
print("Problem solved", start_problem)
break
for (problem_id, type) in problems:
problem = self.get(f"/problems/{type}/{problem_id}")
print(problem)
if problem['state'] == "NEEDS_CONFIGURATION":
solver_id = solver_per_type[problem['typeId']]()
print("Start and set solver for", type, problem_id, "to", solver_id)
self.patch(f"/problems/{type}/{problem['id']}", {"state": "SOLVING", "solverId": solver_id, "solverSettings": self.get_setting(settings_per_solver_id, solver_id)})
if problem['state'] == "READY_TO_SOLVE":
print("Start", type, problem_id)
self.patch(f"/problems/{type}/{problem['id']}", {"state": "SOLVING"})
if problem['state'] == "SOLVING":
if problem['subProblems']:
for subproblem in problem['subProblems']:
# add subproblem to the list of problems to process if it is not already there
sub_type = subproblem['subRoutine']['typeId']
for sub in subproblem['subProblemIds']:
if not any(p[0] == sub for p in problems):
print("Add new subproblem to list", sub_type, sub)
problems.append((sub, sub_type))