Skip to content

Commit 7011d12

Browse files
author
Douglas Greenshields
committed
add request access logic
1 parent 2565e25 commit 7011d12

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

src/AsyncPlugin.php

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
use Http\Message\RequestFactory;
1212
use Http\Promise\Promise;
1313
use Psr\Http\Message\ResponseInterface;
14-
use Solarium\Core\Client\Adapter\Guzzle;
14+
use Solarium\Core\Client\Adapter\Guzzle as GuzzleAdapter;
1515
use Solarium\Core\Client\Endpoint;
16+
use Solarium\Core\Client\Request;
1617
use Solarium\Core\Client\Response;
1718
use Solarium\Core\Plugin\AbstractPlugin;
1819
use Solarium\Core\Query\AbstractQuery;
@@ -36,15 +37,7 @@ class AsyncPlugin extends AbstractPlugin
3637
*/
3738
public function queryAsync($query, $endpoint = null)
3839
{
39-
if (null !== $this->asyncClient) {
40-
$asyncClient = $this->asyncClient;
41-
$usingGuzzle = false;
42-
} else {
43-
$existingSolariumAdapter = $this->client->getAdapter(false);
44-
$this->client->setAdapter(Guzzle::class);
45-
$asyncClient = new Guzzle6Adapter($this->client->getAdapter()->getGuzzleClient());
46-
$usingGuzzle = true;
47-
}
40+
$asyncClient = $this->asyncClient ?: new Guzzle6Adapter($this->client->getAdapter()->getGuzzleClient());
4841
$request = $this->client->createRequest($query);
4942
$method = $request->getMethod();
5043
$endpoint = $this->client->getEndpoint($endpoint);
@@ -64,7 +57,7 @@ public function queryAsync($query, $endpoint = null)
6457
$asyncClient = new PluginClient($asyncClient, [$authenticationPlugin]);
6558
}
6659

67-
$promise = $asyncClient->sendAsyncRequest($request)
60+
return $asyncClient->sendAsyncRequest($request)
6861
->then(
6962
function (ResponseInterface $response) {
7063
$responseHeaders = [
@@ -79,13 +72,6 @@ function (ResponseInterface $response) {
7972
return new Response((string) $response->getBody(), $responseHeaders);
8073
}
8174
);
82-
83-
if ($usingGuzzle) {
84-
//set the solarium adapter state back
85-
$this->client->setAdapter($existingSolariumAdapter);
86-
}
87-
88-
return $promise;
8975
}
9076

9177
public function setAsyncClient(HttpAsyncClient $asyncClient)
@@ -101,4 +87,43 @@ public function setRequestFactory(RequestFactory $requestFactory)
10187

10288
return $this;
10389
}
90+
91+
protected function initPluginType()
92+
{
93+
$this->client->setAdapter(GuzzleAdapter::class);
94+
}
95+
96+
private function getRequestBody(Request $request)
97+
{
98+
if ($request->getMethod() !== 'POST') {
99+
return null;
100+
}
101+
102+
if ($request->getFileUpload()) {
103+
return fopen($request->getFileUpload(), 'r');
104+
}
105+
106+
return $request->getRawData();
107+
}
108+
109+
private function getRequestHeaders(Request $request)
110+
{
111+
$headers = [];
112+
foreach ($request->getHeaders() as $headerLine) {
113+
list($header, $value) = explode(':', $headerLine);
114+
if ($header = trim($header)) {
115+
$headers[$header] = trim($value);
116+
}
117+
}
118+
119+
if (!isset($headers['Content-Type'])) {
120+
if ($request->getMethod() == Request::METHOD_GET) {
121+
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
122+
} else {
123+
$headers['Content-Type'] = 'application/xml; charset=utf-8';
124+
}
125+
}
126+
127+
return $headers;
128+
}
104129
}

0 commit comments

Comments
 (0)