From 696281a54633b11bda0f582700b60951bdada69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Ludvik?= Date: Tue, 23 Jan 2024 16:25:16 +0100 Subject: [PATCH 1/2] added personalizing parameters to search queries in API --- src/Component/LuigisBox/LuigisBoxClient.php | 8 +++++++- src/Model/Batch/LuigisBoxBatchLoadData.php | 10 ++++++++++ src/Model/Batch/LuigisBoxBatchLoadDataFactory.php | 6 ++++-- src/Model/Batch/LuigisBoxBatchLoader.php | 3 ++- src/Model/Product/ProductSearchResultsProvider.php | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Component/LuigisBox/LuigisBoxClient.php b/src/Component/LuigisBox/LuigisBoxClient.php index 9a83217..33c2a7a 100644 --- a/src/Component/LuigisBox/LuigisBoxClient.php +++ b/src/Component/LuigisBox/LuigisBoxClient.php @@ -58,6 +58,7 @@ protected function getTrackerId(): string * @param int $page * @param array $limitsByType * @param array $filter + * @param string $userIdentifier * @param string|null $orderingMode * @return \Shopsys\LuigisBoxBundle\Component\LuigisBox\LuigisBoxResult[] */ @@ -67,6 +68,7 @@ public function getData( int $page, array $limitsByType, array $filter, + string $userIdentifier, ?string $orderingMode, ): array { $this->checkNecessaryConfigurationIsSet(); @@ -80,6 +82,7 @@ public function getData( $page, $limitsByType, $filter, + $userIdentifier, $orderingMode, ), ), @@ -115,6 +118,7 @@ protected function getTotalHitsFromData(array $data, string $action): int * @param int $page * @param array $limitsByType * @param array $filter + * @param string $userIdentifier * @param string|null $orderingMode * @return string */ @@ -124,13 +128,15 @@ protected function getLuigisBoxApiUrl( int $page, array $limitsByType, array $filter, + string $userIdentifier, ?string $orderingMode, ): string { $url = $this->luigisBoxApiUrl . $action . '/' . '?tracker_id=' . $this->getTrackerId() . '&q=' . urlencode($query) . - '&hit_fields=url'; + '&hit_fields=url' . + '&user_id=' . $userIdentifier; if ($action === self::ACTION_SEARCH) { $quicksearchTypesWithLimits = $this->getQuicksearchTypesWithLimits($limitsByType); diff --git a/src/Model/Batch/LuigisBoxBatchLoadData.php b/src/Model/Batch/LuigisBoxBatchLoadData.php index c7ac9c7..59f109d 100644 --- a/src/Model/Batch/LuigisBoxBatchLoadData.php +++ b/src/Model/Batch/LuigisBoxBatchLoadData.php @@ -13,6 +13,7 @@ class LuigisBoxBatchLoadData * @param string $endpoint * @param int $page * @param array $filter + * @param string $userIdentifier * @param string|null $orderingMode */ public function __construct( @@ -22,6 +23,7 @@ public function __construct( protected readonly string $endpoint, protected readonly int $page, protected readonly array $filter, + protected readonly string $userIdentifier, protected readonly ?string $orderingMode = null, ) { } @@ -81,4 +83,12 @@ public function getOrderingMode(): ?string { return $this->orderingMode; } + + /** + * @return string + */ + public function getUserIdentifier(): string + { + return $this->userIdentifier; + } } diff --git a/src/Model/Batch/LuigisBoxBatchLoadDataFactory.php b/src/Model/Batch/LuigisBoxBatchLoadDataFactory.php index a8bec19..33fdac5 100644 --- a/src/Model/Batch/LuigisBoxBatchLoadDataFactory.php +++ b/src/Model/Batch/LuigisBoxBatchLoadDataFactory.php @@ -37,9 +37,10 @@ public function create( $luigisBoxFilter = $this->productFilterToLuigisBoxFilterMapper->mapOnlyType($type); } - $search = $argument['search'] ?? ''; + $search = $argument['searchInput']['search'] ?? ''; $orderingMode = $argument['orderingMode']; - $endpoint = $argument['isAutocomplete'] === true ? LuigisBoxClient::ACTION_AUTOCOMPLETE : LuigisBoxClient::ACTION_SEARCH; + $endpoint = $argument['searchInput']['isAutocomplete'] === true ? LuigisBoxClient::ACTION_AUTOCOMPLETE : LuigisBoxClient::ACTION_SEARCH; + $userIdentifier = $argument['searchInput']['userIdentifier']; return new LuigisBoxBatchLoadData( $type, @@ -48,6 +49,7 @@ public function create( $endpoint, $page, $luigisBoxFilter, + $userIdentifier, $orderingMode, ); } diff --git a/src/Model/Batch/LuigisBoxBatchLoader.php b/src/Model/Batch/LuigisBoxBatchLoader.php index 0ed2c73..5443576 100644 --- a/src/Model/Batch/LuigisBoxBatchLoader.php +++ b/src/Model/Batch/LuigisBoxBatchLoader.php @@ -65,6 +65,7 @@ public function loadByBatchData(array $luigisBoxBatchLoadData): Promise $page = $mainBatchLoadData->getPage(); $productFilter = $mainBatchLoadData->getFilter(); $productOrderingMode = $mainBatchLoadData->getOrderingMode(); + $userIdentifier = $mainBatchLoadData->getUserIdentifier(); $limitsByType = []; foreach ($luigisBoxBatchLoadData as $luigisBoxBatchLoadDataItem) { @@ -72,7 +73,7 @@ public function loadByBatchData(array $luigisBoxBatchLoadData): Promise } return $this->promiseAdapter->all($this->mapDataByTypes( - $this->luigisBoxClient->getData($query, $endpoint, $page, $limitsByType, $productFilter, $productOrderingMode), + $this->luigisBoxClient->getData($query, $endpoint, $page, $limitsByType, $productFilter, $userIdentifier, $productOrderingMode), $limitsByType, )); } diff --git a/src/Model/Product/ProductSearchResultsProvider.php b/src/Model/Product/ProductSearchResultsProvider.php index 094e017..b10a5f3 100644 --- a/src/Model/Product/ProductSearchResultsProvider.php +++ b/src/Model/Product/ProductSearchResultsProvider.php @@ -57,7 +57,7 @@ public function getProductsSearchResults( Argument $argument, ProductFilterData $productFilterData, ): Promise { - $search = $argument['search'] ?? ''; + $search = $argument['searchInput']['search'] ?? ''; $orderingMode = $argument['orderingMode']; $luigisBoxFilter = $this->productFilterToLuigisBoxFilterMapper->map(LuigisBoxClient::TYPE_IN_LUIGIS_BOX_PRODUCT, $productFilterData, $this->domain); From da316428f6db931ed189dcf59613078e31a81e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Ludvik?= Date: Thu, 7 Mar 2024 10:07:03 +0100 Subject: [PATCH 2/2] added catching and logging of bad requests to LuigisBoxClient --- src/Component/LuigisBox/LuigisBoxClient.php | 67 ++++++++++++++++----- src/Resources/config/services.yaml | 1 + 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/Component/LuigisBox/LuigisBoxClient.php b/src/Component/LuigisBox/LuigisBoxClient.php index 33c2a7a..43a9007 100644 --- a/src/Component/LuigisBox/LuigisBoxClient.php +++ b/src/Component/LuigisBox/LuigisBoxClient.php @@ -12,6 +12,8 @@ use Shopsys\LuigisBoxBundle\Component\LuigisBox\Exception\LuigisBoxActionNotRecognizedException; use Shopsys\LuigisBoxBundle\Component\LuigisBox\Exception\LuigisBoxIndexNotRecognizedException; use Shopsys\ProductFeed\LuigisBoxBundle\Model\FeedItem\LuigisBoxProductFeedItem; +use Symfony\Bridge\Monolog\Logger; +use Throwable; class LuigisBoxClient { @@ -27,11 +29,13 @@ class LuigisBoxClient * @param string $luigisBoxApiUrl * @param array $trackerIdsByDomainIds * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain + * @param \Symfony\Bridge\Monolog\Logger $logger */ public function __construct( protected readonly string $luigisBoxApiUrl, protected readonly array $trackerIdsByDomainIds, protected readonly Domain $domain, + protected readonly Logger $logger, ) { } @@ -74,22 +78,40 @@ public function getData( $this->checkNecessaryConfigurationIsSet(); $this->validateActionIsValid($action); - $data = json_decode( - file_get_contents( - $this->getLuigisBoxApiUrl( - $query, - $action, - $page, - $limitsByType, - $filter, - $userIdentifier, - $orderingMode, + try { + $data = json_decode( + file_get_contents( + $this->getLuigisBoxApiUrl( + $query, + $action, + $page, + $limitsByType, + $filter, + $userIdentifier, + $orderingMode, + ), ), - ), - true, - 512, - JSON_THROW_ON_ERROR, - ); + true, + 512, + JSON_THROW_ON_ERROR, + ); + } catch (Throwable $e) { + $this->logger->error( + 'Luigi\'s Box API request failed.', + [ + 'exception' => $e, + 'query' => $query, + 'action' => $action, + 'page' => $page, + 'limitsByType' => $limitsByType, + 'filter' => $filter, + 'userIdentifier' => $userIdentifier, + 'orderingMode' => $orderingMode, + ], + ); + + return $this->getEmptyResults(array_keys($limitsByType)); + } if ($action === self::ACTION_SEARCH) { $data = $data['results']; @@ -98,6 +120,21 @@ public function getData( return $this->getResultsIndexedByItemType($data, $action, array_keys($limitsByType)); } + /** + * @param string[] $types + * @return \Shopsys\LuigisBoxBundle\Component\LuigisBox\LuigisBoxResult[] + */ + protected function getEmptyResults(array $types): array + { + $resultsByType = []; + + foreach ($types as $type) { + $resultsByType[$type] = new LuigisBoxResult([], [], 0); + } + + return $resultsByType; + } + /** * @param array $data * @param string $action diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 9472536..f0452c1 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -13,6 +13,7 @@ services: arguments: $luigisBoxApiUrl: '%env(string:LUIGIS_BOX_API_URL)%' $trackerIdsByDomainIds: '%env(json:LUIGIS_BOX_TRACKER_IDS_BY_DOMAIN_IDS)%' + $logger: '@monolog.logger' Shopsys\LuigisBoxBundle\Model\Product\ProductSearchResultsProvider: arguments: