Skip to content

Commit

Permalink
Merge pull request #983 from NatLibFi/issue952-global-rest-label
Browse files Browse the repository at this point in the history
Global REST label method
  • Loading branch information
joelit authored May 8, 2020
2 parents 610131f + 8459c37 commit 2f28514
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
10 changes: 9 additions & 1 deletion controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,15 @@ public function label($request)
return null;
}

$labelResults = $request->getVocab()->getAllConceptLabels($request->getUri(), $request->getLang());
$vocab = $request->getVocab();
if ($vocab === null) {
$vocab = $this->model->guessVocabularyFromUri($request->getUri());
}
if ($vocab === null) {
return $this->returnError('404', 'Not Found', "Could not find concept <{$request->getUri()}>");
}

$labelResults = $vocab->getAllConceptLabels($request->getUri(), $request->getLang());
if ($labelResults === null) {
return $this->returnError('404', 'Not Found', "Could not find concept <{$request->getUri()}>");
}
Expand Down
2 changes: 2 additions & 0 deletions rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
$controller->vocabularies($request);
} elseif ($parts[1] == 'search') {
$controller->search($request);
} elseif ($parts[1] == 'label') {
$controller->label($request);
} elseif ($parts[1] == 'types') {
$controller->types($request);
} elseif ($parts[1] == 'data') {
Expand Down
47 changes: 44 additions & 3 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,47 @@
]
}
},
"/label": {
"get": {
"summary": "List of labels for the requested concept",
"parameters": [
{
"name": "uri",
"in": "query",
"description": "URI of the concept whose labels to return",
"required": true,
"type": "string"
},
{
"name": "lang",
"in": "query",
"description": "search language, e.g. \"en\" or \"fi\"",
"required": false,
"type": "string"
}
],
"produces": [
"application/ld+json"
],
"responses": {
"200": {
"description": "labels for the requested concept",
"schema": {
"$ref": "#/definitions/LabelsAndUri"
}
},
"304": {
"description": "the resource was not modified, so there is no need to retransmit the requested resources"
},
"404": {
"description": "no concept could be found with the requested URI"
}
},
"tags": [
"Global methods"
]
}
},
"/data": {
"get": {
"summary": "RDF data of the requested concept",
Expand Down Expand Up @@ -717,7 +758,7 @@
{
"name": "uri",
"in": "query",
"description": "URI of the concept whose label to return",
"description": "URI of the concept whose labels to return",
"required": true,
"type": "string"
},
Expand All @@ -734,7 +775,7 @@
],
"responses": {
"200": {
"description": "preferred label for the requested concept",
"description": "labels for the requested concept",
"schema": {
"$ref": "#/definitions/LabelsAndUri"
}
Expand Down Expand Up @@ -2232,4 +2273,4 @@
]
}
}
}
}
48 changes: 48 additions & 0 deletions tests/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,54 @@ public function testLabelOnePrefOneAltLabel() {
$this->assertJsonStringEqualsJsonString($expected, $out);
}

/**
* @covers RestController::label
*/
public function testLabelGlobal() {
$request = new Request($this->model);
$request->setQueryParam('format', 'application/json');
$request->setURI('http://www.skosmos.skos/test/ta112');
$request->setLang('en');

$this->controller->label($request);
$out = $this->getActualOutput();

$expected = <<<EOD
{"@context": {
"skos": "http://www.w3.org/2004/02/skos/core#",
"uri": "@id",
"type": "@type",
"prefLabel": "skos:prefLabel",
"altLabel": "skos:altLabel",
"hiddenLabel": "skos:hiddenLabel",
"@language": "en"
},
"uri": "http://www.skosmos.skos/test/ta112",
"prefLabel": "Carp",
"altLabel": [
"Golden crucian"
]
}
EOD;
$this->assertJsonStringEqualsJsonString($expected, $out);
}

/**
* @covers RestController::label
*/
public function testLabelGlobalNonexistentVocab() {
$request = new Request($this->model);
$request->setQueryParam('format', 'application/json');
$request->setURI('http://www.skosmos.skos/nonexistent/vocab');
$request->setLang('en');

$this->controller->label($request);
$out = $this->getActualOutput();

$expected = "404 Not Found : Could not find concept <http://www.skosmos.skos/nonexistent/vocab>";
$this->assertEquals($expected, $out);
}

/**
* @covers RestController::label
*/
Expand Down

0 comments on commit 2f28514

Please sign in to comment.