V1Response.php 2.0 KB

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