Ejemplo n.º 1
0
def add_to_cart(hashid: str, index_name: str, session_id: str, item_id: str,
                amount: int, title: str, price: float, **opts):
    """
    Adds an item to the cart, or creates one if it does not exists.
    If the item is already present in the cart, the amount is added to the item current amount.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
        index_name: The index used for this product in Doofinder.
        item_id: The ID of the item to be added (this refers to the ID in the shop database).
        amount: Amount of items to add to the cart.
        title: Item title.
        price: Item price.
    """
    query_params = {
        'index': index_name,
        'id': item_id,
        'amount': amount,
        'title': title,
        'price': price
    }

    api_client = SearchAPIClient(**opts)
    return api_client.put(f'/6/{hashid}/stats/cart/{session_id}',
                          query_params=query_params)
Ejemplo n.º 2
0
def click_stats(hashid: str,
                dfid: str,
                session_id: str,
                query: Optional[str] = None,
                **opts):
    """
    Save click event on Doofinder statistics

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        dfid: Doofinder item id. It comes in every Doofinder results for every item.
            It has the form {hashid}@{index}@{md5id}
            i.e. 6a96504dc173514cab1e0198af92e6e9@product@e19347e1c3ca0c0b97de5fb3b690855a
        session_id (<= 32 characters): The current session ID, must be unique for each user.
        query (<= 200 characters): The search term. It must be escaped.
    """

    query_params = parse_query_params({
        'dfid': dfid,
        'session_id': session_id,
        'query': query
    })

    api_client = SearchAPIClient(**opts)
    return api_client.put(f'/6/{hashid}/stats/click',
                          query_params=query_params)
Ejemplo n.º 3
0
def suggest(hashid: str, query: str = '', indices: List[str] = None, stats: bool = None, session_id: str = None, **opts):
    """
    Fetch suggestions for terms based on the items indexed in a search engine.

    Args:
        hashid (str): Unique search engine id. Indicates to which search engine
            we are doing the query.
        query (str): The terms we are looking for in the items of the search engine. Default to ''.
        indices (List[str], optional): With the indices parameter you can specify to search within one specific Index.
            If this parameter is not provided, the search will work with all Indices.
            Note that [ and ] characters should be escaped (%5B and %5D) in all cases.
            Example: indices[]=product&indices[]=page; indices=products
        stats (boolean, optional): Enable/Disable this search in stats reports.
            Default: None.
        session_id (str, optional, <= 32 characters): The current session ID, must be unique for each user.
    """
    query_params = parse_query_params({
        'query': query,
        'indices': indices,
        'stats': stats,
        'session_id': session_id
    })
    api_client = SearchAPIClient(**opts)
    return api_client.get(
        f'/6/{hashid}/_suggest',
        query_params=query_params
    )
Ejemplo n.º 4
0
def clear_cart(hashid: str, session_id: str, **opts):
    """
    Deletes the cart with all its content.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
    """
    api_client = SearchAPIClient(**opts)
    return api_client.delete(f'/6/{hashid}/stats/cart/{session_id}')
Ejemplo n.º 5
0
    def test_raise(self, status_code, expected_exception, requests_mock):
        response_mock = mock.Mock()
        response_mock.status_code = status_code
        response_mock.json.return_value = {'error': "Error message"}
        session_mock = mock.Mock()
        session_mock.request.return_value = response_mock
        requests_mock.Session.return_value = session_mock

        api_client = SearchAPIClient()
        with self.assertRaises(expected_exception) as cm:
            api_client.request('GET', url='/')

        self.assertEqual(cm.exception.message, 'Error message')
Ejemplo n.º 6
0
def init_session(hashid: str, session_id: str, **opts):
    """
    Returns number of total unique search sessions in a period group by date.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
    """
    query_params = parse_query_params({
        'session_id': session_id,
    })

    api_client = SearchAPIClient(**opts)
    return api_client.put(f'/6/{hashid}/stats/init', query_params=query_params)
Ejemplo n.º 7
0
def log_checkout(hashid: str, session_id: str, **opts):
    """
    That is the event for when a customer complete a checkout.
    The info of the items that were in her session when she completes the checkout are logged in the event.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
    """
    query_params = parse_query_params({'session_id': session_id})

    api_client = SearchAPIClient(**opts)
    return api_client.put(f'/6/{hashid}/stats/checkout',
                          query_params=query_params)
Ejemplo n.º 8
0
    def test_can_set_per_request_host(self, requests_mock):
        response_mock = mock.Mock()
        response_mock.status_code = 200
        response_mock.text = 'OK'
        session_mock = mock.Mock()
        session_mock.request.return_value = response_mock
        requests_mock.Session.return_value = session_mock

        pydoof.search_url = 'https://eu1-search.doofinder.com'
        api_client = SearchAPIClient(search_url='http://localhost:8000')
        api_client.request('GET', url='/')

        session_mock.request.assert_called_with('GET',
                                                url='http://localhost:8000/',
                                                params=mock.ANY,
                                                json=mock.ANY,
                                                headers=mock.ANY,
                                                auth=mock.ANY)
Ejemplo n.º 9
0
def remove_from_cart(hashid: str, index_name: str, session_id: str,
                     item_id: str, amount: int, **opts):
    """
    Removes amount from the given item in the cart, and deletes if the result
    is lower than 0.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
        index_name: The index used for this product in Doofinder.
        item_id: The ID of the item to be added (this refers to the ID in the shop database).
        amount: Amount of items to add to the cart.
    """
    query_params = {'index': index_name, 'id': item_id, 'amount': amount}

    api_client = SearchAPIClient(**opts)
    return api_client.patch(f'/6/{hashid}/stats/cart/{session_id}',
                            query_params=query_params)
