-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
184 lines (159 loc) · 6.45 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import requests
import time
TOKEN = os.getenv("TOKEN") # Replace with your own token
BASE_URL = os.getenv("BASE_URL")
if not TOKEN:
raise ValueError("Missing required environment variable: TOKEN")
if not BASE_URL:
raise ValueError("Missing required environment variable: BASE_URL")
RETRY = 3
DELAY = 10
def hello(retry=RETRY):
try:
url = f"{BASE_URL}/"
response = requests.get(url)
return response.text
except Exception as e:
if retry > 0:
print(f"Failed to connect to server, retrying in {DELAY} seconds")
time.sleep(DELAY)
return hello(retry - 1)
else:
return f"Failed to connect to server: {str(e)}"
def check_auth(retry=RETRY):
try:
url = f"{BASE_URL}/check_auth"
headers = {'Content-Type': 'application/json', 'Authorization': TOKEN}
response = requests.get(url, headers=headers)
return response
except Exception as e:
if retry > 0:
print(f"Failed to authenticate, retrying in {DELAY} seconds")
time.sleep(DELAY)
return check_auth(retry - 1)
else:
return f"Failed to authenticate: {str(e)}"
def get_all_problems(cid, update=False, retry=RETRY):
"""
Retrieves all problems associated with a given contest ID from an API endpoint.
Args:
cid (int): The contest ID for which to retrieve problems.
update (bool, optional): Flag to indicate whether to update the problems.
Defaults to False.
retry (int, optional): Number of retry attempts if the request fails.
Defaults to RETRY constant.
Returns:
dict/str: If successful, returns a JSON response containing problem data.
If all retries fail, returns an error message string.
Example:
>>> problems = get_all_problems(2000)
>>> problems = get_all_problems(2001, update=True, retry=3)
"""
try:
url = f"{BASE_URL}/get_all_problems"
headers = {'Content-Type': 'application/json', 'Authorization': TOKEN}
params = {'cid': cid, 'update': update}
response = requests.get(url, headers=headers, params=params, timeout=180)
assert response.status_code == 200
return response.json()
except Exception as e:
if retry > 0:
print(f"Failed to get problems, retrying in {DELAY} seconds")
time.sleep(DELAY)
return get_all_problems(cid, update, retry - 1)
else:
return f"Failed to get problems: {str(e)}"
def get_problem(prob, retry=RETRY):
"""
Retrieves details of a specific problem from the API endpoint.
Args:
prob (str): The problem identifier to retrieve.
retry (int, optional): Number of retry attempts if the request fails.
Defaults to RETRY constant.
Returns:
dict/str: If successful, returns a JSON response containing problem details.
If all retries fail, returns an error message string.
Example:
>>> problem = get_problem("2000")
>>> problem = get_problem("2001", retry=3)
"""
try:
url = f"{BASE_URL}/get_problem"
headers = {'Content-Type': 'application/json', 'Authorization': TOKEN}
params = {'prob': prob}
response = requests.get(url, headers=headers, params=params, timeout=20)
assert response.status_code == 200
return response.json()
except Exception as e:
if retry > 0:
print(f"Failed to get problem, retrying in {DELAY} seconds")
time.sleep(DELAY)
return get_problem(prob, retry - 1)
else:
return f"Failed to get problem: {str(e)}"
def submit_code(prob, lang, code, tag="", retry=RETRY):
"""
Submits code for a specific problem to the API endpoint.
Args:
prob (str): The problem identifier to submit code for.
lang (str): The programming language id of the submitted code.
Lang id mapping shown in main.py.
code (str): The actual code to be submitted.
tag (str, optional): Additional tag for the submission. Defaults to empty string.
retry (int, optional): Number of retry attempts if the request fails.
Defaults to RETRY constant.
Returns:
dict/str: If successful, returns a JSON response containing submission details.
If all retries fail, returns an error message string.
Example:
>>> result = submit_code("2000A", 70, "print('Hello')")
>>> result = submit_code("2000A", 91, "int main() {}", "test", retry=3)
"""
try:
url = f"{BASE_URL}/submit_code"
headers = {'Content-Type': 'application/json', 'Authorization': TOKEN}
payload = {
'prob': prob,
'lang': lang,
'code': code,
'tag': tag
}
response = requests.post(url, json=payload, headers=headers, timeout=60)
assert response.status_code == 200
return response.json()
except Exception as e:
if retry > 0:
print(f"Failed to submit code, retrying in {DELAY} seconds")
time.sleep(DELAY)
return submit_code(prob, lang, code, tag, retry - 1)
else:
return f"Failed to submit code: {str(e)}"
def check_status(submission_id, retry=RETRY):
"""
Checks the status of a specific submission using the API endpoint.
Args:
submission_id (str): The ID of the submission to check.
retry (int, optional): Number of retry attempts if the request fails.
Defaults to RETRY constant.
Returns:
dict/str: If successful, returns a JSON response containing submission status.
If all retries fail, returns an error message string.
Example:
>>> status = check_status("12345")
>>> status = check_status("67890", retry=3)
"""
try:
url = f"{BASE_URL}/check_status"
headers = {'Content-Type': 'application/json', 'Authorization': TOKEN}
params = {'submission_id': submission_id}
response = requests.get(url, headers=headers, params=params, timeout=20)
assert response.status_code == 200
return response.json()
except Exception as e:
if retry > 0:
print(f"Failed to get problem, retrying in {DELAY} seconds")
time.sleep(DELAY)
return check_status(submission_id, retry - 1)
else:
return f"Failed to get problem: {str(e)}"