Skip to content

Commit a6311a3

Browse files
author
Martin Krulis
committed
A concept of a tool for resubmitting selected list of solutions added.
1 parent 9efeabf commit a6311a3

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

misc_api_utils/resubmit.php

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
/*
4+
* This is not a ready-to-use script. Just a concept, which may become basis for other script(s).
5+
*/
6+
7+
8+
class ReCodEx_API
9+
{
10+
private $base_url;
11+
private $jwt;
12+
13+
private $last_response = null;
14+
private $last_response_headers = null;
15+
private $last_info = null;
16+
private $last_errno = null;
17+
private $last_error = null;
18+
19+
public function __get($name)
20+
{
21+
if (isset($this->$name))
22+
return $this->$name;
23+
else
24+
return null;
25+
}
26+
27+
28+
public function __construct(string $base_url, string $jwt = '')
29+
{
30+
if (!$base_url)
31+
throw new Exception("Base URL cannot be empty.");
32+
if (substr($base_url, -1, 1) != '/') $base_url .= '/';
33+
$this->base_url = $base_url;
34+
35+
$this->set_jwt($jwt);
36+
}
37+
38+
public function set_jwt(string $jwt = '')
39+
{
40+
if (!preg_match('#^[-A-Za-z0-9+/=._]*$#', $jwt))
41+
throw new Exception("Given JWT token is not valid.");
42+
43+
$this->jwt = $jwt;
44+
}
45+
46+
47+
public function get_last_header($header)
48+
{
49+
$header = strtolower($header);
50+
}
51+
52+
private function process_response()
53+
{
54+
$this->last_response_headers = null;
55+
56+
if ($this->last_errno == 0 && $this->last_info && $this->last_info['header_size']) {
57+
$size = $this->last_info['header_size'];
58+
if (strlen($this->last_response) < $size)
59+
throw new Exception("Header size is expected to be larger than the whole size of the response.");
60+
61+
$this->last_response_headers = [];
62+
foreach (explode("\n", substr($this->last_response, 0, $size)) as $header) {
63+
$header = trim($header);
64+
$header = explode(':', $header, 2);
65+
if (!$header || count($header) != 2) continue;
66+
list($name, $value) = $header;
67+
$this->last_response_headers[strtolower(trim($name))] = trim($value);
68+
}
69+
70+
$this->last_response = substr($this->last_response, $size);
71+
if (strpos(strtolower($this->last_response_headers['content-type']), 'application/json') !== false)
72+
$this->last_response = json_decode($this->last_response);
73+
74+
}
75+
76+
if ($this->last_errno == 0 && $this->last_info && $this->last_info['http_code'] == 403) {
77+
// Unauthorized
78+
$this->jwt = null;
79+
}
80+
81+
if ($this->last_errno == 0 && $this->last_info && $this->last_info['http_code'] == 200)
82+
return $this->last_response;
83+
else
84+
return false;
85+
}
86+
87+
public function query(string $endpoint, string $method = 'GET', array $data = [], bool $jsonBody = false)
88+
{
89+
$ch = curl_init();
90+
curl_setopt($ch, CURLOPT_URL, $this->base_url . $endpoint);
91+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
92+
curl_setopt($ch, CURLOPT_HEADER, true);
93+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
94+
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);
95+
96+
$headers = [];
97+
98+
/**
99+
* Set selected method and possibly the body data.
100+
*/
101+
$method = strtoupper($method);
102+
switch ($method) {
103+
case 'DELETE':
104+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
105+
// fall througt to GET req ...
106+
case 'GET':
107+
if ($data)
108+
throw new Exception("$method requests does not have any additional data.");
109+
break;
110+
111+
case 'POST':
112+
if ($jsonBody)
113+
throw new Exception("JSON body not implemented yet.");
114+
curl_setopt($ch, CURLOPT_POST, 1);
115+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
116+
$headers[] = "Content-Type: multipart/form-data";
117+
break;
118+
}
119+
120+
/**
121+
* Include JWT security token, if provided.
122+
*/
123+
if ($this->jwt) {
124+
$headers[] = "Authorization: Bearer $this->jwt";
125+
}
126+
127+
128+
/**
129+
* Run request, collect results ...
130+
*/
131+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
132+
133+
$this->last_response = curl_exec($ch);
134+
$this->last_errno = curl_errno($ch);
135+
$this->last_error = curl_error($ch);
136+
$this->last_info = curl_getinfo($ch);
137+
138+
// echo curl_getinfo($ch, CURLINFO_HEADER_OUT), "\n\n";
139+
140+
curl_close($ch);
141+
142+
return $this->process_response();
143+
}
144+
145+
146+
public function resubmit_assignment_solution($id, $debug = false)
147+
{
148+
return $this->query("assignment-solutions/$id/resubmit", "POST", [
149+
'debug' => (bool)$debug,
150+
'private' => false,
151+
]);
152+
}
153+
}
154+
155+
156+
exit; // prevent accidental usage
157+
158+
159+
$api = new ReCodEx_API("https://recodex.mff.cuni.cz/api/v1/","placeyourJWThere");
160+
161+
162+
// example of utilization
163+
$counter = 0;
164+
foreach (file('./solutions.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $id) {
165+
$id = trim($id);
166+
if (!preg_match('/^[-0-9a-fA-F]{36}$/', $id)) continue;
167+
168+
++$counter;
169+
echo "Resubmitting #$counter: $id ... ";
170+
$res = $api->resubmit_assignment_solution($id);
171+
$code = (int)$api->last_info['http_code'];
172+
echo $code, "\n";
173+
174+
if ($code != 200 || empty($res->success)) {
175+
if ($code == 404) continue;
176+
var_dump($res);
177+
var_dump($api);
178+
exit(1);
179+
}
180+
}

0 commit comments

Comments
 (0)