Skip to content

Commit fe44c50

Browse files
committed
Support returnProperties and includedProperties parameters of ListItems and ListUsers. Send User-Agent HTTP header. Use POST for recomm requests. Use artificial default value for parameters in order to allow sending null value.
1 parent c219280 commit fe44c50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+616
-230
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
RecombeeApiClient
33
*****************
44

5-
A Python client for easy use of the `Recombee <https://www.recombee.com/>`_ recommendation API.
5+
A Python client for easy use of the `Recombee <https://www.recombee.com/>`_ recommendation API. Both Python 2 and Python 3 are supported.
66

77
If you don't have an account at Recombee yet, you can create a free account `here <https://www.recombee.com/>`_.
88

@@ -18,6 +18,8 @@ Install the client with pip:
1818
1919
$ pip install recombee-api-client
2020
21+
(use pip3 instead of pip if you use Python 3)
22+
2123
========
2224
Examples
2325
========

recombee_api_client/api_client.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,40 @@ def send(self, request):
6161
except requests.exceptions.Timeout:
6262
raise ApiTimeoutException(request)
6363

64+
@staticmethod
65+
def __get_http_headers(additional_headers=None):
66+
headers = {'User-Agent': 'recombee-python-api-client/1.4.0'}
67+
if additional_headers:
68+
headers.update(additional_headers)
69+
return headers
70+
6471
def __put(self, request, uri, timeout):
65-
response = requests.put(uri, timeout=timeout)
72+
response = requests.put(uri,
73+
data=json.dumps(request.get_body_parameters()),
74+
headers= self.__get_http_headers({'Content-Type': 'application/json'}),
75+
timeout=timeout)
6676
self.__check_errors(response, request)
6777
return response.json()
6878

6979
def __get(self, request, uri, timeout):
70-
response = requests.get(uri, timeout=timeout)
80+
response = requests.get(uri,
81+
headers= self.__get_http_headers(),
82+
timeout=timeout)
7183
self.__check_errors(response, request)
7284
return response.json()
7385

7486
def __post(self, request, uri, timeout):
75-
response = requests.post(uri, data=json.dumps(request.get_body_parameters()),
76-
headers={'Content-Type': 'application/json'},
77-
timeout=timeout)
87+
response = requests.post(uri,
88+
data=json.dumps(request.get_body_parameters()),
89+
headers= self.__get_http_headers({'Content-Type': 'application/json'}),
90+
timeout=timeout)
7891
self.__check_errors(response, request)
7992
return response.json()
8093

8194
def __delete(self, request, uri, timeout):
82-
response = requests.delete(uri, timeout=timeout)
95+
response = requests.delete(uri,
96+
headers= self.__get_http_headers(),
97+
timeout=timeout)
8398
self.__check_errors(response, request)
8499
return response.json()
85100

@@ -109,7 +124,6 @@ def __send_multipart_batch(self, batch):
109124

110125
def __process_request_uri(self, request):
111126
uri = request.path
112-
uri = uri[len('/{databaseId}/'):]
113127
uri += self.__query_parameters_to_url(request)
114128
return uri
115129

@@ -131,7 +145,7 @@ def __format_query_parameter_value(self, value):
131145
# Sign request with HMAC, request URI must be exacly the same
132146
# We have 30s to complete request with this token
133147
def __sign_url(self, req_part):
134-
uri = '/' + self.database_id + '/' + req_part
148+
uri = '/' + self.database_id + req_part
135149
time = self.__hmac_time(uri)
136150
sign = self.__hmac_sign(uri, time)
137151
res = uri + time + '&hmac_sign=' +sign
@@ -145,4 +159,4 @@ def __hmac_time(self, uri):
145159
def __hmac_sign(self, uri, time):
146160
url = uri + time
147161
sign = hmac.new(str.encode(self.token), str.encode(url), sha1).hexdigest()
148-
return sign
162+
return sign

recombee_api_client/api_requests/add_bookmark.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddBookmark(Request):
47
"""
58
Adds a bookmark of a given item made by a given user.
69
710
"""
811

