/
Guzzle.php
executable file
·224 lines (192 loc) · 6.23 KB
/
Guzzle.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Elastica\Transport;
use Elastica\Connection;
use Elastica\Exception\Connection\GuzzleException;
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;
use Elastica\Util;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Uri;
/**
* Elastica Guzzle Transport object.
*
* @author Milan Magudia <milan@magudia.com>
*/
class Guzzle extends AbstractTransport
{
/**
* Http scheme.
*
* @var string Http scheme
*/
protected $_scheme = 'http';
/**
* Curl resource to reuse.
*
* @var Client Guzzle client to reuse
*/
protected static $_guzzleClientConnection;
/**
* Makes calls to the elasticsearch server.
*
* All calls that are made to the server are done through this function
*
* @param \Elastica\Request $request
* @param array $params Host, Port, ...
*
* @throws \Elastica\Exception\ConnectionException
* @throws \Elastica\Exception\ResponseException
* @throws \Elastica\Exception\Connection\HttpException
*
* @return \Elastica\Response Response object
*/
public function exec(Request $request, array $params): Response
{
$connection = $this->getConnection();
$client = $this->_getGuzzleClient($connection->isPersistent());
$options = [
'base_uri' => $this->_getBaseUrl($connection),
'headers' => [
'Content-Type' => $request->getContentType(),
],
'exceptions' => false, // 4xx and 5xx is expected and NOT an exceptions in this context
];
if ($connection->getTimeout()) {
$options['timeout'] = $connection->getTimeout();
}
$proxy = $connection->getProxy();
// See: https://github.com/facebook/hhvm/issues/4875
if (\is_null($proxy) && \defined('HHVM_VERSION')) {
$proxy = \getenv('http_proxy') ?: null;
}
if (!\is_null($proxy)) {
$options['proxy'] = $proxy;
}
$req = $this->_createPsr7Request($request, $connection);
try {
$start = \microtime(true);
$res = $client->send($req, $options);
$end = \microtime(true);
} catch (TransferException $ex) {
throw new GuzzleException($ex, $request, new Response($ex->getMessage()));
}
$responseBody = (string) $res->getBody();
$response = new Response($responseBody, $res->getStatusCode());
$response->setQueryTime($end - $start);
if ($connection->hasConfig('bigintConversion')) {
$response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
}
$response->setTransferInfo(
[
'request_header' => $request->getMethod(),
'http_code' => $res->getStatusCode(),
]
);
if ($response->hasError()) {
throw new ResponseException($request, $response);
}
if ($response->hasFailedShards()) {
throw new PartialShardFailureException($request, $response);
}
return $response;
}
/**
* @param Request $request
* @param Connection $connection
*
* @return Psr7\Request
*/
protected function _createPsr7Request(Request $request, Connection $connection)
{
$req = new Psr7\Request(
$request->getMethod(),
$this->_getActionPath($request),
$connection->hasConfig('headers') && \is_array($connection->getConfig('headers'))
? $connection->getConfig('headers')
: []
);
$data = $request->getData();
if (!empty($data) || '0' === $data) {
if (Request::GET == $req->getMethod()) {
$req = $req->withMethod(Request::POST);
}
if ($this->hasParam('postWithRequestBody') && true == $this->getParam('postWithRequestBody')) {
$request->setMethod(Request::POST);
$req = $req->withMethod(Request::POST);
}
$req = $req->withBody(
Psr7\stream_for(\is_array($data)
? JSON::stringify($data, JSON_UNESCAPED_UNICODE)
: $data
)
);
}
return $req;
}
/**
* Return Guzzle resource.
*
* @param bool $persistent False if not persistent connection
*
* @return Client
*/
protected function _getGuzzleClient(bool $persistent = true): Client
{
if (!$persistent || !self::$_guzzleClientConnection) {
self::$_guzzleClientConnection = new Client();
}
return self::$_guzzleClientConnection;
}
/**
* Builds the base url for the guzzle connection.
*
* @param Connection $connection
*
* @return string
*/
protected function _getBaseUrl(Connection $connection): string
{
// If url is set, url is taken. Otherwise port, host and path
$url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
if (!empty($url)) {
$baseUri = $url;
} else {
$baseUri = (string) Uri::fromParts([
'scheme' => $this->_scheme,
'host' => $connection->getHost(),
'port' => $connection->getPort(),
'path' => \ltrim('/', $connection->getPath()),
]);
}
return \rtrim($baseUri, '/');
}
/**
* Builds the action path url for each request.
*
* @param Request $request
*
* @return string
*/
protected function _getActionPath(Request $request): string
{
$action = $request->getPath();
if ($action) {
$action = '/'.\ltrim($action, '/');
}
if (!Util::isDateMathEscaped($action)) {
$action = Util::escapeDateMath($action);
}
$query = $request->getQuery();
if (!empty($query)) {
$action .= '?'.\http_build_query(
$this->sanityzeQueryStringBool($query)
);
}
return $action;
}
}