|
| 1 | +from typing import Optional, Union, List |
| 2 | + |
| 3 | +from algoliasearch.helpers import is_async_available |
| 4 | +from algoliasearch.http.request_options import RequestOptions |
| 5 | +from algoliasearch.http.verb import Verb |
| 6 | +from algoliasearch.configs import SearchConfig |
| 7 | +from algoliasearch.http.requester import Requester |
| 8 | +from algoliasearch.http.transporter import Transporter |
| 9 | + |
| 10 | + |
| 11 | +class RecommendClient(object): |
| 12 | + @property |
| 13 | + def app_id(self): |
| 14 | + # type: () -> str |
| 15 | + |
| 16 | + return self._config.app_id |
| 17 | + |
| 18 | + def __init__(self, transporter, search_config): |
| 19 | + # type: (Transporter, SearchConfig) -> None |
| 20 | + |
| 21 | + self._transporter = transporter |
| 22 | + self._config = search_config |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def create(app_id=None, api_key=None): |
| 26 | + # type: (Optional[str], Optional[str]) -> RecommendClient |
| 27 | + |
| 28 | + config = SearchConfig(app_id, api_key) |
| 29 | + |
| 30 | + return RecommendClient.create_with_config(config) |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def create_with_config(config): |
| 34 | + # type: (SearchConfig) -> RecommendClient |
| 35 | + |
| 36 | + requester = Requester() |
| 37 | + transporter = Transporter(requester, config) |
| 38 | + |
| 39 | + client = RecommendClient(transporter, config) |
| 40 | + |
| 41 | + if is_async_available(): |
| 42 | + from algoliasearch.recommend_client_async import RecommendClientAsync |
| 43 | + from algoliasearch.http.transporter_async import TransporterAsync |
| 44 | + from algoliasearch.http.requester_async import RequesterAsync |
| 45 | + |
| 46 | + return RecommendClientAsync( |
| 47 | + client, TransporterAsync(RequesterAsync(), config), config |
| 48 | + ) |
| 49 | + |
| 50 | + return client |
| 51 | + |
| 52 | + def get_recommendations(self, queries, request_options=None): |
| 53 | + # type: (List[dict], Optional[Union[dict, RequestOptions]]) -> dict |
| 54 | + |
| 55 | + for q in queries: |
| 56 | + if "threshold" not in q or q["threshold"] is None: |
| 57 | + q["threshold"] = 0 |
| 58 | + |
| 59 | + return self._transporter.read( |
| 60 | + Verb.POST, |
| 61 | + "1/indexes/*/recommendations", |
| 62 | + {"requests": queries}, |
| 63 | + request_options, |
| 64 | + ) |
| 65 | + |
| 66 | + def get_related_products(self, queries, request_options=None): |
| 67 | + # type: (List[dict], Optional[Union[dict, RequestOptions]]) -> dict |
| 68 | + |
| 69 | + for q in queries: |
| 70 | + q["model"] = "related-products" |
| 71 | + |
| 72 | + return self.get_recommendations(queries, request_options) |
| 73 | + |
| 74 | + def get_frequently_bought_together(self, queries, request_options=None): |
| 75 | + # type: (List[dict], Optional[Union[dict, RequestOptions]]) -> dict |
| 76 | + |
| 77 | + for q in queries: |
| 78 | + q.pop("fallbackParameters", None) |
| 79 | + q["model"] = "bought-together" |
| 80 | + |
| 81 | + return self.get_recommendations(queries, request_options) |
| 82 | + |
| 83 | + def close(self): |
| 84 | + # type: () -> None |
| 85 | + |
| 86 | + return self._transporter.close() # type: ignore |
| 87 | + |
| 88 | + def _sync(self): |
| 89 | + # type: () -> RecommendClient |
| 90 | + |
| 91 | + return self |
0 commit comments