Skip to content

Commit

Permalink
ACP-3963 Algolia Event-Based Features. (#11158)
Browse files Browse the repository at this point in the history
ACP-3963 Algolia Event-Based Features.
  • Loading branch information
vol4onok authored Nov 21, 2024
1 parent 0bf0a79 commit c09b9ed
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ public function send(AcpHttpRequestTransfer $acpHttpRequestTransfer): AcpHttpRes
$acpHttpRequestTransfer->getMethodOrFail(),
$acpHttpRequestTransfer->getUriOrFail(),
$acpHttpRequestTransfer->getHeaders(),
$acpHttpRequestTransfer->getBodyOrFail(),
$acpHttpRequestTransfer->getBody(),
);
$options = [];

if ($acpHttpRequestTransfer->getQuery()) {
$options['query'] = $acpHttpRequestTransfer->getQuery();
}

try {
$response = $this->httpClient->send($request);
$response = $this->httpClient->send($request, $options);
} catch (RequestException $e) {
$acpHttpResponseTransfer = new AcpHttpResponseTransfer();
$acpHttpResponseTransfer
Expand Down
27 changes: 27 additions & 0 deletions src/Spryker/Client/KernelApp/KernelAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,35 @@
use Spryker\Client\Kernel\AbstractBundleConfig;
use Spryker\Shared\KernelApp\KernelAppConstants;

/**
* @method \Spryker\Shared\KernelApp\KernelAppConfig getSharedConfig()
*/
class KernelAppConfig extends AbstractBundleConfig
{
/**
* @var string
*/
protected const HEADER_TENANT_IDENTIFIER = 'x-tenant-identifier';

/**
* Specification:
* - Returns an array of default headers to be used in HTTP requests.
* - The array keys represent the header names.
* - The array values represent the header values.
* - The default headers include a 'x-tenant-identifier' header that merges the shared headers.
* - If a header already exists in the request, it will not be overridden.
*
* @api
*
* @return array<string, string>
*/
public function getDefaultHeaders(): array
{
return array_merge($this->getSharedConfig()->getDefaultHeaders(), [
static::HEADER_TENANT_IDENTIFIER => $this->getTenantIdentifier(),
]);
}

/**
* @api
*
Expand Down
24 changes: 11 additions & 13 deletions src/Spryker/Client/KernelApp/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

class Request implements RequestInterface
{
/**
* @var array<\Spryker\Shared\KernelAppExtension\RequestExpanderPluginInterface>
*/
/**
* @var array<\Spryker\Shared\KernelAppExtension\RequestExpanderPluginInterface>
*/
protected array $requestExpanderPlugins;

protected KernelAppConfig $config;
Expand Down Expand Up @@ -72,16 +72,14 @@ protected function executeRequestExpanderPlugins(AcpHttpRequestTransfer $acpHttp
*/
protected function expandRequest(AcpHttpRequestTransfer $acpHttpRequestTransfer): AcpHttpRequestTransfer
{
return $this->addTenantIdentifier($acpHttpRequestTransfer);
}
foreach ($this->config->getDefaultHeaders() as $headerName => $headerValue) {
if (isset($acpHttpRequestTransfer->getHeaders()[$headerName])) {
continue;
}

/**
* @param \Generated\Shared\Transfer\AcpHttpRequestTransfer $acpHttpRequestTransfer
*
* @return \Generated\Shared\Transfer\AcpHttpRequestTransfer
*/
protected function addTenantIdentifier(AcpHttpRequestTransfer $acpHttpRequestTransfer): AcpHttpRequestTransfer
{
return $acpHttpRequestTransfer->addHeader('x-tenant-identifier', $this->config->getTenantIdentifier());
$acpHttpRequestTransfer->addHeader($headerName, $headerValue);
}

return $acpHttpRequestTransfer;
}
}
55 changes: 55 additions & 0 deletions src/Spryker/Shared/KernelApp/KernelAppConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Shared\KernelApp;

use Spryker\Shared\Kernel\AbstractBundleConfig;
use Symfony\Component\HttpFoundation\Request;

class KernelAppConfig extends AbstractBundleConfig
{
/**
* @var string
*/
protected const COOKIE_HEADER = 'Cookie';

/**
* @var string
*/
protected const CONTENT_TYPE_HEADER = 'Content-Type';

/**
* @var string
*/
protected const CONTENT_TYPE_HEADER_VALUE = 'application/json';

/**
* Specification:
* - Returns an array of default headers to be used in HTTP requests.
* - The array keys represent the header names.
* - The array values represent the header values.
* - The default headers include a 'Content-Type' header with the value 'application/json' and Cookie for zdebug session..
* - If a header already exists in the request, it will not be overridden.
*
* @api
*
* @return array<string, string>
*/
public function getDefaultHeaders(): array
{
$headers = [
static::CONTENT_TYPE_HEADER => static::CONTENT_TYPE_HEADER_VALUE,
];

$request = Request::createFromGlobals();
if ($request->cookies->has('XDEBUG_SESSION')) {
$headers[static::COOKIE_HEADER] = sprintf('XDEBUG_SESSION=%s', $request->cookies->get('XDEBUG_SESSION'));
}

return $headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<property name="method" type="string"/>
<property name="uri" type="string"/>
<property name="headers" type="array" associative="true" singular="header"/>
<property name="query" type="array" associative="true" singular="param"/>
<property name="body" type="string"/>
</transfer>

Expand Down
8 changes: 0 additions & 8 deletions src/Spryker/Zed/KernelApp/Business/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Generated\Shared\Transfer\AcpHttpResponseTransfer;
use Spryker\Client\KernelApp\KernelAppClientInterface;
use Spryker\Zed\KernelApp\KernelAppConfig;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

class Request implements RequestInterface
{
Expand Down Expand Up @@ -53,13 +52,6 @@ public function __construct(
public function request(AcpHttpRequestTransfer $acpHttpRequestTransfer): AcpHttpResponseTransfer
{
$acpHttpRequestTransfer = $this->executeRequestExpanderPlugins($acpHttpRequestTransfer);

$internalRequest = SymfonyRequest::createFromGlobals();

if ($internalRequest->cookies->has('XDEBUG_SESSION')) {
$acpHttpRequestTransfer->addHeader('Cookie', sprintf('XDEBUG_SESSION=%s', $internalRequest->cookies->get('XDEBUG_SESSION')));
}

$acpHttpRequestTransfer = $this->expandWithDefaultHeaders($acpHttpRequestTransfer);

return $this->kernelAppClient->request($acpHttpRequestTransfer);
Expand Down
17 changes: 4 additions & 13 deletions src/Spryker/Zed/KernelApp/KernelAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@

use Spryker\Zed\Kernel\AbstractBundleConfig;

/**
* @method \Spryker\Shared\KernelApp\KernelAppConfig getSharedConfig()
*/
class KernelAppConfig extends AbstractBundleConfig
{
/**
* @var string
*/
protected const CONTENT_TYPE_HEADER = 'Content-Type';

/**
* @var string
*/
protected const CONTENT_TYPE_HEADER_VALUE = 'application/json';

/**
* Specification:
* - Returns an array of default headers to be used in HTTP requests.
Expand All @@ -35,8 +28,6 @@ class KernelAppConfig extends AbstractBundleConfig
*/
public function getDefaultHeaders(): array
{
return [
static::CONTENT_TYPE_HEADER => static::CONTENT_TYPE_HEADER_VALUE,
];
return $this->getSharedConfig()->getDefaultHeaders();
}
}
38 changes: 30 additions & 8 deletions tests/SprykerTest/Client/KernelApp/KernelAppClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ class KernelAppClientTest extends Unit
protected KernelAppClientTester $tester;

/**
* @dataProvider requestTransferProvider
*
* @param \Generated\Shared\Transfer\AcpHttpRequestTransfer $acpHttpRequestTransfer
*
* @return void
*/
public function testRequestIsExpendedWithHeaderFromExpanderPluginBeforeRequestIsSend(): void
public function testRequestIsExpendedWithHeaderFromExpanderPluginBeforeRequestIsSend(AcpHttpRequestTransfer $acpHttpRequestTransfer): void
{
// Arrange
$kernelAppClient = new KernelAppClient();
Expand All @@ -52,10 +56,13 @@ public function testRequestIsExpendedWithHeaderFromExpanderPluginBeforeRequestIs

// Mock GuzzleClient to be able to introspect the RequestInterface and manipulate the returned response
$clientMock = Stub::make(Client::class, [
'send' => function (RequestInterface $request) use ($expectedXMas): ResponseInterface {
'send' => function (RequestInterface $request, $options) use ($expectedXMas, $acpHttpRequestTransfer): ResponseInterface {
$this->assertTrue($request->hasHeader('x-mas'));
$xMas = $request->getHeader('x-mas')[0];
$this->assertSame($expectedXMas, $xMas);
($acpHttpRequestTransfer->getMethod() === 'GET') ?
$this->assertNotEmpty($options['query']) :
$this->assertNotEmpty($acpHttpRequestTransfer->getBody());

return Stub::makeEmpty(ResponseInterface::class, [
'getBody' => function () {
Expand Down Expand Up @@ -101,12 +108,6 @@ public function expandRequest(AcpHttpRequestTransfer $acpHttpRequestTransfer): A
]);

// Act
$acpHttpRequestTransfer = new AcpHttpRequestTransfer();
$acpHttpRequestTransfer
->setMethod('POST')
->setUri('www.example.com')
->setBody('{"foo": "bar"}');

$acpHttpResponseTransfer = $kernelAppClient->request($acpHttpRequestTransfer);

// Assert
Expand Down Expand Up @@ -191,4 +192,25 @@ public function expandRequest(AcpHttpRequestTransfer $acpHttpRequestTransfer): A
$this->assertInstanceOf(AcpHttpResponseTransfer::class, $acpHttpResponseTransfer);
$this->assertSame(422, $acpHttpResponseTransfer->getHttpStatusCode());
}

/**
* @return array<\Generated\Shared\Transfer\AcpHttpRequestTransfer>
*/
protected function requestTransferProvider(): array
{
return [
[
(new AcpHttpRequestTransfer())
->setMethod('GET')
->setUri('www.example.com')
->setQuery(['foo' => 'bar']),
],
[
(new AcpHttpRequestTransfer())
->setMethod('POST')
->setUri('www.example.com')
->setBody('{"foo": "bar"}'),
],
];
}
}

0 comments on commit c09b9ed

Please sign in to comment.