2016-09-05 15:00:53 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-09-05 15:00:53 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-09-05 15:00:53 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\AppFramework\OCS;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Http;
|
2023-06-14 02:56:42 -04:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2021-03-03 09:10:42 -05:00
|
|
|
use OCP\AppFramework\OCSController;
|
2016-09-05 15:00:53 -04:00
|
|
|
|
2023-06-14 02:56:42 -04:00
|
|
|
/**
|
|
|
|
|
* @psalm-import-type DataResponseType from DataResponse
|
2024-12-16 10:20:48 -05:00
|
|
|
* @template S of Http::STATUS_*
|
2023-06-14 02:56:42 -04:00
|
|
|
* @template-covariant T of DataResponseType
|
|
|
|
|
* @template H of array<string, mixed>
|
2024-12-16 10:20:48 -05:00
|
|
|
* @template-extends BaseResponse<Http::STATUS_*, DataResponseType, array<string, mixed>>
|
2023-06-14 02:56:42 -04:00
|
|
|
*/
|
2016-09-05 15:00:53 -04:00
|
|
|
class V2Response extends BaseResponse {
|
|
|
|
|
/**
|
|
|
|
|
* The V2 endpoint just passes on status codes.
|
|
|
|
|
* Of course we have to map the OCS specific codes to proper HTTP status codes
|
|
|
|
|
*
|
2024-12-16 10:20:48 -05:00
|
|
|
* @return Http::STATUS_*
|
2016-09-05 15:00:53 -04:00
|
|
|
*/
|
|
|
|
|
public function getStatus() {
|
2020-10-05 09:12:57 -04:00
|
|
|
$status = parent::getStatus();
|
2021-03-03 09:10:42 -05:00
|
|
|
if ($status === OCSController::RESPOND_UNAUTHORISED) {
|
2016-09-05 15:00:53 -04:00
|
|
|
return Http::STATUS_UNAUTHORIZED;
|
2021-03-03 09:10:42 -05:00
|
|
|
} elseif ($status === OCSController::RESPOND_NOT_FOUND) {
|
2016-09-05 15:00:53 -04:00
|
|
|
return Http::STATUS_NOT_FOUND;
|
2021-03-03 09:10:42 -05:00
|
|
|
} elseif ($status === OCSController::RESPOND_SERVER_ERROR || $status === OCSController::RESPOND_UNKNOWN_ERROR) {
|
2016-09-05 15:00:53 -04:00
|
|
|
return Http::STATUS_INTERNAL_SERVER_ERROR;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($status < 200 || $status > 600) {
|
2016-09-05 15:00:53 -04:00
|
|
|
return Http::STATUS_BAD_REQUEST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the meta part of the response
|
|
|
|
|
* And then late the base class render
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function render() {
|
|
|
|
|
$status = parent::getStatus();
|
|
|
|
|
|
|
|
|
|
$meta = [
|
|
|
|
|
'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure',
|
|
|
|
|
'statuscode' => $this->getOCSStatus(),
|
2022-12-19 08:53:54 -05:00
|
|
|
'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage ?? '',
|
2016-09-05 15:00:53 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($this->itemsCount !== null) {
|
|
|
|
|
$meta['totalitems'] = $this->itemsCount;
|
|
|
|
|
}
|
|
|
|
|
if ($this->itemsPerPage !== null) {
|
|
|
|
|
$meta['itemsperpage'] = $this->itemsPerPage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->renderResult($meta);
|
|
|
|
|
}
|
|
|
|
|
}
|