Skip to content

Commit

Permalink
Merge 2dc904c into 19eed1c
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoErcolanelli committed Sep 24, 2016
2 parents 19eed1c + 2dc904c commit dabb56f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
34 changes: 22 additions & 12 deletions algoliasearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def __init__(self, client, index_name):
self.index_name = index_name
self._request_path = '/1/indexes/%s' % safe(self.index_name)


@deprecated
def addObject(self, content, object_id=None):
return self.add_object(content, object_id)
Expand Down Expand Up @@ -141,19 +140,20 @@ def get_object(self, object_id, attributes_to_retrieve=None):
def getObjects(self, object_ids):
return self.get_objects(object_ids)

def get_objects(self, object_ids):
def get_objects(self, object_ids, attributes_to_retrieve=None):
"""
Get several objects from this index.
@param object_ids the array of unique identifier of objects to retrieve
@param attributes_to_retrieve (optional) if set, contains the list
of attributes to retrieve as a string separated by a comma
"""

requests = []
for object_id in object_ids:
requests.append({
'indexName': self.index_name,
'objectID': object_id
})
request = {'indexName': self.index_name, 'objectID': object_id}
if attributes_to_retrieve is not None:
request['attributesToRetrieve'] = ",".join(attributes_to_retrieve)
requests.append(request)
data = {'requests': requests}
path = '/1/indexes/*/objects' # Use client._req()
return self.client._req(True, path, 'POST', data=data)
Expand All @@ -162,26 +162,32 @@ def get_objects(self, object_ids):
def partialUpdateObject(self, partial_object):
return self.partial_update_object(partial_object)

def partial_update_object(self, partial_object):
def partial_update_object(self, partial_object, no_create=False):
"""
Update partially an object (only update attributes passed in argument).
@param partial_object contains the object attributes to override, the
object must contains an objectID attribute
@param no_create specifies whether or not a missing object must be
created
"""
path = '/%s/partial' % safe(partial_object['objectID'])
if no_create:
path += '?createIfNotExists=false'
return self._req(False, path, 'POST', data=partial_object)

@deprecated
def partialUpdateObjects(self, objects):
return self.partial_update_objects(objects)

def partial_update_objects(self, objects):
def partial_update_objects(self, objects, no_create=False):
"""
Partially Override the content of several objects.
@param objects contains an array of objects to update (each object
must contains a objectID attribute)
@param no_create specifies whether or not a missing object must be
created
"""
requests = []
for obj in objects:
Expand All @@ -190,7 +196,7 @@ def partial_update_objects(self, objects):
'objectID': obj['objectID'],
'body': obj
})
return self.batch(requests)
return self.batch(requests, no_create=no_create)

@deprecated
def saveObject(self, obj):
Expand Down Expand Up @@ -487,7 +493,7 @@ def search_disjunctive_faceting(self, query, disjunctive_facets,
for i in range(1, len(answers['results'])):
for facet in answers['results'][i]['facets']:
aggregated_answer['disjunctiveFacets'][facet] = (
answers['results'][i]['facets'][facet])
answers['results'][i]['facets'][facet])

if facet not in disjunctive_refinements:
continue
Expand Down Expand Up @@ -876,11 +882,15 @@ def update_user_key(self, key, obj, validity=None,
path = '/keys/%s' % key
return self._req(False, path, 'PUT', data=obj)

def batch(self, requests):
def batch(self, requests, no_create=False):
"""Send a batch requests."""
if isinstance(requests, (list, tuple)):
requests = {'requests': requests}

path = '/batch'
if no_create:
path += '?createIfNotExists=false'

return self._req(False, '/batch', 'POST', data=requests)

def _req(self, is_search, path, meth, params=None, data=None):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ def test_set_end_user_ip(self):
self.assertEqual(self.client.headers['X-Forwarded-For'], '192.168.0.1')

def test_set_extra_headers(self):
self.client.set_extra_headers(Private=True)
self.client.set_extra_headers(Private="on")
self.assertIn('Private', self.client.headers)
self.assertEqual(self.client.headers['Private'], True)
self.assertEqual(self.client.headers['Private'], "on")

self.client.set_extra_headers(**{
'X-User': 223254,
'X-User': '223254',
'X-Privacy-Settings': 'NSA-Free'
})
self.assertIn('X-User', self.client.headers)
self.assertEqual(self.client.headers['X-User'], 223254)
self.assertEqual(self.client.headers['X-User'], '223254')
self.assertIn('X-Privacy-Settings', self.client.headers)
self.assertEqual(self.client.headers['X-Privacy-Settings'], 'NSA-Free')

Expand Down
15 changes: 12 additions & 3 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from algoliasearch.client import MAX_API_KEY_LENGTH
from algoliasearch.helpers import AlgoliaException

from .helpers import safe_index_name
from .helpers import get_api_client
from .helpers import FakeData
from tests.helpers import safe_index_name
from tests.helpers import get_api_client
from tests.helpers import FakeData


class IndexTest(unittest.TestCase):
Expand Down Expand Up @@ -212,6 +212,15 @@ def test_get_objects(self):
self.assertDictContainsSubset(self.objs[0], res['results'][1])
self.assertDictContainsSubset(self.objs[2], res['results'][2])

def test_get_objects_with_attributes_to_retrieve(self):
res = self.index.get_objects(self.objectIDs[1:3], attributes_to_retrieve=['name', 'email'])
for obj, obj_res in zip(self.objs[1:3], res['results']):
self.assertEqual(obj['name'], obj_res['name'])
self.assertEqual(obj['email'], obj_res['email'])
self.assertNotIn('phone', obj_res)
self.assertNotIn('city', obj_res)
self.assertNotIn('country', obj_res)

def test_browse(self):
res = self.index.browse(page=0, hits_per_page=2)
self.assertEqual(res['page'], 0)
Expand Down

0 comments on commit dabb56f

Please sign in to comment.