-
Notifications
You must be signed in to change notification settings - Fork 454
/
Client.php
133 lines (120 loc) · 3.48 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Concrete\Core\Http\Client;
use Concrete\Core\Logging\Channels;
use Concrete\Core\Logging\LoggerAwareTrait;
use Zend\Http\Client as ZendClient;
use Zend\Http\Request as ZendRequest;
use Zend\Uri\Http as ZendUriHttp;
use Concrete\Core\Logging\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Exception;
use Throwable;
class Client extends ZendClient implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* Get the currently configured logger.
*
* @return LoggerInterface|null
*/
public function getLogger()
{
return $this->logger;
}
public function getLoggerChannel()
{
return Channels::CHANNEL_NETWORK;
}
/**
* Set the currently configured logger.
*
* @param LoggerInterface|null $value
*
* @return static
*/
public function setLogger(LoggerInterface $value = null)
{
$this->logger = $value;
return $this;
}
/**
* Send a request without following any redirects
*
* @see ZendClient::send()
* @param ZendRequest|null $request
* @return \Zend\Http\Response
*/
public function sendWithoutRedirects(ZendRequest $request = null)
{
$maxRedirects = $this->config['maxredirects'];
$this->config['maxredirects'] = 0;
try {
return $this->send($request);
} finally {
$this->config['maxredirects'] = $maxRedirects;
}
}
/**
* {@inheritdoc}
*
* @see ZendClient::send()
*/
public function send(ZendRequest $request = null)
{
$response = parent::send($request);
$logger = $this->getLogger();
if ($logger !== null) {
$statusCode = $response->getStatusCode();
try {
$body = $response->getBody();
} catch (Exception $x) {
$body = '';
} catch (Throwable $x) {
$body = '';
}
if (mb_strlen($body) <= 200) {
$shortBody = $body;
} else {
$shortBody = mb_substr($body, 0, 197) . '...';
}
$logger->debug(
'The response code was {statusCode} and the body was {shortBody}',
[
'statusCode' => $statusCode,
'headers' => $response->getHeaders()->toArray(),
'shortBody' => $shortBody,
'body' => $body,
]
);
}
return $response;
}
/**
* {@inheritdoc}
*
* @see ZendClient::doRequest()
*/
protected function doRequest(ZendUriHttp $uri, $method, $secure = false, $headers = [], $body = '')
{
$logger = $this->getLogger();
if ($logger !== null) {
$uriString = (string) $uri;
if (mb_strlen($body) <= 200) {
$shortBody = (string) $body;
} else {
$shortBody = mb_substr($body, 0, 197) . '...';
}
$logger->debug(
'Sending {method} request to {uri} with body {shortBody}',
[
'uri' => $uriString,
'method' => $method,
'headers' => is_array($headers) ? $headers : (array) $headers,
'shortBody' => $shortBody,
'body' => $body,
]
);
}
return parent::doRequest($uri, $method, $secure, $headers, $body);
}
}