-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(recommendation): rename
RecommendationClient
to `Personalizat…
…ionClient`
- Loading branch information
Showing
7 changed files
with
247 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from typing import Optional, Union | ||
|
||
from algoliasearch.configs import PersonalizationConfig | ||
from algoliasearch.helpers import is_async_available | ||
from algoliasearch.http.request_options import RequestOptions | ||
from algoliasearch.http.requester import Requester | ||
from algoliasearch.http.transporter import Transporter | ||
from algoliasearch.http.verb import Verb | ||
|
||
|
||
class PersonalizationClient(object): | ||
def __init__(self, transporter, config): | ||
# type: (Transporter, PersonalizationConfig) -> None | ||
|
||
self._transporter = transporter | ||
self._config = config | ||
|
||
@staticmethod | ||
def create(app_id=None, api_key=None, region=None): | ||
# type: (Optional[str], Optional[str], Optional[str]) -> PersonalizationClient # noqa: E501 | ||
|
||
config = PersonalizationConfig(app_id, api_key, region) | ||
|
||
return PersonalizationClient.create_with_config(config) | ||
|
||
@staticmethod | ||
def create_with_config(config): | ||
# type: (PersonalizationConfig) -> PersonalizationClient | ||
|
||
requester = Requester() | ||
transporter = Transporter(requester, config) | ||
|
||
client = PersonalizationClient(transporter, config) | ||
|
||
if is_async_available(): | ||
from algoliasearch.personalization_client_async import ( | ||
PersonalizationClientAsync, | ||
) | ||
from algoliasearch.http.transporter_async import TransporterAsync | ||
from algoliasearch.http.requester_async import RequesterAsync | ||
|
||
return PersonalizationClientAsync( | ||
client, TransporterAsync(RequesterAsync(), config), config | ||
) | ||
|
||
return client | ||
|
||
def set_personalization_strategy( | ||
self, personalization_strategy, request_options=None | ||
): # noqa: E501 | ||
# type: (dict, Optional[Union[dict, RequestOptions]]) -> dict | ||
|
||
return self._transporter.write( | ||
Verb.POST, | ||
"1/strategies/personalization", | ||
personalization_strategy, | ||
request_options, | ||
) | ||
|
||
def get_personalization_strategy(self, request_options=None): | ||
# type: (Optional[Union[dict, RequestOptions]]) -> dict | ||
|
||
return self._transporter.read( | ||
Verb.GET, "1/strategies/personalization", None, request_options | ||
) | ||
|
||
def close(self): | ||
# type: () -> None | ||
|
||
return self._transporter.close() # type: ignore |
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,43 @@ | ||
import types | ||
import asyncio | ||
from typing import Optional, Type | ||
|
||
from algoliasearch.personalization_client import PersonalizationClient | ||
from algoliasearch.configs import PersonalizationConfig | ||
from algoliasearch.helpers_async import _create_async_methods_in | ||
from algoliasearch.http.transporter_async import TransporterAsync | ||
|
||
|
||
class PersonalizationClientAsync(PersonalizationClient): | ||
def __init__(self, personalization_client, transporter, search_config): | ||
# type: (PersonalizationClient, TransporterAsync, PersonalizationConfig) -> None # noqa: E501 | ||
|
||
self._transporter_async = transporter | ||
|
||
super(PersonalizationClientAsync, self).__init__( | ||
personalization_client._transporter, search_config | ||
) | ||
|
||
client = PersonalizationClient(transporter, search_config) | ||
|
||
_create_async_methods_in(self, client) | ||
|
||
@asyncio.coroutine | ||
def __aenter__(self): | ||
# type: () -> PersonalizationClientAsync # type: ignore | ||
|
||
return self # type: ignore | ||
|
||
@asyncio.coroutine | ||
def __aexit__(self, exc_type, exc, tb): # type: ignore | ||
# type: (Optional[Type[BaseException]], Optional[BaseException],Optional[types.TracebackType]) -> None # noqa: E501 | ||
|
||
yield from self.close_async() # type: ignore | ||
|
||
@asyncio.coroutine | ||
def close_async(self): # type: ignore | ||
# type: () -> None | ||
|
||
super().close() | ||
|
||
yield from self._transporter_async.close() # type: ignore |
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,46 @@ | ||
import unittest | ||
|
||
from tests.helpers.env import Env | ||
from tests.helpers.factory import Factory as F | ||
from algoliasearch.exceptions import RequestException | ||
|
||
|
||
class TestPersonalizationClient(unittest.TestCase): | ||
def setUp(self): | ||
self.client = F.personalization_client() | ||
|
||
def tearDown(self): | ||
self.client.close() | ||
|
||
@unittest.skipIf( | ||
Env.is_community(), "Community can not test personalization operations" | ||
) | ||
def test_personalization(self): | ||
personalization_strategy = { | ||
"eventsScoring": [ | ||
{"eventName": "Add to cart", "eventType": "conversion", "score": 50}, | ||
{"eventName": "Purchase", "eventType": "conversion", "score": 100}, | ||
], | ||
"facetsScoring": [ | ||
{"facetName": "brand", "score": 100}, | ||
{"facetName": "categories", "score": 10}, | ||
], | ||
"personalizationImpact": 0, | ||
} | ||
|
||
try: | ||
response = self.client.set_personalization_strategy( | ||
personalization_strategy | ||
) | ||
self.assertEqual( | ||
response, | ||
{"status": 200, "message": "Strategy was successfully updated"}, | ||
) | ||
except RequestException as err: | ||
self.assertEqual( | ||
err, | ||
RequestException("Number of strategy saves exceeded for the day", 429), | ||
) # noqa: E501 | ||
|
||
response = self.client.get_personalization_strategy() | ||
self.assertEqual(response, personalization_strategy) |
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