Commit a90bf161 authored by Victor Emanouilov's avatar Victor Emanouilov
Browse files

[ENH] tabular API - format update endpoint

parent 1407b98d
Pipeline #602467773 passed with stages
in 8 minutes and 29 seconds
......@@ -41,7 +41,13 @@ class Services_ApiClient
throw new Services_Exception(tr('Remote service inaccessible (%0), error: "%1"', $response->getStatusCode(), $error), 400);
}
return json_decode($response->getBody(), true);
$parsed = json_decode($response->getBody(), true);
if (json_last_error() != JSON_ERROR_NONE) {
throw new Services_Exception(tr('Remote service responded with invalid JSON: %0', $response->getBody()));
}
return $parsed;
}
public function getResultLoader($endpoint, $arguments = [], $offsetKey = 'offset', $maxRecordsKey = 'maxRecords', $resultKey = 'result', $perPage = 20)
......
......@@ -49,7 +49,8 @@ class APIWriter
$url = str_replace('#id', $id, $this->config['update_url']);
$client = new \Services_ApiClient($url, false);
$method = strtolower($this->config['update_method'] ?? 'patch');
$result = $client->$method('', $row);
$formatted_row = $this->formatRow(@$this->config['update_format'], $columns, $row);
$result = $client->$method('', $formatted_row);
} else {
$skipped++;
continue;
......@@ -59,14 +60,7 @@ class APIWriter
$url = $this->config['create_url'];
$client = new \Services_ApiClient($url, false);
$method = strtolower($this->config['create_method'] ?? 'post');
if (! empty($this->config['create_format'])) {
$formatted_row = $this->config['create_format'];
foreach ($columns as $column) {
$formatted_row = str_replace('%' . $column->getLabel() . '%', $row[$column->getLabel()], $formatted_row);
}
} else {
$formatted_row = $row;
}
$formatted_row = $this->formatRow(@$this->config['create_format'], $columns, $row);
$result = $client->$method('', $formatted_row);
} else {
$skipped++;
......@@ -100,4 +94,17 @@ class APIWriter
}
return compact('succeeded', 'failed', 'skipped');
}
private function formatRow($format, $columns, $row)
{
if (! empty($format)) {
$formatted_row = $format;
foreach ($columns as $column) {
$formatted_row = str_replace('%' . $column->getLabel() . '%', $row[$column->getLabel()], $formatted_row);
}
} else {
$formatted_row = $row;
}
return $formatted_row;
}
}
......@@ -193,6 +193,17 @@
</select>
</div>
</div>
<div class="mb-3 row">
<label class="col-form-label col-sm-2 offset-sm-1">
{tr}Update endpoint format{/tr}
<a class="tikihelp text-info" title="{tr}Update format{/tr}: {tr}Use special formatting when sending data to update endpoint if tabular's data format is not sufficient. E.g. [&quot;%field1%&quot;, &quot;%field3%&quot;]{/tr}">
{icon name=information}
</a>
</label>
<div class="col-sm-9">
<input class="form-control" type="text" name="api[update_format]" value="{$api_config.update_format|escape}">
</div>
</div>
<div class="mb-3 row">
<label class="col-form-label col-sm-2 offset-sm-1">
{tr}Modify data path{/tr}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment