-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "REST: Add a new GetPropertyLabelWithFallback route"
- Loading branch information
Showing
18 changed files
with
623 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
repo/rest-api/specs/resources/labels/label-with-fallback-for-property.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"get": { | ||
"operationId": "getPropertyLabelWithFallback", | ||
"tags": [ "labels" ], | ||
"summary": "[WIP] Retrieve a Property's label in a specific language, with language fallback", | ||
"description": "This endpoint is currently in development and is not recommended for production use", | ||
"parameters": [ | ||
{ "$ref": "../../global/parameters.json#/PropertyId" }, | ||
{ "$ref": "../../global/parameters.json#/LanguageCode" }, | ||
{ "$ref": "../../global/parameters.json#/IfNoneMatch" }, | ||
{ "$ref": "../../global/parameters.json#/IfModifiedSince" }, | ||
{ "$ref": "../../global/parameters.json#/IfMatch" }, | ||
{ "$ref": "../../global/parameters.json#/IfUnmodifiedSince" }, | ||
{ "$ref": "../../global/parameters.json#/Authorization" } | ||
], | ||
"responses": { | ||
"200": { "$ref": "../../global/responses.json#/Label" }, | ||
"304": { "$ref": "../../global/responses.json#/NotModified" }, | ||
"400": { "$ref": "../../global/responses.json#/InvalidTermByLanguageInput" }, | ||
"404": { "$ref": "../../global/responses.json#/ResourceNotFound" }, | ||
"412": { "$ref": "../../global/responses.json#/PreconditionFailedError" }, | ||
"500": { "$ref": "../../global/responses.json#/UnexpectedError" } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...UseCases/GetPropertyLabelWithFallback/DeserializedGetPropertyLabelWithFallbackRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedLanguageCodeRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedPropertyIdRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedGetPropertyLabelWithFallbackRequest extends DeserializedPropertyIdRequest, DeserializedLanguageCodeRequest { | ||
} |
46 changes: 46 additions & 0 deletions
46
...pi/src/Application/UseCases/GetPropertyLabelWithFallback/GetPropertyLabelWithFallback.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestPropertyRevisionMetadata; | ||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
use Wikibase\Repo\RestApi\Domain\Services\PropertyLabelRetriever; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyLabelWithFallback { | ||
|
||
private PropertyLabelRetriever $labelRetriever; | ||
private GetLatestPropertyRevisionMetadata $getRevisionMetadata; | ||
private GetPropertyLabelWithFallbackValidator $validator; | ||
|
||
public function __construct( | ||
GetPropertyLabelWithFallbackValidator $validator, | ||
GetLatestPropertyRevisionMetadata $getRevisionMetadata, | ||
PropertyLabelRetriever $labelRetriever | ||
) { | ||
$this->labelRetriever = $labelRetriever; | ||
$this->getRevisionMetadata = $getRevisionMetadata; | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
public function execute( GetPropertyLabelWithFallbackRequest $request ): GetPropertyLabelWithFallbackResponse { | ||
$deserializedRequest = $this->validator->validateAndDeserialize( $request ); | ||
$propertyId = $deserializedRequest->getPropertyId(); | ||
$languageCode = $deserializedRequest->getLanguageCode(); | ||
|
||
[ $revisionId, $lastModified ] = $this->getRevisionMetadata->execute( $propertyId ); | ||
|
||
$label = $this->labelRetriever->getLabel( $propertyId, $languageCode ); | ||
if ( !$label ) { | ||
throw UseCaseError::newResourceNotFound( 'label' ); | ||
} | ||
|
||
return new GetPropertyLabelWithFallbackResponse( $label, $lastModified, $revisionId ); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...Application/UseCases/GetPropertyLabelWithFallback/GetPropertyLabelWithFallbackRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\LabelLanguageCodeRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyIdRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\UseCaseRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyLabelWithFallbackRequest implements UseCaseRequest, PropertyIdRequest, LabelLanguageCodeRequest { | ||
|
||
private string $propertyId; | ||
private string $languageCode; | ||
|
||
public function __construct( string $propertyId, string $languageCode ) { | ||
$this->propertyId = $propertyId; | ||
$this->languageCode = $languageCode; | ||
} | ||
|
||
public function getPropertyId(): string { | ||
return $this->propertyId; | ||
} | ||
|
||
public function getLanguageCode(): string { | ||
return $this->languageCode; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...pplication/UseCases/GetPropertyLabelWithFallback/GetPropertyLabelWithFallbackResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Domain\ReadModel\Label; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyLabelWithFallbackResponse { | ||
|
||
private Label $label; | ||
private string $lastModified; | ||
private int $revisionId; | ||
|
||
public function __construct( Label $label, string $lastModified, int $revisionId ) { | ||
$this->label = $label; | ||
$this->lastModified = $lastModified; | ||
$this->revisionId = $revisionId; | ||
} | ||
|
||
public function getLabel(): Label { | ||
return $this->label; | ||
} | ||
|
||
public function getLastModified(): string { | ||
return $this->lastModified; | ||
} | ||
|
||
public function getRevisionId(): int { | ||
return $this->revisionId; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...plication/UseCases/GetPropertyLabelWithFallback/GetPropertyLabelWithFallbackValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface GetPropertyLabelWithFallbackValidator { | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
public function validateAndDeserialize( GetPropertyLabelWithFallbackRequest $request ): DeserializedGetPropertyLabelWithFallbackRequest; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
repo/rest-api/src/RouteHandlers/GetPropertyLabelWithFallbackRouteHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\RouteHandlers; | ||
|
||
use MediaWiki\Rest\RequestInterface; | ||
use MediaWiki\Rest\Response; | ||
use MediaWiki\Rest\ResponseInterface; | ||
use MediaWiki\Rest\SimpleHandler; | ||
use MediaWiki\Rest\StringStream; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback\GetPropertyLabelWithFallback; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback\GetPropertyLabelWithFallbackRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabelWithFallback\GetPropertyLabelWithFallbackResponse; | ||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\AuthenticationMiddleware; | ||
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\MiddlewareHandler; | ||
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\UserAgentCheckMiddleware; | ||
use Wikibase\Repo\RestApi\WbRestApi; | ||
use Wikimedia\ParamValidator\ParamValidator; | ||
use function json_encode; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyLabelWithFallbackRouteHandler extends SimpleHandler { | ||
|
||
private const PROPERTY_ID_PATH_PARAM = 'property_id'; | ||
private const LANGUAGE_CODE_PATH_PARAM = 'language_code'; | ||
|
||
private GetPropertyLabelWithFallback $useCase; | ||
private MiddlewareHandler $middlewareHandler; | ||
private ResponseFactory $responseFactory; | ||
|
||
public function __construct( | ||
GetPropertyLabelWithFallback $useCase, | ||
MiddlewareHandler $middlewareHandler, | ||
ResponseFactory $responseFactory | ||
) { | ||
$this->useCase = $useCase; | ||
$this->middlewareHandler = $middlewareHandler; | ||
$this->responseFactory = $responseFactory; | ||
} | ||
|
||
public static function factory(): self { | ||
$responseFactory = new ResponseFactory(); | ||
return new self( | ||
WbRestApi::getGetPropertyLabelWithFallback(), | ||
new MiddlewareHandler( [ | ||
WbRestApi::getUnexpectedErrorHandlerMiddleware(), | ||
new UserAgentCheckMiddleware(), | ||
new AuthenticationMiddleware(), | ||
WbRestApi::getPreconditionMiddlewareFactory()->newPreconditionMiddleware( | ||
fn( RequestInterface $request ): string => $request->getPathParam( self::PROPERTY_ID_PATH_PARAM ) | ||
), | ||
] ), | ||
$responseFactory | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed ...$args | ||
*/ | ||
public function run( ...$args ): Response { | ||
return $this->middlewareHandler->run( $this, [ $this, 'runUseCase' ], $args ); | ||
} | ||
|
||
public function runUseCase( string $propertyId, string $languageCode ): Response { | ||
try { | ||
$useCaseResponse = $this->useCase->execute( new GetPropertyLabelWithFallbackRequest( $propertyId, $languageCode ) ); | ||
return $this->newSuccessResponse( $useCaseResponse ); | ||
} catch ( UseCaseError $e ) { | ||
return $this->responseFactory->newErrorResponseFromException( $e ); | ||
} | ||
} | ||
|
||
public function getParamSettings(): array { | ||
return [ | ||
self::PROPERTY_ID_PATH_PARAM => [ | ||
self::PARAM_SOURCE => 'path', | ||
ParamValidator::PARAM_TYPE => 'string', | ||
ParamValidator::PARAM_REQUIRED => true, | ||
], | ||
self::LANGUAGE_CODE_PATH_PARAM => [ | ||
self::PARAM_SOURCE => 'path', | ||
ParamValidator::PARAM_TYPE => 'string', | ||
ParamValidator::PARAM_REQUIRED => true, | ||
], | ||
]; | ||
} | ||
|
||
private function newSuccessResponse( GetPropertyLabelWithFallbackResponse $useCaseResponse ): Response { | ||
$httpResponse = $this->getResponseFactory()->create(); | ||
$httpResponse->setHeader( 'Content-Type', 'application/json' ); | ||
$httpResponse->setHeader( 'Last-Modified', wfTimestamp( TS_RFC2822, $useCaseResponse->getLastModified() ) ); | ||
$httpResponse->setHeader( 'ETag', "\"{$useCaseResponse->getRevisionId()}\"" ); | ||
$httpResponse->setBody( | ||
new StringStream( json_encode( $useCaseResponse->getLabel()->getText() ) ) | ||
); | ||
|
||
return $httpResponse; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function needsWriteAccess(): bool { | ||
return false; | ||
} | ||
|
||
/** | ||
* Preconditions are checked via {@link PreconditionMiddleware} | ||
*/ | ||
public function checkPreconditions(): ?ResponseInterface { | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.