Ejemplo n.º 10
0
def log_banner_image_click(hashid: str,
                           id: str,
                           session_id: str,
                           query: Optional[str] = None,
                           **opts):
    """
    Logs a "click on banner image" event in stats logs.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        id: id of image displayed in banner. This id is obtained in the search response that has banner information.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
        query (<= 200 characters): The search term. It must be escaped.
    """
    query_params = parse_query_params({
        'id': id,
        'session_id': session_id,
        'query': query
    })

    api_client = SearchAPIClient(**opts)
    return api_client.put(f'/6/{hashid}/stats/image',
                          query_params=query_params)
Ejemplo n.º 11
0
def log_redirect(hashid: str,
                 redirection_id: str,
                 session_id: str,
                 query: Optional[str] = None,
                 **opts):
    """
    Logs a "redirection triggered" event in stats logs.

    Args:
        hashid: Unique search engine id. Indicates to which search engine we are doing the query.
        redirection_id: Id of redirection. This id is obtained in the search response that has redirect information.
        session_id (<= 32 characters): The current session ID, must be unique for each user.
        query (<= 200 characters): The search term. It must be escaped.
    """
    query_params = parse_query_params({
        'id': redirection_id,
        'session_id': session_id,
        'query': query
    })

    api_client = SearchAPIClient(**opts)
    return api_client.put(f'/6/{hashid}/stats/redirect',
                          query_params=query_params)
Ejemplo n.º 12
0
def refresh_config(cls, hashid, **opts):
    api_client = SearchAPIClient(**opts)
    api_client.post(
        f'/cache/{hashid}/refresh'
    )
Ejemplo n.º 13
0
def query(hashid: str, query: str = '', auto_filters: bool = None, custom_results: bool = None,
          excluded_results: bool = None, filter: Dict[str, Any] = None,
          exclude: Dict[str, Any] = None,
          indices: List[str] = None, query_name: QueryNames = None,
          sort: List[Dict[str, str]] = None, page: int = None, rpp: int = None,
          facets: List[Dict[str, Any]] = None, filter_execution: SearchFilterExecution = None,
          session_id: str = None, stats: bool = None, skip_auto_filters: List[str] = None,
          skip_top_facet: List[str] = None, title_facet: bool = None, top_facet: bool = None, **opts):
    """
    Queries items indexed in a search engine.

    Args:
        hashid (str): Unique search engine id. Indicates to which search engine
            we are doing the query.
        query (str): The terms we are looking for in the items of the search engine. Default to ''
        auto_filters (boolean, optional): Enable/Disable the automatic filters in search.
            Default: None.
        custom_results (boolean, optional): Enable/Disable the custom results in search.
            Default: None.
        excluded_results (boolean, optional): Enable/Disable the excluded results in search.
            Default: None.
        filter (dict, optional): A dictionary that indicates a filter for
            items. For instance, look for those items of color "blue".
            Default to None.
        exclude (dict, optional): A dictionary that indicates an exclude rule
            for items. For instance, exclude those items that belong to `Foo`
            category. Default to None
        indices (list, optional): With the indices parameter you can specify to search within one specific Index.
            Default: None
        query_name (str, optional): Indicates a query name to used. It could be
            one of "match_and", "match_or", "fuzzy", or "phonetic_text". If you
            do not provide one, search API will select the best one. Default to
            None.
        sort (lst, optional): Indicates a sorting rule for results. If
            sort is a list of strings, each element will define a field to sort
            by in ascending order. If sort is a list of dictionaries, you can
            set the order. For instance, sort: [{'color': 'desc'}] will sort
            results by color name in descending order. If you do not provided
            one, results will be ordered by score in descending order. Default
            to None.
        page (int, optional): Indicates a page of results. If you provide a
            page, `query` will return the results from that page. Default to
            None.
        rpp (int, optional): Indicates how many results to fetch by page,
            minimum 1, maximum 100. Default to None.
        facets (list, optional): Indicates a list with dicts of facets to fetch.
            An object with field is required, size is optional (max 100).
        filter_execution (SearchQueryName, optional): If you want you can change it to "or".
            Default to None.
        stats (bool, optional): Enable/Disable this search in stats reports.
            Default: None
        skip_auto_filters (list, optional): A list of fields to be skipped from auto_filters feature.
        skip_top_facet (list, optional): A list of fields to be skipped from top_facet feature.
        title_facet (bool, optional): Enable/Disable title_facet feature.
            Default: None.
        top_facet (bool, optional): Enable/Disable top_facet feature.
            Default: None.
    """
    query_params = parse_query_params({
        'query': query,
        'auto_filters': auto_filters,
        'custom_results': custom_results,
        'excluded_results': excluded_results,
        'filter': filter,
        'exclude': exclude,
        'query_name': query_name,
        'indices': indices,
        'sort': sort,
        'page': page,
        'rpp': rpp,
        'facets': facets,
        'filter_execution': filter_execution,
        'session_id': session_id,
        'stats': stats,
        'skip_auto_filters': skip_auto_filters,
        'skip_top_facet': skip_top_facet,
        'title_facet': title_facet,
        'top_facet': top_facet
    })

    api_client = SearchAPIClient(**opts)
    return api_client.get(
        f'/6/{hashid}/_search',
        query_params=query_params
    )