def test_find_object(self): self.index.search = mock.Mock(name="search") self.index.search.return_value = { "hits": [{ "foo": "bar" }], "nbPages": 1 } self.index.find_object(lambda obj: True) args, _ = self.index.search.call_args self.assertEqual(args[0], "") self.assertEqual(args[1].data, RequestOptions.create(self.config, { "page": 0 }).data) self.index.find_object(lambda obj: True, { "query": "foo", "hitsPerPage": 5 }) args, _ = self.index.search.call_args self.assertEqual(args[0], "foo") self.assertEqual( args[1].data, RequestOptions.create(self.config, { "hitsPerPage": 5, "page": 0 }).data, ) self.index.find_object( lambda obj: True, RequestOptions.create(self.config, {"User-Agent": "blabla"}), ) args, _ = self.index.search.call_args self.assertEqual(args[0], "") self.assertEqual(args[1].data, RequestOptions.create(self.config, { "page": 0 }).data) self.assertEqual( args[1].headers, RequestOptions.create(self.config, { "User-Agent": "blabla" }).headers, )
def get_objects(self, object_ids, request_options=None): # type: (Iterator[str], Optional[Union[dict, RequestOptions]]) -> dict if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create( self._config, request_options ) # store attributesToRetrieve for use in each request attributes_to_retrieve = request_options.data.pop( 'attributesToRetrieve', None) requests = [] for object_id in object_ids: request = {'indexName': self._name, 'objectID': str(object_id)} if attributes_to_retrieve: request['attributesToRetrieve'] = attributes_to_retrieve requests.append(request) return self._transporter.read( Verb.POST, '1/indexes/*/objects', { 'requests': requests }, request_options )
def test_get_objects_with_attributes_to_retreive_bulk(self): request_options = RequestOptions.create( self.config, {"attributesToRetrieve": ["firstname", "lastname"]}) requests = [ { "indexName": "index-name", "objectID": "foo_id", "attributesToRetrieve": ["firstname", "lastname"], }, { "indexName": "index-name", "objectID": "bar_id", "attributesToRetrieve": ["firstname", "lastname"], }, ] self.index.get_objects(["foo_id", "bar_id"], request_options) self.transporter.read.assert_called_once_with( "POST", "1/indexes/*/objects", {"requests": requests}, # asserts version 2 it's used. request_options, ) self.assertNotIn("attributesToRetrieve", request_options.data)
def test_get_settings(self): self.transporter.read.return_value = { "attributesToIndex": ["attr1", "attr2"], "numericAttributesToIndex": ["attr1", "attr2"], "slaves": ["index1", "index2"], "ignorePlurals": True, } request_options = RequestOptions.create(self.config, {"foo": "bar"}) settings = self.index.get_settings(request_options) self.transporter.read.assert_called_once_with( "GET", "1/indexes/index-name/settings", None, request_options) self.assertEqual(request_options.query_parameters["getVersion"], 2) self.assertEqual( settings, { "searchableAttributes": ["attr1", "attr2"], "numericAttributesForFiltering": ["attr1", "attr2"], "replicas": ["index1", "index2"], "ignorePlurals": True, }, )
def get_objects(self, object_ids, request_options=None): # type: (Iterator[str], Optional[Union[dict, RequestOptions]]) -> dict if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create( self._config, request_options ) # store attributesToRetrieve for use in each request attributes_to_retrieve = request_options.data.pop('attributesToRetrieve', None) requests = [] for object_id in object_ids: request = {'indexName': self._name, 'objectID': str(object_id)} if attributes_to_retrieve: request['attributesToRetrieve'] = attributes_to_retrieve requests.append(request) return self._transporter.read( Verb.POST, '1/indexes/*/objects', { 'requests': requests }, request_options )
def test_get_objects_with_attributes_to_retreive_bulk(self): request_options = RequestOptions.create(self.config, { 'attributesToRetrieve': ['firstname', 'lastname'] }) requests = [{ 'indexName': 'index-name', 'objectID': 'foo_id', 'attributesToRetrieve': ['firstname', 'lastname'] }, { 'indexName': 'index-name', 'objectID': 'bar_id', 'attributesToRetrieve': ['firstname', 'lastname'] }] self.index.get_objects(['foo_id', 'bar_id'], request_options) self.transporter.read.assert_called_once_with( 'POST', '1/indexes/*/objects', {'requests': requests}, # asserts version 2 it's used. request_options ) self.assertNotIn('attributesToRetrieve', request_options.data)
def find_object(self, callback, request_options=None): # type: (Callable[[Dict[str, Any]], bool], Optional[Union[dict, RequestOptions]]) -> dict # noqa: E501 paginate = True query = "" page = 0 if isinstance(request_options, dict): request_options = copy.copy(request_options) paginate = request_options.pop("paginate", paginate) query = request_options.pop("query", query) request_options = RequestOptions.create(self._config, request_options) while True: request_options.data["page"] = page res = self.search(query, request_options) for pos, hit in enumerate(res["hits"]): if callback(hit): return { "object": hit, "position": pos, "page": page, } has_next_page = page + 1 < int(res["nbPages"]) if not paginate or not has_next_page: raise ObjectNotFoundException page += 1
def test_update_api_key_response(self): request_options = RequestOptions({}, {}, {}, { 'data': {'validity': 300} }) response = UpdateApiKeyResponse({}, {}, request_options) self.assertTrue(response._have_changed({})) self.assertTrue(response._have_changed({'validity': 297})) request_options.data = {'maxQueriesPerIPPerHour': 2} self.assertFalse(response._have_changed({})) self.assertFalse(response._have_changed({'maxQueriesPerIPPerHour': 1})) self.assertTrue(response._have_changed({'maxQueriesPerIPPerHour': 2})) request_options.data = {'acl': ['search']} self.assertFalse(response._have_changed({})) self.assertFalse(response._have_changed({'acl': []})) self.assertTrue(response._have_changed({'acl': ['search']}))
def test_get_settings_with_request_options(self): request_options = RequestOptions.create(self.config, {'foo': 'bar'}) self.index.get_settings(request_options) args = self.transporter.read.call_args[0] self.assertEqual(args[3].query_parameters['getVersion'], 2)
def test_find_object(self): self.index.search = mock.Mock(name="search") self.index.search.return_value = { 'hits': [{ 'foo': 'bar' }], 'nbPages': 1 } self.index.find_object(lambda obj: True) args, _ = self.index.search.call_args self.assertEqual(args[0], '') self.assertEqual(args[1].data, RequestOptions.create(self.config, { 'page': 0 }).data) self.index.find_object(lambda obj: True, { 'query': 'foo', 'hitsPerPage': 5 }) args, _ = self.index.search.call_args self.assertEqual(args[0], 'foo') self.assertEqual( args[1].data, RequestOptions.create(self.config, { 'hitsPerPage': 5, 'page': 0 }).data) self.index.find_object( lambda obj: True, RequestOptions.create(self.config, {'User-Agent': 'blabla'})) args, _ = self.index.search.call_args self.assertEqual(args[0], '') self.assertEqual(args[1].data, RequestOptions.create(self.config, { 'page': 0 }).data) self.assertEqual( args[1].headers, RequestOptions.create(self.config, { 'User-Agent': 'blabla' }).headers)
def test_update_api_key_response(self): request_options = RequestOptions({}, {}, {}, {'data': { 'validity': 300 }}) response = UpdateApiKeyResponse({}, {}, request_options) self.assertTrue(response._have_changed({})) self.assertTrue(response._have_changed({'validity': 297})) request_options.data = {'maxQueriesPerIPPerHour': 2} self.assertFalse(response._have_changed({})) self.assertFalse(response._have_changed({'maxQueriesPerIPPerHour': 1})) self.assertTrue(response._have_changed({'maxQueriesPerIPPerHour': 2})) request_options.data = {'acl': ['search']} self.assertFalse(response._have_changed({})) self.assertFalse(response._have_changed({'acl': []})) self.assertTrue(response._have_changed({'acl': ['search']}))
def replace_all_synonyms(self, synoyms, request_options=None): # type: (Union[List[dict], Iterator[dict]], Optional[Union[dict, RequestOptions]]) -> IndexingResponse # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) request_options["replaceExistingSynonyms"] = True return self.save_synonyms(list(synoyms), request_options)
def test_update_api_key_response(self): request_options = RequestOptions({}, {}, {}, {"data": { "validity": 300 }}) response = UpdateApiKeyResponse({}, {}, request_options) self.assertTrue(response._have_changed({})) self.assertTrue(response._have_changed({"validity": 297})) request_options.data = {"maxQueriesPerIPPerHour": 2} self.assertFalse(response._have_changed({})) self.assertFalse(response._have_changed({"maxQueriesPerIPPerHour": 1})) self.assertTrue(response._have_changed({"maxQueriesPerIPPerHour": 2})) request_options.data = {"acl": ["search"]} self.assertFalse(response._have_changed({})) self.assertFalse(response._have_changed({"acl": []})) self.assertTrue(response._have_changed({"acl": ["search"]}))
def replace_all_rules(self, rules, request_options=None): # type: (Union[List[dict], Iterator[dict]], Optional[Union[dict, RequestOptions]]) -> IndexingResponse # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) request_options.query_parameters["clearExistingRules"] = 1 return self.save_rules(list(rules), request_options)
def remove_user_id(self, user_id, request_options=None): # type: (str, Optional[Union[dict, RequestOptions]]) -> dict if request_options is None: request_options = RequestOptions.create(self._config) request_options["X-Algolia-User-ID"] = user_id return self._transporter.write(Verb.DELETE, "1/clusters/mapping", None, request_options)
def replace_all_rules(self, rules, request_options=None): # type: (Union[List[dict], Iterator[dict]], Optional[Union[dict, RequestOptions]]) -> IndexingResponse # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) request_options.query_parameters['clearExistingRules'] = 1 return self.save_rules(list(rules), request_options)
def replace_all_synonyms(self, synoyms, request_options=None): # type: (Union[List[dict], Iterator[dict]], Optional[Union[dict, RequestOptions]]) -> IndexingResponse # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) request_options['replaceExistingSynonyms'] = True return self.save_synonyms(list(synoyms), request_options)
def assign_user_id(self, user_id, cluster, request_options=None): # type: (str, str,Optional[Union[dict, RequestOptions]]) -> dict if request_options is None: request_options = RequestOptions.create(self._config) request_options["X-Algolia-User-ID"] = user_id return self._transporter.write(Verb.POST, "1/clusters/mapping", {"cluster": cluster}, request_options)
def read(self, verb, path, data, request_options): # type: (str, str, Optional[Union[dict, list]], Optional[Union[dict, RequestOptions]]) -> dict # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) timeout = request_options.timeouts['readTimeout'] hosts = self._config.hosts.read() return self.request(verb, hosts, path, data, request_options, timeout)
def update_api_key(self, key, request_options=None): # type: (str, Optional[Union[dict, RequestOptions]]) -> UpdateApiKeyResponse # noqa: E501 if not isinstance(request_options, RequestOptions): request_options = RequestOptions.create(self._config, request_options) raw_response = self._transporter.write(Verb.PUT, endpoint("1/keys/{}", key), {}, request_options) return UpdateApiKeyResponse(self, raw_response, request_options)
def read(self, verb, path, data, request_options): # type: (str, str, Optional[Union[dict, list]], Optional[Union[dict, RequestOptions]]) -> dict # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) timeout = request_options.timeouts["readTimeout"] hosts = self._config.hosts.read() return self.request(verb, hosts, path, data, request_options, timeout)
def test_get_objects(self): request_options = RequestOptions.create(self.config) requests = [{'indexName': 'index-name', 'objectID': 'foo_id'}] self.index.get_objects(['foo_id'], request_options) self.transporter.read.assert_called_once_with( 'POST', '1/indexes/*/objects', {'requests': requests}, # asserts version 2 it's used. request_options)
def test_get_objects(self): request_options = RequestOptions.create(self.config) requests = [{"indexName": "index-name", "objectID": "foo_id"}] self.index.get_objects(["foo_id"], request_options) self.transporter.read.assert_called_once_with( "POST", "1/indexes/*/objects", {"requests": requests}, # asserts version 2 it's used. request_options, )
def assign_user_id(self, user_id, cluster, request_options=None): # type: (str, str,Optional[Union[dict, RequestOptions]]) -> dict if request_options is None: request_options = RequestOptions.create(self._config) request_options['X-Algolia-User-ID'] = user_id return self._transporter.write( Verb.POST, '1/clusters/mapping', {'cluster': cluster}, request_options )
def get_settings_async(self, request_options=None): # type: ignore # type: (Optional[Union[dict, RequestOptions]]) -> dict if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) request_options.query_parameters['getVersion'] = 2 raw_response = yield from self._transporter_async.read( Verb.GET, endpoint('1/indexes/{}/settings', self._name), None, request_options) return SettingsDeserializer.deserialize(raw_response)
def remove_user_id(self, user_id, request_options=None): # type: (str, Optional[Union[dict, RequestOptions]]) -> dict if request_options is None: request_options = RequestOptions.create(self._config) request_options['X-Algolia-User-ID'] = user_id return self._transporter.write( Verb.DELETE, '1/clusters/mapping', None, request_options )
def update_api_key(self, key, request_options=None): # type: (str, Optional[Union[dict, RequestOptions]]) -> UpdateApiKeyResponse # noqa: E501 if not isinstance(request_options, RequestOptions): request_options = RequestOptions.create(self._config, request_options) raw_response = self._transporter.write( Verb.PUT, endpoint('1/keys/{}', key), {}, request_options ) return UpdateApiKeyResponse(self, raw_response, request_options)
def test_get_objects(self): request_options = RequestOptions.create(self.config) requests = [{ 'indexName': 'index-name', 'objectID': 'foo_id' }] self.index.get_objects(['foo_id'], request_options) self.transporter.read.assert_called_once_with( 'POST', '1/indexes/*/objects', {'requests': requests}, # asserts version 2 it's used. request_options )
def get_settings(self, request_options=None): # type: (Optional[Union[dict, RequestOptions]]) -> dict # noqa: E501 if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create(self._config, request_options) request_options.query_parameters["getVersion"] = 2 raw_response = self._transporter.read( Verb.GET, endpoint("1/indexes/{}/settings", self._name), None, request_options, ) return SettingsDeserializer.deserialize(raw_response)
def setUp(self): self.requester = Requester() self.requester.send = mock.Mock(name="send") self.requester.send.return_value = Response(200, {'foo': 'bar'}) self.data = {'data': 'foo'} self.config = SearchConfig('foo', 'bar') self.request_options = RequestOptions.create( self.config, { 'User-Agent': 'foo', 'createIfNotExists': True, 'readTimeout': 109, 'bodyParam': 'bar' }) self.transporter = Transporter(self.requester, self.config)
def has_pending_mappings(self, request_options=None): # type: (Optional[Union[dict, RequestOptions]]) -> dict retrieve_mappings = None if isinstance(request_options, dict): retrieve_mappings = request_options.pop("retrieveMappings", retrieve_mappings) if retrieve_mappings: if request_options is None or isinstance(request_options, dict): request_options = RequestOptions.create( self._config, request_options) request_options.query_parameters["getClusters"] = retrieve_mappings return self._transporter.read(Verb.GET, "1/clusters/mapping/pending", None, request_options)
def test_get_objects_with_attributes_to_retreive(self): request_options = RequestOptions.create( self.config, {'attributesToRetrieve': ['firstname', 'lastname']}) requests = [{ 'indexName': 'index-name', 'objectID': 'foo_id', 'attributesToRetrieve': ['firstname', 'lastname'] }] self.index.get_objects(['foo_id'], request_options) self.transporter.read.assert_called_once_with( 'POST', '1/indexes/*/objects', {'requests': requests}, # asserts version 2 it's used. request_options) self.assertNotIn('attributesToRetrieve', request_options.data)
def setUp(self): self.config = SearchConfig("foo", "bar") self.config.headers["Foo-Bar"] = "foo-bar" self.request_options = RequestOptions.create( self.config, { # Headers "User-Agent": "foo", # Upper case header, should equal to `X-Forwarded-For` "X-FORWARDED-FOR": "1.1.1.1", # Query Params "createIfNotExists": True, # Timeouts Params "readTimeout": 10, # Data Params "bodyParam": "bar", }, )
def setUp(self): self.requester = Requester() self.requester.send = mock.Mock(name="send") self.requester.send.return_value = Response(200, {'foo': 'bar'}) self.data = { 'data': 'foo' } self.config = SearchConfig('foo', 'bar') self.request_options = RequestOptions.create(self.config, { 'User-Agent': 'foo', 'createIfNotExists': True, 'readTimeout': 109, 'bodyParam': 'bar' }) self.transporter = Transporter(self.requester, self.config)
def setUp(self): self.config = SearchConfig('foo', 'bar') self.config.headers['Foo-Bar'] = 'foo-bar' self.request_options = RequestOptions.create( self.config, { # Headers 'User-Agent': 'foo', # Query Params 'createIfNotExists': True, # Timeouts Params 'readTimeout': 10, # Data Params 'bodyParam': 'bar' })
def setUp(self): self.requester = Requester() self.requester.send = mock.Mock(name="send") self.requester.send.return_value = Response(200, {"foo": "bar"}) self.data = {"data": "foo"} self.config = SearchConfig("foo", "bar") self.request_options = RequestOptions.create( self.config, { "User-Agent": "foo", "createIfNotExists": True, "readTimeout": 109, "bodyParam": "bar", }, ) self.transporter = Transporter(self.requester, self.config)
def test_with_proxy(self): config = SearchConfig("foo", "bar") requester = Requester() requester.send = mock.Mock(name="send") requester.send.return_value = Response(200, {"foo": "bar"}) transporter = Transporter(requester, config) headers = RequestOptions.create(config).headers data = {} transporter.write("post", "endpoint/foo", data, None) request = Request( "POST", headers, {}, 2, # Default connect timeout 30, # Default timeout proxies={"https": "https://127.0.0.1:8080"}, ) request.url = "https://foo.algolia.net/endpoint/foo?" requester.send.assert_called_once_with(request)
def setUp(self): self.config = SearchConfig('foo', 'bar') self.config.headers['Foo-Bar'] = 'foo-bar' self.request_options = RequestOptions.create(self.config, { # Headers 'User-Agent': 'foo', # Upper case header, should equal to `X-Forwarded-For` 'X-FORWARDED-FOR': '1.1.1.1', # Query Params 'createIfNotExists': True, # Timeouts Params 'readTimeout': 10, # Data Params 'bodyParam': 'bar' })
def test_get_settings(self): self.transporter.read.return_value = { 'attributesToIndex': ['attr1', 'attr2'], 'numericAttributesToIndex': ['attr1', 'attr2'], 'slaves': ['index1', 'index2'], 'ignorePlurals': True, } request_options = RequestOptions.create(self.config, {'foo': 'bar'}) settings = self.index.get_settings(request_options) self.transporter.read.assert_called_once_with( 'GET', '1/indexes/index-name/settings', None, request_options) self.assertEqual(request_options.query_parameters['getVersion'], 2) self.assertEqual( settings, { 'searchableAttributes': ['attr1', 'attr2'], 'numericAttributesForFiltering': ['attr1', 'attr2'], 'replicas': ['index1', 'index2'], 'ignorePlurals': True, })
def find_object_async(self, callback, request_options=None): # type: ignore # noqa: E501 # type: (Callable[[Dict[str, any]], bool], Optional[Union[dict, RequestOptions]]) -> dict # noqa: E501 paginate = True query = '' page = 0 if isinstance(request_options, dict): request_options = copy.copy(request_options) paginate = request_options.pop('paginate', paginate) query = request_options.pop('query', query) request_options = RequestOptions.create( self._config, request_options ) while True: request_options.data['page'] = page res = yield from self.search_async(query, request_options) # type: ignore # noqa: E501 for pos, hit in enumerate(res['hits']): if callback(hit): return { 'object': hit, 'position': pos, 'page': page, } has_next_page = page + 1 < int(res['nbPages']) if not paginate or not has_next_page: raise ObjectNotFoundException page += 1
def test_with_options(self): self.assertIsInstance(RequestOptions.create(self.config), RequestOptions)
def test_default_user_agent(self): self.assertEqual( RequestOptions.create(self.config).headers['User-Agent'], UserAgent.get() )