-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add PSR-18 http adapter #754
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a70d56e
add PSR-18 http adapter
dmaicher f54b12e
fix missing Content-Type request header
dmaicher 9a5c631
deprecate other adapters + AdapterHelper
dmaicher 464424c
add symfony/phpunit-bridge to display deprecations
dmaicher 0df3d39
deprecate Client options: adapter + adapteroptions
dmaicher 55c0566
handle basic http auth
dmaicher bbdd234
deprecate endpoint timeout option
dmaicher d520929
use Psr18Adapter for integration tests
dmaicher 9f94ef0
simplify
dmaicher 13bd39d
fix BC layer
dmaicher 836ba9d
rename class
dmaicher d40f319
fix
dmaicher 98098c7
fix
dmaicher b3abcbf
another fix
dmaicher 7c0b6da
some fixes
dmaicher b4f2a10
make it final
dmaicher a27c23f
Do not allow passing null as adapter in Solarium 6
dmaicher a023339
change constructor signature
dmaicher ec22eb7
fix
dmaicher c420f95
revert deprecation of Endpoint timeout
dmaicher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
namespace Solarium\Core\Client\Adapter; | ||
|
||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Solarium\Core\Client\Endpoint; | ||
use Solarium\Core\Client\Request; | ||
use Solarium\Core\Client\Response; | ||
use Solarium\Core\Configurable; | ||
use Solarium\Exception\HttpException; | ||
|
||
final class Psr18Adapter extends Configurable implements AdapterInterface | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $httpClient; | ||
|
||
/** | ||
* @var RequestFactoryInterface | ||
*/ | ||
private $requestFactory; | ||
|
||
/** | ||
* @var StreamFactoryInterface | ||
*/ | ||
private $streamFactory; | ||
|
||
public function __construct( | ||
ClientInterface $httpClient, | ||
RequestFactoryInterface $requestFactory, | ||
StreamFactoryInterface $streamFactory | ||
) { | ||
parent::__construct(); | ||
$this->httpClient = $httpClient; | ||
$this->requestFactory = $requestFactory; | ||
$this->streamFactory = $streamFactory; | ||
} | ||
|
||
public function execute(Request $request, Endpoint $endpoint): Response | ||
{ | ||
try { | ||
return $this->createResponse($this->httpClient->sendRequest($this->createPsr7Request($request, $endpoint))); | ||
} catch (ClientExceptionInterface $clientException) { | ||
throw new HttpException("HTTP request failed, {$clientException}"); | ||
} | ||
} | ||
|
||
private function createPsr7Request(Request $request, Endpoint $endpoint): RequestInterface | ||
{ | ||
$uri = AdapterHelper::buildUri($request, $endpoint); | ||
|
||
$psr7Request = $this->requestFactory->createRequest( | ||
$request->getMethod(), | ||
$uri | ||
); | ||
|
||
if (null !== $body = $this->getRequestBody($request)) { | ||
$psr7Request = $psr7Request->withBody($this->streamFactory->createStream($body)); | ||
} | ||
|
||
foreach ($this->getRequestHeaders($request, $endpoint) as $name => $values) { | ||
foreach ($values as $value) { | ||
$psr7Request = $psr7Request->withAddedHeader($name, $value); | ||
} | ||
} | ||
|
||
return $psr7Request; | ||
} | ||
|
||
private function createResponse(ResponseInterface $psr7Response): Response | ||
{ | ||
$headerLines = [ | ||
sprintf( | ||
'HTTP/%s %s %s', | ||
$psr7Response->getProtocolVersion(), | ||
$psr7Response->getStatusCode(), | ||
$psr7Response->getReasonPhrase() | ||
), | ||
]; | ||
|
||
foreach ($psr7Response->getHeaders() as $name => $values) { | ||
$headerLines[] = sprintf('%s: %s', $name, implode(', ', $values)); | ||
} | ||
|
||
return new Response((string) $psr7Response->getBody(), $headerLines); | ||
} | ||
|
||
private function getRequestBody(Request $request): ?string | ||
{ | ||
if (Request::METHOD_PUT == $request->getMethod()) { | ||
return $request->getRawData(); | ||
} | ||
|
||
if (Request::METHOD_POST !== $request->getMethod()) { | ||
return null; | ||
} | ||
|
||
if ($request->getFileUpload()) { | ||
return AdapterHelper::buildUploadBodyFromRequest($request); | ||
} | ||
|
||
return $request->getRawData(); | ||
} | ||
|
||
private function getRequestHeaders(Request $request, Endpoint $endpoint): array | ||
{ | ||
$headers = []; | ||
|
||
foreach ($request->getHeaders() as $headerLine) { | ||
[$header, $value] = explode(':', $headerLine); | ||
if ($header = trim($header)) { | ||
$headers[$header][] = $value; | ||
} | ||
} | ||
|
||
if (!isset($headers['Content-Type'])) { | ||
if (Request::METHOD_GET == $request->getMethod()) { | ||
$headers['Content-Type'] = ['application/x-www-form-urlencoded; charset=utf-8']; | ||
} else { | ||
$headers['Content-Type'] = ['application/xml; charset=utf-8']; | ||
} | ||
} | ||
|
||
if (!isset($headers['Authorization'])) { | ||
// Try endpoint authentication first, fallback to request for backwards compatibility | ||
$authData = $endpoint->getAuthentication(); | ||
if (empty($authData['username'])) { | ||
$authData = $request->getAuthentication(); | ||
} | ||
|
||
if (!empty($authData['username']) && !empty($authData['password'])) { | ||
$headers['Authorization'] = ['Basic '.base64_encode($authData['username'].':'.$authData['password'])]; | ||
} | ||
} | ||
|
||
return $headers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for "final" (or the advantage)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not open this class for extension. Its does not make sense anyway as everything is private except
public function execute(Request $request, Endpoint $endpoint): Response
.So we should not even encourage people to extend it. They should use composition if they need to customize stuff.
Or what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Let's leave it like this and wait until someone complains ;-)