]> git.mxchange.org Git - friendica.git/blob - src/Network/HTTPClient/Response/GuzzleResponse.php
Merge pull request #11402 from annando/featured-endpoint
[friendica.git] / src / Network / HTTPClient / Response / GuzzleResponse.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Network\HTTPClient\Response;
23
24 use Friendica\Core\Logger;
25 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
26 use GuzzleHttp\Psr7\Response;
27 use GuzzleHttp\RedirectMiddleware;
28 use Psr\Http\Message\ResponseInterface;
29
30 /**
31  * A content wrapper class for Guzzle call results
32  */
33 class GuzzleResponse extends Response implements ICanHandleHttpResponses, ResponseInterface
34 {
35         /** @var string The URL */
36         private $url;
37         /** @var boolean */
38         private $isTimeout;
39         /** @var boolean */
40         private $isSuccess;
41         /**
42          * @var int the error number or 0 (zero) if no error
43          */
44         private $errorNumber;
45
46         /**
47          * @var string the error message or '' (the empty string) if no
48          */
49         private $error;
50
51         /** @var string  */
52         private $redirectUrl = '';
53         /** @var bool */
54         private $isRedirectUrl = false;
55
56         public function __construct(ResponseInterface $response, string $url, $errorNumber = 0, $error = '')
57         {
58                 parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
59                 $this->url         = $url;
60                 $this->error       = $error;
61                 $this->errorNumber = $errorNumber;
62
63                 $this->checkSuccess();
64                 $this->checkRedirect($response);
65         }
66
67         private function checkSuccess()
68         {
69                 $this->isSuccess = ($this->getStatusCode() >= 200 && $this->getStatusCode() <= 299) || $this->errorNumber == 0;
70
71                 // Everything higher or equal 400 is not a success
72                 if ($this->getReturnCode() >= 400) {
73                         $this->isSuccess = false;
74                 }
75
76                 if (!$this->isSuccess) {
77                         Logger::debug('debug', ['info' => $this->getHeaders()]);
78                 }
79
80                 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
81                         $this->isTimeout = true;
82                 } else {
83                         $this->isTimeout = false;
84                 }
85         }
86
87         private function checkRedirect(ResponseInterface $response)
88         {
89                 $headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? [];
90
91                 if (count($headersRedirect) > 0) {
92                         $this->redirectUrl   = $headersRedirect[0];
93                         $this->isRedirectUrl = true;
94                 }
95         }
96
97         /** {@inheritDoc} */
98         public function getReturnCode(): string
99         {
100                 return $this->getStatusCode();
101         }
102
103         /** {@inheritDoc} */
104         public function getContentType(): string
105         {
106                 $contentTypes = $this->getHeader('Content-Type') ?? [];
107
108                 return array_pop($contentTypes) ?? '';
109         }
110
111         /** {@inheritDoc} */
112         public function inHeader(string $field): bool
113         {
114                 return $this->hasHeader($field);
115         }
116
117         /** {@inheritDoc} */
118         public function getHeaderArray(): array
119         {
120                 return $this->getHeaders();
121         }
122
123         /** {@inheritDoc} */
124         public function isSuccess(): bool
125         {
126                 return $this->isSuccess;
127         }
128
129         /** {@inheritDoc} */
130         public function getUrl(): string
131         {
132                 return $this->url;
133         }
134
135         /** {@inheritDoc} */
136         public function getRedirectUrl(): string
137         {
138                 return $this->redirectUrl;
139         }
140
141         /** {@inheritDoc}
142          */
143         public function isRedirectUrl(): bool
144         {
145                 return $this->isRedirectUrl;
146         }
147
148         /** {@inheritDoc} */
149         public function getErrorNumber(): int
150         {
151                 return $this->errorNumber;
152         }
153
154         /** {@inheritDoc} */
155         public function getError(): string
156         {
157                 return $this->error;
158         }
159
160         /** {@inheritDoc} */
161         public function isTimeout(): bool
162         {
163                 return $this->isTimeout;
164         }
165
166         /// @todo - fix mismatching use of "getBody()" as string here and parent "getBody()" as streaminterface
167         public function getBody(): string
168         {
169                 return (string) parent::getBody();
170         }
171 }