Skip to content

Commit

Permalink
Support RecommendNextItems endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraFiedler committed Jan 14, 2021
1 parent 021d747 commit dc44d9c
Show file tree
Hide file tree
Showing 93 changed files with 1,952 additions and 1,803 deletions.
13 changes: 8 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Basic example
from recombee_api_client.api_client import RecombeeClient
from recombee_api_client.exceptions import APIException
from recombee_api_client.api_requests import AddPurchase, RecommendItemsToUser, Batch
from recombee_api_client.api_requests import *
import random
client = RecombeeClient('--my-database-id--', '--db-private-token--')
Expand All @@ -55,8 +55,12 @@ Basic example
client.send(Batch(purchase_requests))
# Get recommendations for user 'user-25'
recommended = client.send(RecommendItemsToUser('user-25', 5))
print("Recommended items: %s" % recommended)
response = client.send(RecommendItemsToUser('user-25', 5))
print("Recommended items: %s" % response)
# User scrolled down - get next 3 recommended items
response = client.send(RecommendNextItems(response['recommId'], 3))
print("Next recommended items: %s" % response)
except APIException as e:
print(e)
Expand Down Expand Up @@ -146,7 +150,7 @@ Using property values
)
# Perform personalized full-text search with a user's search query (e.g. 'computers').
matches = client.send(SearchItems('user-42', 'computers', 5))
matches = client.send(SearchItems('user-42', 'computers', 5, scenario='search_top'))
print("Matched items: %s" % matches)
------------------
Expand All @@ -160,7 +164,6 @@ We are doing our best to provide the fastest and most reliable service, but prod
Example:

.. code-block:: python
from recombee_api_client.exceptions import *
try:
Expand Down
15 changes: 8 additions & 7 deletions recombee_api_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
class RecombeeClient:
"""
Client for sending requests to Recombee recommender system
:param database_id: Name of your database_id at Recombee
:param token: Secret token obtained from Recombee for signing requests
:param protocol: Default protocol for sending requests. Possible values: 'http', 'https'.
"""
BATCH_MAX_SIZE = 10000

def __init__(self, database_id, token, protocol = 'https', options = {}):
"""
@param database_id: Name of your database_id at Recombee
@param token: Secret token obtained from Recombee for signing requests
@param protocol: Default protocol for sending requests. Possible values: 'http', 'https'.
"""
self.database_id = database_id
self.token = token
self.protocol = protocol
Expand All @@ -38,7 +39,7 @@ def __init__(self, database_id, token, protocol = 'https', options = {}):

def send(self, request):
"""
@param request: Request to be sent to Recombee recommender
:param request: Request to be sent to Recombee recommender
"""

if isinstance(request, Batch) and len(request.requests) > self.BATCH_MAX_SIZE:
Expand All @@ -63,7 +64,7 @@ def send(self, request):

@staticmethod
def __get_http_headers(additional_headers=None):
headers = {'User-Agent': 'recombee-python-api-client/3.0.0'}
headers = {'User-Agent': 'recombee-python-api-client/3.1.0'}
if additional_headers:
headers.update(additional_headers)
return headers
Expand Down
3 changes: 2 additions & 1 deletion recombee_api_client/api_requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
from recombee_api_client.api_requests.list_item_view_portions import ListItemViewPortions
from recombee_api_client.api_requests.list_user_view_portions import ListUserViewPortions
from recombee_api_client.api_requests.recommend_items_to_user import RecommendItemsToUser
from recombee_api_client.api_requests.recommend_users_to_user import RecommendUsersToUser
from recombee_api_client.api_requests.recommend_items_to_item import RecommendItemsToItem
from recombee_api_client.api_requests.recommend_next_items import RecommendNextItems
from recombee_api_client.api_requests.recommend_users_to_user import RecommendUsersToUser
from recombee_api_client.api_requests.recommend_users_to_item import RecommendUsersToItem
from recombee_api_client.api_requests.search_items import SearchItems
from recombee_api_client.api_requests.user_based_recommendation import UserBasedRecommendation
Expand Down
35 changes: 18 additions & 17 deletions recombee_api_client/api_requests/add_bookmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@
class AddBookmark(Request):
"""
Adds a bookmark of a given item made by a given user.
Required parameters:
:param user_id: User who bookmarked the item
:param item_id: Bookmarked item
Optional parameters:
:param timestamp: UTC timestamp of the bookmark as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
:param cascade_create: Sets whether the given user/item should be created if not present in the database.
:param recomm_id: If this bookmark is based on a recommendation request, `recommId` is the id of the clicked recommendation.
:param additional_data: A dictionary of additional data for the interaction.
"""

def __init__(self, user_id, item_id, timestamp=DEFAULT, cascade_create=DEFAULT, recomm_id=DEFAULT, additional_data=DEFAULT):
"""
Required parameters:
@param user_id: User who bookmarked the item
@param item_id: Bookmarked item
Optional parameters:
@param timestamp: UTC timestamp of the bookmark as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
@param recomm_id: If this bookmark is based on a recommendation request, `recommId` is the id of the clicked recommendation.
@param additional_data: A dictionary of additional data for the interaction.
"""
self.user_id = user_id
self.item_id = item_id
self.timestamp = timestamp
Expand Down
43 changes: 22 additions & 21 deletions recombee_api_client/api_requests/add_cart_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@
class AddCartAddition(Request):
"""
Adds a cart addition of a given item made by a given user.
Required parameters:
:param user_id: User who added the item to the cart
:param item_id: Item added to the cart
Optional parameters:
:param timestamp: UTC timestamp of the cart addition as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
:param cascade_create: Sets whether the given user/item should be created if not present in the database.
:param amount: Amount (number) added to cart. The default is 1. For example if `user-x` adds two `item-y` during a single order (session...), the `amount` should equal to 2.
:param price: Price of the added item. If `amount` is greater than 1, sum of prices of all the items should be given.
:param recomm_id: If this cart addition is based on a recommendation request, `recommId` is the id of the clicked recommendation.
:param additional_data: A dictionary of additional data for the interaction.
"""

