1
0

V2Response.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\AppFramework\OCS;
  7. use OCP\AppFramework\Http;
  8. use OCP\AppFramework\Http\DataResponse;
  9. use OCP\AppFramework\OCSController;
  10. /**
  11. * @psalm-import-type DataResponseType from DataResponse
  12. * @template S of int
  13. * @template-covariant T of DataResponseType
  14. * @template H of array<string, mixed>
  15. * @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
  16. */
  17. class V2Response extends BaseResponse {
  18. /**
  19. * The V2 endpoint just passes on status codes.
  20. * Of course we have to map the OCS specific codes to proper HTTP status codes
  21. *
  22. * @return int
  23. */
  24. public function getStatus() {
  25. $status = parent::getStatus();
  26. if ($status === OCSController::RESPOND_UNAUTHORISED) {
  27. return Http::STATUS_UNAUTHORIZED;
  28. } elseif ($status === OCSController::RESPOND_NOT_FOUND) {
  29. return Http::STATUS_NOT_FOUND;
  30. } elseif ($status === OCSController::RESPOND_SERVER_ERROR || $status === OCSController::RESPOND_UNKNOWN_ERROR) {
  31. return Http::STATUS_INTERNAL_SERVER_ERROR;
  32. } elseif ($status < 200 || $status > 600) {
  33. return Http::STATUS_BAD_REQUEST;
  34. }
  35. return $status;
  36. }
  37. /**
  38. * Construct the meta part of the response
  39. * And then late the base class render
  40. *
  41. * @return string
  42. */
  43. public function render() {
  44. $status = parent::getStatus();
  45. $meta = [
  46. 'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure',
  47. 'statuscode' => $this->getOCSStatus(),
  48. 'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage ?? '',
  49. ];
  50. if ($this->itemsCount !== null) {
  51. $meta['totalitems'] = $this->itemsCount;
  52. }
  53. if ($this->itemsPerPage !== null) {
  54. $meta['itemsperpage'] = $this->itemsPerPage;
  55. }
  56. return $this->renderResult($meta);
  57. }
  58. }