API json fixes
This commit is contained in:
+19
-4
@@ -130,8 +130,22 @@ get '/api/v1/nexttask/:type' => sub {
|
||||
|
||||
post '/api/v1/task/:task/complete' => sub {
|
||||
my $task = route_parameters->get("task");
|
||||
my $assign_key = body_parameters->get("assign_key");
|
||||
my $results = body_parameters->get("results");
|
||||
|
||||
# Accept both JSON body and form-encoded data for assign_key/results.
|
||||
my ($assign_key, $results);
|
||||
my $ct = request_header('Content-Type') || '';
|
||||
if ($ct eq 'application/json') {
|
||||
my $body = decode_json(request->body());
|
||||
$assign_key = $body->{assign_key};
|
||||
$results = $body->{results};
|
||||
} else {
|
||||
$assign_key = body_parameters->get("assign_key");
|
||||
$results = body_parameters->get("results");
|
||||
}
|
||||
|
||||
unless ($assign_key) {
|
||||
send_error("Missing assign_key", 400);
|
||||
}
|
||||
|
||||
# Verify the task is assigned to the worker
|
||||
my $sth = database->prepare("SELECT * FROM tasks WHERE id=? AND assign_key=? AND status='IN_PROGRESS'");
|
||||
@@ -143,9 +157,10 @@ post '/api/v1/task/:task/complete' => sub {
|
||||
send_error("Task not assigned to this worker or not in progress", 403);
|
||||
}
|
||||
|
||||
# Update the task as completed
|
||||
# Update the task as completed (store results as JSON text for MariaDB)
|
||||
my $results_json = defined $results ? encode_json($results) : 'null';
|
||||
my $update_sth = database->prepare("UPDATE tasks SET status='COMPLETED', results=?, updated_at=NOW() WHERE id=?");
|
||||
$update_sth->execute($results, $task);
|
||||
$update_sth->execute($results_json, $task);
|
||||
if($update_sth->rows == 0) {
|
||||
send_error("Failed to complete task", 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user