V2Response.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\AppFramework\Http;
  25. use OCP\API;
  26. class V2Response extends BaseResponse {
  27. /**
  28. * The V2 endpoint just passes on status codes.
  29. * Of course we have to map the OCS specific codes to proper HTTP status codes
  30. *
  31. * @return int
  32. */
  33. public function getStatus() {
  34. $status = parent::getStatus();
  35. if ($status === API::RESPOND_UNAUTHORISED) {
  36. return Http::STATUS_UNAUTHORIZED;
  37. } else if ($status === API::RESPOND_NOT_FOUND) {
  38. return Http::STATUS_NOT_FOUND;
  39. } else if ($status === API::RESPOND_SERVER_ERROR || $status === API::RESPOND_UNKNOWN_ERROR) {
  40. return Http::STATUS_INTERNAL_SERVER_ERROR;
  41. } else if ($status < 200 || $status > 600) {
  42. return Http::STATUS_BAD_REQUEST;
  43. }
  44. return $status;
  45. }
  46. /**
  47. * Construct the meta part of the response
  48. * And then late the base class render
  49. *
  50. * @return string
  51. */
  52. public function render() {
  53. $status = parent::getStatus();
  54. $meta = [
  55. 'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure',
  56. 'statuscode' => $this->getOCSStatus(),
  57. 'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage,
  58. ];
  59. if ($this->itemsCount !== null) {
  60. $meta['totalitems'] = $this->itemsCount;
  61. }
  62. if ($this->itemsPerPage !== null) {
  63. $meta['itemsperpage'] = $this->itemsPerPage;
  64. }
  65. return $this->renderResult($meta);
  66. }
  67. }