V1Response.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 V1Response extends BaseResponse {
  18. /**
  19. * The V1 endpoint has very limited http status codes basically everything
  20. * is status 200 except 401
  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. }
  29. return Http::STATUS_OK;
  30. }
  31. /**
  32. * In v1 all OK is 100
  33. *
  34. * @return int
  35. */
  36. public function getOCSStatus() {
  37. $status = parent::getOCSStatus();
  38. if ($status === Http::STATUS_OK) {
  39. return 100;
  40. }
  41. return $status;
  42. }
  43. /**
  44. * Construct the meta part of the response
  45. * And then late the base class render
  46. *
  47. * @return string
  48. */
  49. public function render() {
  50. $meta = [
  51. 'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure',
  52. 'statuscode' => $this->getOCSStatus(),
  53. 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage ?? '',
  54. 'totalitems' => (string)($this->itemsCount ?? ''),
  55. 'itemsperpage' => (string)($this->itemsPerPage ?? ''),
  56. ];
  57. return $this->renderResult($meta);
  58. }
  59. }