-
Notifications
You must be signed in to change notification settings - Fork 35
/
HttpClientTransport.php
151 lines (124 loc) 路 4.93 KB
/
HttpClientTransport.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
<?php
/*
* This file is part of the jolicode/elastically library.
*
* (c) JoliCode <coucou@jolicode.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JoliCode\Elastically\Transport;
use Elastica\Connection;
use Elastica\Exception\Connection\HttpException;
use Elastica\Exception\ConnectionException;
use Elastica\Exception\ExceptionInterface;
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Request as ElasticaRequest;
use Elastica\Response;
use Elastica\Transport\AbstractTransport;
use Elastica\Util;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Implement Symfony HttpClient as an Elastica Transport.
*/
class HttpClientTransport extends AbstractTransport
{
private HttpClientInterface $client;
/**
* Elastica Connection does not have this option.
*/
private string $scheme;
public function __construct(HttpClientInterface $client, string $scheme = 'http', ?Connection $connection = null)
{
parent::__construct($connection);
$this->client = $client;
$this->scheme = $scheme;
}
/**
* @throws ExceptionInterface
* @throws TransportExceptionInterface
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
*/
public function exec(Request $request, array $params): Response
{
$connection = $this->getConnection();
$headers = $connection->hasConfig('headers') && \is_array($connection->getConfig('headers'))
? $connection->getConfig('headers')
: [];
$headers['Content-Type'] = $request->getContentType();
$options = [
'headers' => $headers,
];
$data = $request->getData();
$method = $request->getMethod();
if (!empty($data) || '0' === $data) {
if (ElasticaRequest::GET === $method) {
$method = ElasticaRequest::POST;
}
if (\is_array($data)) {
$options['body'] = JSON::stringify($data, \JSON_UNESCAPED_UNICODE);
} else {
$options['body'] = $data;
}
}
if ($connection->getTimeout()) {
$options['timeout'] = $connection->getTimeout();
}
$proxy = $connection->getProxy();
if (null !== $proxy) {
$options['proxy'] = $proxy;
}
try {
$response = $this->client->request($method, $this->_getUri($request, $connection), $options);
$elasticaResponse = new Response($response->getContent(), $response->getStatusCode());
} catch (ClientException|ServerException $e) { // Error 4xx and 5xx
$elasticaResponse = new Response($response->getContent(false), $response->getStatusCode());
} catch (HttpExceptionInterface $e) {
throw new HttpException($e->getCode(), $request);
} catch (TransportExceptionInterface $e) {
throw new ConnectionException($e->getMessage(), $request);
}
if ($connection->hasConfig('bigintConversion')) {
$elasticaResponse->setJsonBigintConversion($connection->getConfig('bigintConversion'));
}
$elasticaResponse->setTransferInfo($response->getInfo());
if ($elasticaResponse->hasError()) {
throw new ResponseException($request, $elasticaResponse);
}
if ($elasticaResponse->hasFailedShards()) {
throw new PartialShardFailureException($request, $elasticaResponse);
}
return $elasticaResponse;
}
protected function _getUri(Request $request, Connection $connection): string
{
$url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
if (!empty($url)) {
$baseUri = $url;
} else {
$baseUri = $this->scheme . '://' . $connection->getHost() . ':' . $connection->getPort() . '/' . $connection->getPath();
}
$requestPath = $request->getPath();
if (!Util::isDateMathEscaped($requestPath)) {
$requestPath = Util::escapeDateMath($requestPath);
}
$baseUri .= $requestPath;
$query = $request->getQuery();
if (!empty($query)) {
$baseUri .= '?' . http_build_query($this->sanityzeQueryStringBool($query));
}
return $baseUri;
}
}