-
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: Create GetPropertyDescriptionWithFallback route"
- Loading branch information
Showing
18 changed files
with
617 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
repo/rest-api/specs/resources/descriptions/description-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": "getPropertyDescriptionWithFallback", | ||
"tags": [ "descriptions" ], | ||
"summary": "[WIP] Retrieve a Property's description 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#/Description" }, | ||
"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
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
...PropertyDescriptionWithFallback/DeserializedGetPropertyDescriptionWithFallbackRequest.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\GetPropertyDescriptionWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedLanguageCodeRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedPropertyIdRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedGetPropertyDescriptionWithFallbackRequest extends DeserializedPropertyIdRequest, DeserializedLanguageCodeRequest { | ||
} |
45 changes: 45 additions & 0 deletions
45
...cation/UseCases/GetPropertyDescriptionWithFallback/GetPropertyDescriptionWithFallback.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,45 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyDescriptionWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestPropertyRevisionMetadata; | ||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
use Wikibase\Repo\RestApi\Domain\Services\PropertyDescriptionRetriever; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyDescriptionWithFallback { | ||
|
||
private GetPropertyDescriptionWithFallbackValidator $validator; | ||
private GetLatestPropertyRevisionMetadata $getRevisionMetadata; | ||
private PropertyDescriptionRetriever $descriptionRetriever; | ||
|
||
public function __construct( | ||
GetPropertyDescriptionWithFallbackValidator $validator, | ||
GetLatestPropertyRevisionMetadata $getRevisionMetadata, | ||
PropertyDescriptionRetriever $descriptionRetriever | ||
) { | ||
$this->validator = $validator; | ||
$this->getRevisionMetadata = $getRevisionMetadata; | ||
$this->descriptionRetriever = $descriptionRetriever; | ||
} | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
public function execute( GetPropertyDescriptionWithFallbackRequest $request ): GetPropertyDescriptionWithFallbackResponse { | ||
$deserializedRequest = $this->validator->validateAndDeserialize( $request ); | ||
$propertyId = $deserializedRequest->getPropertyId(); | ||
$languageCode = $deserializedRequest->getLanguageCode(); | ||
|
||
[ $revisionId, $lastModified ] = $this->getRevisionMetadata->execute( $propertyId ); | ||
|
||
$description = $this->descriptionRetriever->getDescription( $propertyId, $languageCode ); | ||
if ( !$description ) { | ||
throw UseCaseError::newResourceNotFound( 'description' ); | ||
} | ||
|
||
return new GetPropertyDescriptionWithFallbackResponse( $description, $lastModified, $revisionId ); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...UseCases/GetPropertyDescriptionWithFallback/GetPropertyDescriptionWithFallbackRequest.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\GetPropertyDescriptionWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DescriptionLanguageCodeRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyIdRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\UseCaseRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyDescriptionWithFallbackRequest implements UseCaseRequest, PropertyIdRequest, DescriptionLanguageCodeRequest { | ||
|
||
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
...seCases/GetPropertyDescriptionWithFallback/GetPropertyDescriptionWithFallbackResponse.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\GetPropertyDescriptionWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Domain\ReadModel\Description; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyDescriptionWithFallbackResponse { | ||
|
||
private Description $description; | ||
private string $lastModified; | ||
private int $revisionId; | ||
|
||
public function __construct( Description $description, string $lastModified, int $revisionId ) { | ||
$this->description = $description; | ||
$this->lastModified = $lastModified; | ||
$this->revisionId = $revisionId; | ||
} | ||
|
||
public function getDescription(): Description { | ||
return $this->description; | ||
} | ||
|
||
public function getLastModified(): string { | ||
return $this->lastModified; | ||
} | ||
|
||
public function getRevisionId(): int { | ||
return $this->revisionId; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...eCases/GetPropertyDescriptionWithFallback/GetPropertyDescriptionWithFallbackValidator.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,18 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyDescriptionWithFallback; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface GetPropertyDescriptionWithFallbackValidator { | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
public function validateAndDeserialize( | ||
GetPropertyDescriptionWithFallbackRequest $request | ||
): DeserializedGetPropertyDescriptionWithFallbackRequest; | ||
} |
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
114 changes: 114 additions & 0 deletions
114
repo/rest-api/src/RouteHandlers/GetPropertyDescriptionWithFallbackRouteHandler.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,114 @@ | ||
<?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\GetPropertyDescriptionWithFallback\GetPropertyDescriptionWithFallback; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyDescriptionWithFallback\GetPropertyDescriptionWithFallbackRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyDescriptionWithFallback\GetPropertyDescriptionWithFallbackResponse; | ||
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; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyDescriptionWithFallbackRouteHandler extends SimpleHandler { | ||
|
||
private const PROPERTY_ID_PATH_PARAM = 'property_id'; | ||
private const LANGUAGE_CODE_PATH_PARAM = 'language_code'; | ||
|
||
private GetPropertyDescriptionWithFallback $useCase; | ||
private MiddlewareHandler $middlewareHandler; | ||
private ResponseFactory $responseFactory; | ||
|
||
public function __construct( | ||
GetPropertyDescriptionWithFallback $useCase, | ||
MiddlewareHandler $middlewareHandler, | ||
ResponseFactory $responseFactory | ||
) { | ||
$this->useCase = $useCase; | ||
$this->middlewareHandler = $middlewareHandler; | ||
$this->responseFactory = $responseFactory; | ||
} | ||
|
||
public static function factory(): self { | ||
return new self( | ||
WbRestApi::getGetPropertyDescriptionWithFallback(), | ||
new MiddlewareHandler( [ | ||
WbRestApi::getUnexpectedErrorHandlerMiddleware(), | ||
new UserAgentCheckMiddleware(), | ||
new AuthenticationMiddleware(), | ||
WbRestApi::getPreconditionMiddlewareFactory()->newPreconditionMiddleware( | ||
fn( RequestInterface $request ): string => $request->getPathParam( self::PROPERTY_ID_PATH_PARAM ) | ||
), | ||
] ), | ||
new ResponseFactory() | ||
); | ||
} | ||
|
||
public function needsWriteAccess(): bool { | ||
return false; | ||
} | ||
|
||
/** | ||
* @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 { | ||
return $this->newSuccessHttpResponse( | ||
$this->useCase->execute( new GetPropertyDescriptionWithFallbackRequest( $propertyId, $languageCode ) ) | ||
); | ||
} 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 newSuccessHttpResponse( GetPropertyDescriptionWithFallbackResponse $useCaseResponse ): Response { | ||
$httpResponse = $this->getResponseFactory()->create(); | ||
$httpResponse->setHeader( 'Content-Type', 'application/json' ); | ||
$httpResponse->setHeader( 'Last-Modified', wfTimestamp( TS_RFC2822, $useCaseResponse->getLastModified() ) ); | ||
$this->setEtagFromRevId( $httpResponse, $useCaseResponse->getRevisionId() ); | ||
$httpResponse->setBody( new StringStream( json_encode( $useCaseResponse->getDescription()->getText() ) ) ); | ||
|
||
return $httpResponse; | ||
} | ||
|
||
private function setEtagFromRevId( Response $response, int $revId ): void { | ||
$response->setHeader( 'ETag', "\"$revId\"" ); | ||
} | ||
|
||
/** | ||
* Preconditions are checked via {@link PreconditionMiddleware} | ||
*/ | ||
public function checkPreconditions(): ?ResponseInterface { | ||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.