def __init__(self, user_id, item_id, timestamp=DEFAULT, cascade_create=DEFAULT, amount=DEFAULT, price=DEFAULT, recomm_id=DEFAULT, additional_data=DEFAULT):
"""
Required parameters:
@param user_id: User who added the item to the cart
@param item_id: Item added to the cart
Optional parameters:
@param timestamp: UTC timestamp of the cart addition as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
@param amount: Amount (number) added to cart. The default is 1. For example if `user-x` adds two `item-y` during a single order (session...), the `amount` should equal to 2.
@param price: Price of the added item. If `amount` is greater than 1, sum of prices of all the items should be given.
@param recomm_id: If this cart addition is based on a recommendation request, `recommId` is the id of the clicked recommendation.
@param additional_data: A dictionary of additional data for the interaction.
"""
self.user_id = user_id
self.item_id = item_id
self.timestamp = timestamp
Expand Down
39 changes: 20 additions & 19 deletions recombee_api_client/api_requests/add_detail_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@
class AddDetailView(Request):
"""
Adds a detail view of a given item made by a given user.
Required parameters:
:param user_id: User who viewed the item
:param item_id: Viewed item
Optional parameters:
:param timestamp: UTC timestamp of the view as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
:param duration: Duration of the view
:param cascade_create: Sets whether the given user/item should be created if not present in the database.
:param recomm_id: If this detail view is based on a recommendation request, `recommId` is the id of the clicked recommendation.
:param additional_data: A dictionary of additional data for the interaction.
"""

def __init__(self, user_id, item_id, timestamp=DEFAULT, duration=DEFAULT, cascade_create=DEFAULT, recomm_id=DEFAULT, additional_data=DEFAULT):
"""
Required parameters:
@param user_id: User who viewed the item
@param item_id: Viewed item
Optional parameters:
@param timestamp: UTC timestamp of the view as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
@param duration: Duration of the view
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
@param recomm_id: If this detail view is based on a recommendation request, `recommId` is the id of the clicked recommendation.
@param additional_data: A dictionary of additional data for the interaction.
"""
self.user_id = user_id
self.item_id = item_id
self.timestamp = timestamp
Expand Down
10 changes: 5 additions & 5 deletions recombee_api_client/api_requests/add_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
class AddGroup(Request):
"""
Creates new group in the database.
Required parameters:
:param group_id: ID of the group to be created.
"""

def __init__(self, group_id):
"""
Required parameters:
@param group_id: ID of the group to be created.
"""
self.group_id = group_id
self.timeout = 1000
self.ensure_https = False
Expand Down
10 changes: 5 additions & 5 deletions recombee_api_client/api_requests/add_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class AddItem(Request):
Adds new item of given `itemId` to the items catalog.
All the item properties for the newly created items are set null.
Required parameters:
:param item_id: ID of the item to be created.
"""

def __init__(self, item_id):
"""
Required parameters:
@param item_id: ID of the item to be created.
"""
self.item_id = item_id
self.timeout = 1000
self.ensure_https = False
Expand Down
66 changes: 33 additions & 33 deletions recombee_api_client/api_requests/add_item_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@
class AddItemProperty(Request):
"""
Adding an item property is somehow equivalent to adding a column to the table of items. The items may be characterized by various properties of different types.
Required parameters:
:param property_name: Name of the item property to be created. Currently, the following names are reserved:`id`, `itemid`, case insensitively. Also, the length of the property name must not exceed 63 characters.
:param type: Value type of the item property to be created. One of: `int`, `double`, `string`, `boolean`, `timestamp`, `set`, `image` or `imageList`.
* `int`- Signed integer number.
* `double` - Floating point number. It uses 64-bit base-2 format (IEEE 754 standard).
* `string` - UTF-8 string.
* `boolean` - *true* / *false*
* `timestamp` - Value representing date and time.
* `set` - Set of strings.
* `image` - URL of an image (`jpeg`, `png` or `gif`).
* `imageList` - List of URLs that refer to images.
"""

def __init__(self, property_name, type):
"""
Required parameters:
@param property_name: Name of the item property to be created. Currently, the following names are reserved:`id`, `itemid`, case insensitively. Also, the length of the property name must not exceed 63 characters.
@param type: Value type of the item property to be created. One of: `int`, `double`, `string`, `boolean`, `timestamp`, `set`, `image` or `imageList`.
* `int`- Signed integer number.
* `double` - Floating point number. It uses 64-bit base-2 format (IEEE 754 standard).
* `string` - UTF-8 string.
* `boolean` - *true* / *false*
* `timestamp` - Value representing date and time.
* `set` - Set of strings.
* `image` - URL of an image (`jpeg`, `png` or `gif`).
* `imageList` - List of URLs that refer to images.
"""
self.property_name = property_name
self.type = type
self.timeout = 100000
Expand Down
Loading

0 comments on commit dc44d9c

Please sign in to comment.