9-
def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
12+
def __init__(self, user_id, item_id, timestamp=DEFAULT, cascade_create=DEFAULT):
1013
"""
1114
Required parameters:
1215
@param user_id: User who bookmarked the item
@@ -27,7 +30,7 @@ def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
2730
self.timeout = 1000
2831
self.ensure_https = False
2932
self.method = 'post'
30-
self.path = "/{databaseId}/bookmarks/" % ()
33+
self.path = "/bookmarks/" % ()
3134

3235
def get_body_parameters(self):
3336
"""
@@ -36,9 +39,9 @@ def get_body_parameters(self):
3639
p = dict()
3740
p['userId'] = self.user_id
3841
p['itemId'] = self.item_id
39-
if self.timestamp is not None:
42+
if self.timestamp is not DEFAULT:
4043
p['timestamp'] = self.timestamp
41-
if self.cascade_create is not None:
44+
if self.cascade_create is not DEFAULT:
4245
p['cascadeCreate'] = self.cascade_create
4346
return p
4447

recombee_api_client/api_requests/add_cart_addition.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddCartAddition(Request):
47
"""
58
Adds a cart addition of a given item made by a given user.
69
710
"""
811

9-
def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
12+
def __init__(self, user_id, item_id, timestamp=DEFAULT, cascade_create=DEFAULT):
1013
"""
1114
Required parameters:
1215
@param user_id: User who added the item to the cart
@@ -27,7 +30,7 @@ def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
2730
self.timeout = 1000
2831
self.ensure_https = False
2932
self.method = 'post'
30-
self.path = "/{databaseId}/cartadditions/" % ()
33+
self.path = "/cartadditions/" % ()
3134

3235
def get_body_parameters(self):
3336
"""
@@ -36,9 +39,9 @@ def get_body_parameters(self):
3639
p = dict()
3740
p['userId'] = self.user_id
3841
p['itemId'] = self.item_id
39-
if self.timestamp is not None:
42+
if self.timestamp is not DEFAULT:
4043
p['timestamp'] = self.timestamp
41-
if self.cascade_create is not None:
44+
if self.cascade_create is not DEFAULT:
4245
p['cascadeCreate'] = self.cascade_create
4346
return p
4447

recombee_api_client/api_requests/add_detail_view.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddDetailView(Request):
47
"""
58
Adds a detail view of a given item made by a given user.
69
710
"""
811

9-
def __init__(self,user_id, item_id, timestamp=None, duration=None, cascade_create=None):
12+
def __init__(self, user_id, item_id, timestamp=DEFAULT, duration=DEFAULT, cascade_create=DEFAULT):
1013
"""
1114
Required parameters:
1215
@param user_id: User who viewed the item
@@ -30,7 +33,7 @@ def __init__(self,user_id, item_id, timestamp=None, duration=None, cascade_creat
3033
self.timeout = 1000
3134
self.ensure_https = False
3235
self.method = 'post'
33-
self.path = "/{databaseId}/detailviews/" % ()
36+
self.path = "/detailviews/" % ()
3437

3538
def get_body_parameters(self):
3639
"""
@@ -39,11 +42,11 @@ def get_body_parameters(self):
3942
p = dict()
4043
p['userId'] = self.user_id
4144
p['itemId'] = self.item_id
42-
if self.timestamp is not None:
45+
if self.timestamp is not DEFAULT:
4346
p['timestamp'] = self.timestamp
44-
if self.duration is not None:
47+
if self.duration is not DEFAULT:
4548
p['duration'] = self.duration
46-
if self.cascade_create is not None:
49+
if self.cascade_create is not DEFAULT:
4750
p['cascadeCreate'] = self.cascade_create
4851
return p
4952

recombee_api_client/api_requests/add_group.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddGroup(Request):
47
"""
58
Creates new group in the database.
69
"""
710

8-
def __init__(self,group_id):
11+
def __init__(self, group_id):
912
"""
1013
Required parameters:
1114
@param group_id: ID of the group to be created.
@@ -15,7 +18,7 @@ def __init__(self,group_id):
1518
self.timeout = 1000
1619
self.ensure_https = False
1720
self.method = 'put'
18-
self.path = "/{databaseId}/groups/%s" % (self.group_id)
21+
self.path = "/groups/%s" % (self.group_id)
1922

2023
def get_body_parameters(self):
2124
"""

