diff options
Diffstat (limited to 'core/Controller/TextProcessingApiController.php')
-rw-r--r-- | core/Controller/TextProcessingApiController.php | 56 |
1 files changed, 23 insertions, 33 deletions
diff --git a/core/Controller/TextProcessingApiController.php b/core/Controller/TextProcessingApiController.php index cbba7e976b0..d3e6967f169 100644 --- a/core/Controller/TextProcessingApiController.php +++ b/core/Controller/TextProcessingApiController.php @@ -3,37 +3,23 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net> - * - * @author Marcel Klehr <mklehr@gmx.net> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Core\Controller; use InvalidArgumentException; -use OCA\Core\ResponseDefinitions; +use OC\Core\ResponseDefinitions; use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\AnonRateLimit; +use OCP\AppFramework\Http\Attribute\ApiRoute; use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\Attribute\PublicPage; use OCP\AppFramework\Http\Attribute\UserRateLimit; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCSController; use OCP\Common\Exception\NotFoundException; use OCP\DB\Exception; use OCP\IL10N; @@ -51,15 +37,15 @@ use Psr\Log\LoggerInterface; /** * @psalm-import-type CoreTextProcessingTask from ResponseDefinitions */ -class TextProcessingApiController extends \OCP\AppFramework\OCSController { +class TextProcessingApiController extends OCSController { public function __construct( - string $appName, - IRequest $request, - private IManager $textProcessingManager, - private IL10N $l, - private ?string $userId, + string $appName, + IRequest $request, + private IManager $textProcessingManager, + private IL10N $l, + private ?string $userId, private ContainerInterface $container, - private LoggerInterface $logger, + private LoggerInterface $logger, ) { parent::__construct($appName, $request); } @@ -67,11 +53,12 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController { /** * This endpoint returns all available LanguageModel task types * - * @return DataResponse<Http::STATUS_OK, array{types: array{id: string, name: string, description: string}[]}, array{}> + * @return DataResponse<Http::STATUS_OK, array{types: list<array{id: string, name: string, description: string}>}, array{}> * * 200: Task types returned */ #[PublicPage] + #[ApiRoute(verb: 'GET', url: '/tasktypes', root: '/textprocessing')] public function taskTypes(): DataResponse { $typeClasses = $this->textProcessingManager->getAvailableTaskTypes(); $types = []; @@ -113,6 +100,7 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController { #[PublicPage] #[UserRateLimit(limit: 20, period: 120)] #[AnonRateLimit(limit: 5, period: 120)] + #[ApiRoute(verb: 'POST', url: '/schedule', root: '/textprocessing')] public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse { try { $task = new Task($type, $input, $appId, $this->userId, $identifier); @@ -122,7 +110,7 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController { try { try { $this->textProcessingManager->runOrScheduleTask($task); - } catch(TaskFailureException) { + } catch (TaskFailureException) { // noop, because the task object has the failure status set already, we just return the task json } @@ -150,6 +138,7 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController { * 404: Task not found */ #[PublicPage] + #[ApiRoute(verb: 'GET', url: '/task/{id}', root: '/textprocessing')] public function getTask(int $id): DataResponse { try { $task = $this->textProcessingManager->getUserTask($id, $this->userId); @@ -177,6 +166,7 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController { * 404: Task not found */ #[NoAdminRequired] + #[ApiRoute(verb: 'DELETE', url: '/task/{id}', root: '/textprocessing')] public function deleteTask(int $id): DataResponse { try { $task = $this->textProcessingManager->getUserTask($id, $this->userId); @@ -202,18 +192,18 @@ class TextProcessingApiController extends \OCP\AppFramework\OCSController { * * @param string $appId ID of the app * @param string|null $identifier An arbitrary identifier for the task - * @return DataResponse<Http::STATUS_OK, array{tasks: CoreTextProcessingTask[]}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> + * @return DataResponse<Http::STATUS_OK, array{tasks: list<CoreTextProcessingTask>}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> * - * 200: Task list returned + * 200: Task list returned */ #[NoAdminRequired] + #[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/textprocessing')] public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse { try { $tasks = $this->textProcessingManager->getUserTasksByApp($this->userId, $appId, $identifier); - /** @var CoreTextProcessingTask[] $json */ - $json = array_map(static function (Task $task) { + $json = array_values(array_map(static function (Task $task) { return $task->jsonSerialize(); - }, $tasks); + }, $tasks)); return new DataResponse([ 'tasks' => $json, |