You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Http\Client;
  26. use OCP\Http\Client\IResponse;
  27. use Psr\Http\Message\ResponseInterface;
  28. /**
  29. * Class Response
  30. *
  31. * @package OC\Http
  32. */
  33. class Response implements IResponse {
  34. /** @var ResponseInterface */
  35. private $response;
  36. /**
  37. * @var bool
  38. */
  39. private $stream;
  40. /**
  41. * @param ResponseInterface $response
  42. * @param bool $stream
  43. */
  44. public function __construct(ResponseInterface $response, $stream = false) {
  45. $this->response = $response;
  46. $this->stream = $stream;
  47. }
  48. /**
  49. * @return string|resource
  50. */
  51. public function getBody() {
  52. return $this->stream ?
  53. $this->response->getBody()->detach():
  54. $this->response->getBody()->getContents();
  55. }
  56. /**
  57. * @return int
  58. */
  59. public function getStatusCode(): int {
  60. return $this->response->getStatusCode();
  61. }
  62. /**
  63. * @param string $key
  64. * @return string
  65. */
  66. public function getHeader(string $key): string {
  67. $headers = $this->response->getHeader($key);
  68. if (count($headers) === 0) {
  69. return '';
  70. }
  71. return $headers[0];
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function getHeaders(): array {
  77. return $this->response->getHeaders();
  78. }
  79. }