V1Response.php 2.4 KB

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