3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Network;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Network\HTTPException\NotImplementedException;
27 use GuzzleHttp\Psr7\Response;
28 use Psr\Http\Message\ResponseInterface;
31 * A content wrapper class for Guzzle call results
33 class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
35 /** @var string The URL */
42 * @var int the error number or 0 (zero) if no error
47 * @var string the error message or '' (the empty string) if no
51 public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
53 parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
55 $this->error = $error;
56 $this->errorNumber = $errorNumber;
58 $this->checkSuccess();
61 private function checkSuccess()
63 $this->isSuccess = ($this->getStatusCode() >= 200 && $this->getStatusCode() <= 299) || $this->errorNumber == 0;
65 // Everything higher or equal 400 is not a success
66 if ($this->getReturnCode() >= 400) {
67 $this->isSuccess = false;
70 if (!$this->isSuccess) {
71 Logger::debug('debug', ['info' => $this->getHeaders()]);
74 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
75 $this->isTimeout = true;
77 $this->isTimeout = false;
82 public function getReturnCode()
84 return $this->getStatusCode();
88 public function getContentType()
90 $contentTypes = $this->getHeader('Content-Type') ?? [];
91 return array_pop($contentTypes) ?? '';
95 public function inHeader(string $field)
97 return $this->hasHeader($field);
101 public function getHeaderArray()
103 return $this->getHeaders();
107 public function isSuccess()
109 return $this->isSuccess;
113 public function getUrl()
119 public function getRedirectUrl()
125 public function isRedirectUrl()
127 throw new NotImplementedException();
131 public function getErrorNumber()
133 return $this->errorNumber;
137 public function getError()
143 public function isTimeout()
145 return $this->isTimeout;
148 /// @todo - fix mismatching use of "getBody()" as string here and parent "getBody()" as streaminterface
149 public function getBody(): string
151 return (string) parent::getBody();