7 use Drupal\Core\Site\Settings;
17 'datastoreDistributionUuid',
18 'datastoreQueryProperties',
19 'datastoreQueryConditions',
20 'datastoreQueryLimit',
21 'datastoreQueryOffset',
22 'datastoreQuerySorts',
23 'datastoreQueryCount',
24 'datastoreQueryResults',
25 'datastoreQuerySchema',
27 'datastoreQueryFormat',
28 'datastoreQueryRowIds',
29 'datastoreDatasetUuid',
30 'datastoreDistributionIndex',
36 'datastoreResourceQuery',
37 'datastoreQueryResource',
38 'datastoreQueryProperty',
39 'datastoreQueryExpression',
40 'datastoreQueryCondition',
41 'datastoreQueryConditionGroup',
43 'datastoreQueryResourceProperty',
50 '200JsonOrCsvQueryOk',
59 private $docsGenerator;
73 private Settings $settings;
86 $this->docsGenerator = $docsGenerator;
87 $this->metastore = $metastore;
88 $this->settings = $settings;
101 $specs = [
'metastore_api_docs',
'datastore_api_docs'];
102 $fullSpec = $this->docsGenerator->buildSpec($specs)->{
"$"};
105 'openapi' => $fullSpec[
'openapi'],
106 'info' => $fullSpec[
'info'],
109 $metastorePath = $fullSpec[
'paths'][
'/api/1/metastore/schemas/dataset/items/{identifier}'][
'get'];
110 unset($metastorePath[
'parameters'][0]);
111 $metastorePath[
'parameters'] = array_values($metastorePath[
'parameters']);
112 $datasetSpec[
'paths'][
"/api/1/metastore/schemas/dataset/items/$identifier"][
'get'] = $metastorePath;
114 $datasetSpec[
'paths'][
"/api/1/datastore/query/$identifier/{index}"]
115 = $this->getDatastoreIndexPath($fullSpec, $identifier);
117 $datasetSpec[
'paths'][
'/api/1/datastore/query/{distributionId}'] =
118 $fullSpec[
'paths'][
'/api/1/datastore/query/{distributionId}'];
120 $datasetSpec[
'paths'][
'/api/1/datastore/sql'] =
121 $fullSpec[
'paths'][
'/api/1/datastore/sql'];
123 $datasetSpec[
'components'] = $this->datasetSpecificComponents($fullSpec, $identifier);
125 $this->alterDatastoreParameters($datasetSpec, $identifier);
126 $this->modifySqlEndpoints($datasetSpec, $identifier);
127 if ($dkanApiBase = $this->settings->get(
'dkan_api_base')) {
145 private function datasetSpecificComponents($fullSpec, $identifier) {
147 $components[
'parameters'] =
148 $this->datasetSpecificParameters($fullSpec[
'components'][
'parameters'], $identifier);
149 $components[
'schemas'] =
150 $this->datasetSpecificSchemas($fullSpec[
'components'][
'schemas']);
151 $components[
'responses'] =
152 $this->datasetSpecificResponses($fullSpec[
'components'][
'responses']);
168 private function getDatastoreIndexPath($fullSpec, $identifier) {
169 $datastoreIndexPath = $fullSpec[
'paths'][
'/api/1/datastore/query/{datasetId}/{index}'];
170 unset($datastoreIndexPath[
'get'][
'parameters'][0]);
171 $datastoreIndexPath[
'get'][
'parameters'] = array_values($datastoreIndexPath[
'get'][
'parameters']);
172 unset($datastoreIndexPath[
'post'][
'parameters'][0]);
173 $datastoreIndexPath[
'post'][
'parameters'] = array_values($datastoreIndexPath[
'post'][
'parameters']);
174 return $datastoreIndexPath;
186 private function datasetSpecificSchemas(array $schemas) {
187 $newSchemas = array_filter($schemas,
function ($key) {
188 if (in_array($key, self::SPEC_SCHEMAS)) {
192 }, ARRAY_FILTER_USE_KEY);
207 private function datasetSpecificParameters(array $parameters, $identifier) {
208 $newParameters = array_filter($parameters,
function ($key) {
209 if (in_array($key, self::SPEC_PARAMETERS)) {
213 }, ARRAY_FILTER_USE_KEY);
214 $newParameters[
'datasetUuid'][
'example'] = $identifier;
215 return $newParameters;
227 private function datasetSpecificResponses(array $responses) {
228 $newResponses = array_filter($responses,
function ($key) {
229 if (in_array($key, self::SPEC_RESPONSES)) {
233 }, ARRAY_FILTER_USE_KEY);
234 return $newResponses;
245 private function alterDatastoreParameters(array &$spec,
string $identifier) {
246 $spec[
'components'][
'parameters'][
'datastoreDatasetUuid'][
'example'] = $identifier;
247 foreach ($this->getDistributions($identifier) as $index => $dist) {
248 unset($spec[
'components'][
'parameters'][
'datastoreDistributionUuid'][
'example']);
249 $spec[
'components'][
'parameters'][
'datastoreDistributionUuid'][
'examples'][$dist[
'identifier']] = [
250 'value' => $dist[
'identifier'],
251 'summary' => $dist[
"data"][
"title"] ?? $dist[
'identifier'],
253 unset($spec[
'components'][
'parameters'][
'datastoreDistributionIndex'][
'example']);
254 $spec[
'components'][
'parameters'][
'datastoreDistributionIndex'][
'examples'][
"index{$index}"] = [
256 'summary' => $dist[
"data"][
"title"] ?? $dist[
'identifier'],
269 private function modifySqlEndpoints(array &$spec,
string $identifier) {
271 foreach ($this->getSqlPathsAndOperations($spec[
'paths']) as $path => $operations) {
272 foreach ($this->getDistributions($identifier) as $dist) {
273 $newOperations = $this->modifySqlEndpoint($operations, $dist);
274 $spec[
'paths'][$path] = $newOperations;
282 private function getSqlPathsAndOperations($pathsAndOperations) {
283 foreach (array_keys($pathsAndOperations) as $path) {
284 if (substr_count($path,
'sql') == 0) {
285 unset($pathsAndOperations[$path]);
288 return $pathsAndOperations;
294 private function modifySqlEndpoint($operations, $distribution) {
295 $distKey = isset($distribution[
'data'][
'title']) ? $distribution[
'data'][
'title'] : $distribution[
'identifier'];
296 unset($operations[
'get'][
'parameters'][0][
'example']);
297 $operations[
'get'][
'parameters'][0][
'examples'][$distKey] = [
298 "summary" =>
"Query distribution {$distribution['identifier']}",
299 "value" =>
"[SELECT * FROM {$distribution['identifier']}][LIMIT 2]",
313 private function getDistributions(
string $identifier) {
315 $data = $this->metastore->swapReferences($this->metastore->get(
"dataset", $identifier));
317 return $data->{
"$.distribution"} ?? [];