Skip to content

Commit b6628c1

Browse files
authored
chore: rename recommendation client (#682)
* chore: deprecate RecommendationClient in favor of PersonalizationClient * chore: update config and tests * fix: linting * fix: rename method
1 parent a93663a commit b6628c1

File tree

6 files changed

+163
-2
lines changed

6 files changed

+163
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Config;
4+
5+
final class PersonalizationConfig extends AbstractConfig
6+
{
7+
/**
8+
* @param string|null $appId
9+
* @param string|null $apiKey
10+
* @param string|null $region
11+
*
12+
* @return PersonalizationConfig
13+
*/
14+
public static function create($appId = null, $apiKey = null, $region = null)
15+
{
16+
$config = [
17+
'appId' => null !== $appId ? $appId : getenv('ALGOLIA_APP_ID'),
18+
'apiKey' => null !== $apiKey ? $apiKey : getenv('ALGOLIA_API_KEY'),
19+
'region' => null !== $region ? $region : 'us',
20+
];
21+
22+
return new self($config);
23+
}
24+
25+
public function getRegion()
26+
{
27+
return $this->config['region'];
28+
}
29+
}

src/Config/RecommendationConfig.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Algolia\AlgoliaSearch\Config;
44

5+
/**
6+
* @deprecated Please use Algolia\AlgoliaSearch\Config\PersonalizationConfig instead
7+
*/
58
final class RecommendationConfig extends AbstractConfig
69
{
710
/**

src/PersonalizationClient.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch;
4+
5+
use Algolia\AlgoliaSearch\Config\PersonalizationConfig;
6+
use Algolia\AlgoliaSearch\RequestOptions\RequestOptions;
7+
use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper;
8+
use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts;
9+
10+
final class PersonalizationClient
11+
{
12+
/**
13+
* @var \Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper
14+
*/
15+
private $api;
16+
17+
/**
18+
* @var \Algolia\AlgoliaSearch\Config\PersonalizationConfig
19+
*/
20+
private $config;
21+
22+
/**
23+
* RecommendationClient constructor.
24+
*/
25+
public function __construct(ApiWrapper $api, PersonalizationConfig $config)
26+
{
27+
$this->api = $api;
28+
$this->config = $config;
29+
}
30+
31+
/**
32+
* @param string|null $appId
33+
* @param string|null $apiKey
34+
* @param string|null $region
35+
*
36+
* @return PersonalizationClient
37+
*/
38+
public static function create($appId = null, $apiKey = null, $region = null)
39+
{
40+
$config = PersonalizationConfig::create($appId, $apiKey, $region);
41+
42+
return static::createWithConfig($config);
43+
}
44+
45+
/**
46+
* @return PersonalizationClient
47+
*/
48+
public static function createWithConfig(PersonalizationConfig $config)
49+
{
50+
$config = clone $config;
51+
52+
if ($hosts = $config->getHosts()) {
53+
// If a list of hosts was passed, we ignore the cache
54+
$clusterHosts = ClusterHosts::create($hosts);
55+
} else {
56+
$clusterHosts = ClusterHosts::createForRecommendation($config->getRegion());
57+
}
58+
59+
$apiWrapper = new ApiWrapper(
60+
Algolia::getHttpClient(),
61+
$config,
62+
$clusterHosts
63+
);
64+
65+
return new self($apiWrapper, $config);
66+
}
67+
68+
/**
69+
* @param array<string, int|string|array>|RequestOptions $requestOptions
70+
*
71+
* @return array<string, int|array>
72+
*/
73+
public function getPersonalizationStrategy($requestOptions = [])
74+
{
75+
return $this->api->read('GET', api_path('/1/strategies/personalization'), $requestOptions);
76+
}
77+
78+
/**
79+
* @param array<string, int|array> $strategy
80+
* @param array<string, int|string|array>|RequestOptions $requestOptions
81+
*
82+
* @return array<string, int|string>
83+
*/
84+
public function setPersonalizationStrategy($strategy, $requestOptions = [])
85+
{
86+
return $this->api->write('POST', api_path('/1/strategies/personalization'), $strategy, $requestOptions);
87+
}
88+
}

src/RecommendationClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper;
88
use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts;
99

10+
/**
11+
* @deprecated Please use Algolia\AlgoliaSearch\PersonalizationClient instead
12+
*/
1013
final class RecommendationClient
1114
{
1215
/**

src/SearchClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public static function generateSecuredApiKey($parentApiKey, $restrictions)
265265

266266
/**
267267
* @deprecated endpoint will be deprecated
268-
* @see RecommendationClient
268+
* @see PersonalizationClient
269269
*/
270270
public function getPersonalizationStrategy($requestOptions = [])
271271
{
@@ -274,7 +274,7 @@ public function getPersonalizationStrategy($requestOptions = [])
274274

275275
/**
276276
* @deprecated endpoint will be deprecated
277-
* @see RecommendationClient
277+
* @see PersonalizationClient
278278
*/
279279
public function setPersonalizationStrategy($strategy, $requestOptions = [])
280280
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Tests\Integration;
4+
5+
use Algolia\AlgoliaSearch\PersonalizationClient;
6+
7+
class PersonalizationClientTest extends BaseTest
8+
{
9+
public function testPersonalizationClient()
10+
{
11+
$personalizationClient = PersonalizationClient::create(
12+
getenv('ALGOLIA_APPLICATION_ID_1'),
13+
getenv('ALGOLIA_ADMIN_KEY_1')
14+
);
15+
16+
$strategy = [
17+
'eventsScoring' => [
18+
['eventName' => 'Add to cart', 'eventType' => 'conversion', 'score' => 50],
19+
['eventName' => 'Purchase', 'eventType' => 'conversion', 'score' => 100],
20+
],
21+
'facetsScoring' => [
22+
['facetName' => 'brand', 'score' => 100],
23+
['facetName' => 'categories', 'score' => 10],
24+
],
25+
'personalizationImpact' => 0,
26+
];
27+
28+
try {
29+
$personalizationClient->setPersonalizationStrategy($strategy);
30+
} catch (\Exception $e) {
31+
$this->assertEquals(429, $e->getCode());
32+
}
33+
34+
$fetchedStrategy = $personalizationClient->getPersonalizationStrategy();
35+
36+
$this->assertEquals($strategy, $fetchedStrategy);
37+
}
38+
}

0 commit comments

Comments
 (0)