recombee_api_client/api_requests/add_item.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddItem(Request):
47
"""
@@ -8,7 +11,7 @@ class AddItem(Request):
811
912
"""
1013

11-
def __init__(self,item_id):
14+
def __init__(self, item_id):
1215
"""
1316
Required parameters:
1417
@param item_id: ID of the item to be created.
@@ -18,7 +21,7 @@ def __init__(self,item_id):
1821
self.timeout = 1000
1922
self.ensure_https = False
2023
self.method = 'put'
21-
self.path = "/{databaseId}/items/%s" % (self.item_id)
24+
self.path = "/items/%s" % (self.item_id)
2225

2326
def get_body_parameters(self):
2427
"""

recombee_api_client/api_requests/add_item_property.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddItemProperty(Request):
47
"""
58
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.
69
710
"""
811

9-
def __init__(self,property_name, type):
12+
def __init__(self, property_name, type):
1013
"""
1114
Required parameters:
1215
@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.
@@ -21,7 +24,7 @@ def __init__(self,property_name, type):
2124
self.timeout = 1000
2225
self.ensure_https = False
2326
self.method = 'put'
24-
self.path = "/{databaseId}/items/properties/%s" % (self.property_name)
27+
self.path = "/items/properties/%s" % (self.property_name)
2528

2629
def get_body_parameters(self):
2730
"""

recombee_api_client/api_requests/add_purchase.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddPurchase(Request):
47
"""
58
Adds a purchase of a given item made by a given user.
69
710
"""
811

9-
def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
12+
def __init__(self, user_id, item_id, timestamp=DEFAULT, cascade_create=DEFAULT):
1013
"""
1114
Required parameters:
1215
@param user_id: User who purchased the item
@@ -27,7 +30,7 @@ def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
2730
self.timeout = 1000
2831
self.ensure_https = False
2932
self.method = 'post'
30-
self.path = "/{databaseId}/purchases/" % ()
33+
self.path = "/purchases/" % ()
3134

3235
def get_body_parameters(self):
3336
"""
@@ -36,9 +39,9 @@ def get_body_parameters(self):
3639
p = dict()
3740
p['userId'] = self.user_id
3841
p['itemId'] = self.item_id
39-
if self.timestamp is not None:
42+
if self.timestamp is not DEFAULT:
4043
p['timestamp'] = self.timestamp
41-
if self.cascade_create is not None:
44+
if self.cascade_create is not DEFAULT:
4245
p['cascadeCreate'] = self.cascade_create
4346
return p
4447

recombee_api_client/api_requests/add_rating.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from recombee_api_client.api_requests.request import Request
2+
import uuid
3+
4+
DEFAULT = uuid.uuid4()
25

36
class AddRating(Request):
47
"""
58
Adds a rating of given item made by a given user.
69
710
"""
811

9-
def __init__(self,user_id, item_id, rating, timestamp=None, cascade_create=None):
12+
def __init__(self, user_id, item_id, rating, timestamp=DEFAULT, cascade_create=DEFAULT):
1013
"""
1114
Required parameters:
1215
@param user_id: User who submitted the rating
@@ -30,7 +33,7 @@ def __init__(self,user_id, item_id, rating, timestamp=None, cascade_create=None)
3033
self.timeout = 1000
3134
self.ensure_https = False
3235
self.method = 'post'
33-
self.path = "/{databaseId}/ratings/" % ()
36+
self.path = "/ratings/" % ()
3437

3538
def get_body_parameters(self):
3639
"""
@@ -40,9 +43,9 @@ def get_body_parameters(self):
4043
p['userId'] = self.user_id
4144
p['itemId'] = self.item_id
4245
p['rating'] = self.rating
43-
if self.timestamp is not None:
46+
if self.timestamp is not DEFAULT:
4447
p['timestamp'] = self.timestamp
45-
if self.cascade_create is not None:
48+
if self.cascade_create is not DEFAULT:
4649
p['cascadeCreate'] = self.cascade_create
4750
return p
4851

0 commit comments

Comments
 (0)