V1Response.php 2.1 KB

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