Skip to content

Commit 880037b

Browse files
authored
chore(recommendation): rename RecommendationClient to PersonalizationClient (#536)
1 parent 91c60a3 commit 880037b

File tree

7 files changed

+247
-3
lines changed

7 files changed

+247
-3
lines changed

algoliasearch/configs.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,19 @@ def build_hosts(self):
124124
return HostsCollection(
125125
[Host("{}.{}.{}".format("recommendation", self._region, "algolia.com"))]
126126
)
127+
128+
129+
class PersonalizationConfig(Config):
130+
def __init__(self, app_id=None, api_key=None, region=None):
131+
# type: (Optional[str], Optional[str], Optional[str]) -> None
132+
133+
self._region = "us" if region is None else region
134+
135+
super(PersonalizationConfig, self).__init__(app_id, api_key)
136+
137+
def build_hosts(self):
138+
# type: () -> HostsCollection
139+
140+
return HostsCollection(
141+
[Host("{}.{}.{}".format("personalization", self._region, "algolia.com"))]
142+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from typing import Optional, Union
2+
3+
from algoliasearch.configs import PersonalizationConfig
4+
from algoliasearch.helpers import is_async_available
5+
from algoliasearch.http.request_options import RequestOptions
6+
from algoliasearch.http.requester import Requester
7+
from algoliasearch.http.transporter import Transporter
8+
from algoliasearch.http.verb import Verb
9+
10+
11+
class PersonalizationClient(object):
12+
def __init__(self, transporter, config):
13+
# type: (Transporter, PersonalizationConfig) -> None
14+
15+
self._transporter = transporter
16+
self._config = config
17+
18+
@staticmethod
19+
def create(app_id=None, api_key=None, region=None):
20+
# type: (Optional[str], Optional[str], Optional[str]) -> PersonalizationClient # noqa: E501
21+
22+
config = PersonalizationConfig(app_id, api_key, region)
23+
24+
return PersonalizationClient.create_with_config(config)
25+
26+
@staticmethod
27+
def create_with_config(config):
28+
# type: (PersonalizationConfig) -> PersonalizationClient
29+
30+
requester = Requester()
31+
transporter = Transporter(requester, config)
32+
33+
client = PersonalizationClient(transporter, config)
34+
35+
if is_async_available():
36+
from algoliasearch.personalization_client_async import (
37+
PersonalizationClientAsync,
38+
)
39+
from algoliasearch.http.transporter_async import TransporterAsync
40+
from algoliasearch.http.requester_async import RequesterAsync
41+
42+
return PersonalizationClientAsync(
43+
client, TransporterAsync(RequesterAsync(), config), config
44+
)
45+
46+
return client
47+
48+
def set_personalization_strategy(
49+
self, personalization_strategy, request_options=None
50+
): # noqa: E501
51+
# type: (dict, Optional[Union[dict, RequestOptions]]) -> dict
52+
53+
return self._transporter.write(
54+
Verb.POST,
55+
"1/strategies/personalization",
56+
personalization_strategy,
57+
request_options,
58+
)
59+
60+
def get_personalization_strategy(self, request_options=None):
61+
# type: (Optional[Union[dict, RequestOptions]]) -> dict
62+
63+
return self._transporter.read(
64+
Verb.GET, "1/strategies/personalization", None, request_options
65+
)
66+
67+
def close(self):
68+
# type: () -> None
69+
70+
return self._transporter.close() # type: ignore
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import types
2+
import asyncio
3+
from typing import Optional, Type
4+
5+
from algoliasearch.personalization_client import PersonalizationClient
6+
from algoliasearch.configs import PersonalizationConfig
7+
from algoliasearch.helpers_async import _create_async_methods_in
8+
from algoliasearch.http.transporter_async import TransporterAsync
9+
10+
11+
class PersonalizationClientAsync(PersonalizationClient):
12+
def __init__(self, personalization_client, transporter, search_config):
13+
# type: (PersonalizationClient, TransporterAsync, PersonalizationConfig) -> None # noqa: E501
14+
15+
self._transporter_async = transporter
16+
17+
super(PersonalizationClientAsync, self).__init__(
18+
personalization_client._transporter, search_config
19+
)
20+
21+
client = PersonalizationClient(transporter, search_config)
22+
23+
_create_async_methods_in(self, client)
24+
25+
@asyncio.coroutine
26+
def __aenter__(self):
27+
# type: () -> PersonalizationClientAsync # type: ignore
28+
29+
return self # type: ignore
30+
31+
@asyncio.coroutine
32+
def __aexit__(self, exc_type, exc, tb): # type: ignore
33+
# type: (Optional[Type[BaseException]], Optional[BaseException],Optional[types.TracebackType]) -> None # noqa: E501
34+
35+
yield from self.close_async() # type: ignore
36+
37+
@asyncio.coroutine
38+
def close_async(self): # type: ignore
39+
# type: () -> None
40+
41+
super().close()
42+
43+
yield from self._transporter_async.close() # type: ignore

algoliasearch/recommendation_client.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Optional, Union, Dict, Any
1+
import warnings
2+
from typing import Optional, Union
23

34
from algoliasearch.configs import RecommendationConfig
45
from algoliasearch.helpers import is_async_available
@@ -12,13 +13,25 @@ class RecommendationClient(object):
1213
def __init__(self, transporter, config):
1314
# type: (Transporter, RecommendationConfig) -> None
1415

16+
warnings.warn(
17+
"`%s.%s` is deprecated, use `%s.%s` instead."
18+
% ("RecommendationClient", "init", "PersonalizationClient", "init",),
19+
DeprecationWarning,
20+
)
21+
1522
self._transporter = transporter
1623
self._config = config
1724

1825
@staticmethod
1926
def create(app_id=None, api_key=None, region=None):
2027
# type: (Optional[str], Optional[str], Optional[str]) -> RecommendationClient # noqa: E501
2128

29+
warnings.warn(
30+
"`%s.%s` is deprecated, use `%s.%s` instead."
31+
% ("RecommendationClient", "create", "PersonalizationClient", "create",),
32+
DeprecationWarning,
33+
)
34+
2235
config = RecommendationConfig(app_id, api_key, region)
2336

2437
return RecommendationClient.create_with_config(config)
@@ -27,6 +40,17 @@ def create(app_id=None, api_key=None, region=None):
2740
def create_with_config(config):
2841
# type: (RecommendationConfig) -> RecommendationClient
2942

43+
warnings.warn(
44+
"`%s.%s` is deprecated, use `%s.%s` instead."
45+
% (
46+
"RecommendationClient",
47+
"create_with_config",
48+
"PersonalizationClient",
49+
"create_with_config",
50+
),
51+
DeprecationWarning,
52+
)
53+
3054
requester = Requester()
3155
transporter = Transporter(requester, config)
3256

@@ -50,6 +74,17 @@ def set_personalization_strategy(
5074
): # noqa: E501
5175
# type: (dict, Optional[Union[dict, RequestOptions]]) -> dict
5276

77+
warnings.warn(
78+
"`%s.%s` is deprecated, use `%s.%s` instead."
79+
% (
80+
"RecommendationClient",
81+
"set_personalization_strategy",
82+
"PersonalizationClient",
83+
"set_personalization_strategy",
84+
),
85+
DeprecationWarning,
86+
)
87+
5388
return self._transporter.write(
5489
Verb.POST,
5590
"1/strategies/personalization",
@@ -60,11 +95,28 @@ def set_personalization_strategy(
6095
def get_personalization_strategy(self, request_options=None):
6196
# type: (Optional[Union[dict, RequestOptions]]) -> dict
6297

98+
warnings.warn(
99+
"`%s.%s` is deprecated, use `%s.%s` instead."
100+
% (
101+
"RecommendationClient",
102+
"get_personalization_strategy",
103+
"PersonalizationClient",
104+
"get_personalization_strategy",
105+
),
106+
DeprecationWarning,
107+
)
108+
63109
return self._transporter.read(
64110
Verb.GET, "1/strategies/personalization", None, request_options
65111
)
66112

67113
def close(self):
68114
# type: () -> None
69115

116+
warnings.warn(
117+
"`%s.%s` is deprecated, use `%s.%s` instead."
118+
% ("RecommendationClient", "close", "PersonalizationClient", "close",),
119+
DeprecationWarning,
120+
)
121+
70122
return self._transporter.close() # type: ignore

algoliasearch/search_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def set_personalization_strategy(self, strategy, request_options=None):
342342
% (
343343
"SearchClient",
344344
"set_personalization_strategy",
345-
"RecommendationClient",
345+
"PersonalizationClient",
346346
"set_personalization_strategy",
347347
),
348348
DeprecationWarning,
@@ -363,7 +363,7 @@ def get_personalization_strategy(self, request_options=None):
363363
% (
364364
"SearchClient",
365365
"get_personalization_strategy",
366-
"RecommendationClient",
366+
"PersonalizationClient",
367367
"get_personalization_strategy",
368368
),
369369
DeprecationWarning,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
3+
from tests.helpers.env import Env
4+
from tests.helpers.factory import Factory as F
5+
from algoliasearch.exceptions import RequestException
6+
7+
8+
class TestPersonalizationClient(unittest.TestCase):
9+
def setUp(self):
10+
self.client = F.personalization_client()
11+
12+
def tearDown(self):
13+
self.client.close()
14+
15+
@unittest.skipIf(
16+
Env.is_community(), "Community can not test personalization operations"
17+
)
18+
def test_personalization(self):
19+
personalization_strategy = {
20+
"eventsScoring": [
21+
{"eventName": "Add to cart", "eventType": "conversion", "score": 50},
22+
{"eventName": "Purchase", "eventType": "conversion", "score": 100},
23+
],
24+
"facetsScoring": [
25+
{"facetName": "brand", "score": 100},
26+
{"facetName": "categories", "score": 10},
27+
],
28+
"personalizationImpact": 0,
29+
}
30+
31+
try:
32+
response = self.client.set_personalization_strategy(
33+
personalization_strategy
34+
)
35+
self.assertEqual(
36+
response,
37+
{"status": 200, "message": "Strategy was successfully updated"},
38+
)
39+
except RequestException as err:
40+
self.assertEqual(
41+
err,
42+
RequestException("Number of strategy saves exceeded for the day", 429),
43+
) # noqa: E501
44+
45+
response = self.client.get_personalization_strategy()
46+
self.assertEqual(response, personalization_strategy)

tests/helpers/factory.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
import datetime
23
import os
34
import platform
@@ -11,6 +12,7 @@
1112
from algoliasearch.insights_client import InsightsClient
1213
from algoliasearch.search_client import SearchClient, SearchConfig
1314
from algoliasearch.recommendation_client import RecommendationClient
15+
from algoliasearch.personalization_client import PersonalizationClient
1416
from algoliasearch.http.hosts import HostsCollection, Host
1517
from faker import Faker
1618

@@ -82,11 +84,26 @@ def analytics_client(app_id=None, api_key=None):
8284
def recommendation_client(app_id=None, api_key=None):
8385
# type: (Optional[str], Optional[str]) -> RecommendationClient
8486

87+
warnings.warn(
88+
"`%s` is deprecated, use `%s` instead."
89+
% ("RecommendationClient", "PersonalizationClient",),
90+
DeprecationWarning,
91+
)
92+
8593
app_id = app_id if app_id is not None else Factory.get_app_id()
8694
api_key = api_key if api_key is not None else Factory.get_api_key()
8795

8896
return Factory.decide(RecommendationClient.create(app_id, api_key))
8997

98+
@staticmethod
99+
def personalization_client(app_id=None, api_key=None):
100+
# type: (Optional[str], Optional[str]) -> PersonalizationClient
101+
102+
app_id = app_id if app_id is not None else Factory.get_app_id()
103+
api_key = api_key if api_key is not None else Factory.get_api_key()
104+
105+
return Factory.decide(PersonalizationClient.create(app_id, api_key))
106+
90107
@staticmethod
91108
def insights_client(app_id=None, api_key=None):
92109
# type: (Optional[str], Optional[str]) -> InsightsClient

0 commit comments

Comments
 (0)