1
0

V2Response.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\AppFramework\OCS;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCSController;
  29. /**
  30. * @psalm-import-type DataResponseType from DataResponse
  31. * @template S of int
  32. * @template-covariant T of DataResponseType
  33. * @template H of array<string, mixed>
  34. * @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
  35. */
  36. class V2Response extends BaseResponse {
  37. /**
  38. * The V2 endpoint just passes on status codes.
  39. * Of course we have to map the OCS specific codes to proper HTTP status codes
  40. *
  41. * @return int
  42. */
  43. public function getStatus() {
  44. $status = parent::getStatus();
  45. if ($status === OCSController::RESPOND_UNAUTHORISED) {
  46. return Http::STATUS_UNAUTHORIZED;
  47. } elseif ($status === OCSController::RESPOND_NOT_FOUND) {
  48. return Http::STATUS_NOT_FOUND;
  49. } elseif ($status === OCSController::RESPOND_SERVER_ERROR || $status === OCSController::RESPOND_UNKNOWN_ERROR) {
  50. return Http::STATUS_INTERNAL_SERVER_ERROR;
  51. } elseif ($status < 200 || $status > 600) {
  52. return Http::STATUS_BAD_REQUEST;
  53. }
  54. return $status;
  55. }
  56. /**
  57. * Construct the meta part of the response
  58. * And then late the base class render
  59. *
  60. * @return string
  61. */
  62. public function render() {
  63. $status = parent::getStatus();
  64. $meta = [
  65. 'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure',
  66. 'statuscode' => $this->getOCSStatus(),
  67. 'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage ?? '',
  68. ];
  69. if ($this->itemsCount !== null) {
  70. $meta['totalitems'] = $this->itemsCount;
  71. }
  72. if ($this->itemsPerPage !== null) {
  73. $meta['itemsperpage'] = $this->itemsPerPage;
  74. }
  75. return $this->renderResult($meta);
  76. }
  77. }