def create_with_config(config):
        # type: (RecommendationConfig) -> RecommendationClient

        warnings.warn(
            "`%s.%s` is deprecated, use `%s.%s` instead."
            % (
                "RecommendationClient",
                "create_with_config",
                "PersonalizationClient",
                "create_with_config",
            ),
            DeprecationWarning,
        )

        requester = Requester()
        transporter = Transporter(requester, config)

        client = RecommendationClient(transporter, config)

        if is_async_available():
            from algoliasearch.recommendation_client_async import (
                RecommendationClientAsync,
            )
            from algoliasearch.http.transporter_async import TransporterAsync
            from algoliasearch.http.requester_async import RequesterAsync

            return RecommendationClientAsync(
                client, TransporterAsync(RequesterAsync(), config), config
            )

        return client
    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)
Ejemplo n.º 3
0
 def setUp(self):
     self.config = SearchConfig('foo', 'bar')
     requester = Requester()
     self.transporter = Transporter(requester, self.config)
     self.transporter.read = mock.Mock(name="read")
     self.transporter.read.return_value = {}
     self.transporter.write = mock.Mock(name="write")
     self.transporter.write.return_value = {}
     self.index = SearchIndex(self.transporter, self.config, 'index-name')
    def setUp(self):
        self.config = SearchConfig('foo', 'bar')
        self.transporter = Transporter(Requester(), self.config)
        self.transporter.read = mock.Mock(name="read")
        self.transporter.read.return_value = {}
        self.transporter.write = mock.Mock(name="write")
        self.transporter.write.return_value = {}

        self.client = SearchClient(self.transporter, self.config)
Ejemplo n.º 5
0
    def setUp(self):
        self.config = SearchConfig("foo", "bar")
        self.transporter = Transporter(Requester(), self.config)
        self.transporter.read = mock.Mock(name="read")
        self.transporter.read.return_value = {}
        self.transporter.write = mock.Mock(name="write")
        self.transporter.write.return_value = {}

        self.client = RecommendClient(self.transporter, self.config)
    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 create_with_config(config):
        # type: (SearchConfig) -> SearchClient

        requester = Requester()
        transporter = Transporter(requester, config)

        client = SearchClient(transporter, config)

        if is_async_available():
            from algoliasearch.search_client_async import SearchClientAsync
            from algoliasearch.http.transporter_async import TransporterAsync
            from algoliasearch.http.requester_async import RequesterAsync

            return SearchClientAsync(
                client, TransporterAsync(RequesterAsync(), config), config)

        return client
    def create_with_config(config):
        # type: (RecommendationConfig) -> RecommendationClient

        requester = Requester()
        transporter = Transporter(requester, config)

        client = RecommendationClient(transporter, config)

        if is_async_available():
            from algoliasearch.recommendation_client_async import (
                RecommendationClientAsync, )
            from algoliasearch.http.transporter_async import TransporterAsync
            from algoliasearch.http.requester_async import RequesterAsync

            return RecommendationClientAsync(
                client, TransporterAsync(RequesterAsync(), config), config)

        return client
    def create_with_config(config):
        # type: (InsightsConfig) -> InsightsClient

        requester = Requester()
        transporter = Transporter(requester, config)

        client = InsightsClient(transporter, config)

        if is_async_available():
            from algoliasearch.insights_client_async import InsightsClientAsync
            from algoliasearch.http.transporter_async import \
                TransporterAsync
            from algoliasearch.http.requester_async import RequesterAsync

            return InsightsClientAsync(
                client, TransporterAsync(RequesterAsync(), config), config)

        return client
    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)
class TestTransporter(unittest.TestCase):
    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_success_write(self):
        response = self.transporter.write('post', 'endpoint/foo', self.data,
                                          self.request_options)

        host = self.config.hosts.write()[
            0]  # type: Host

        request = Request(
            'POST',
            self.request_options.headers,
            self.data,
            2,  # Default connect timeout
            30,  # Default timeout
        )

        request.url = 'https://{}/endpoint/foo?createIfNotExists=true'.format(
            host.url)

        self.requester.send.assert_called_once_with(request)

        self.assertEqual({'foo': 'bar'}, response)
        self.assertEqual(self.requester.send.call_count, 1)

    def test_success_read(self):
        response = self.transporter.read('get', 'endpoint/bar', {},
                                         self.request_options)

        host = self.config.hosts.read()[0]  # type: Host

        request = Request(
            'GET',  # Upper case letters
            self.request_options.headers,
            {'bodyParam': 'bar'},
            2,  # Default connect timeout
            109  # Customized timeout
        )
        request.url = 'https://{}/endpoint/bar?createIfNotExists=true'.format(
            host.url)

        self.requester.send.assert_called_once_with(request)

        self.assertEqual({'foo': 'bar'}, response)
        self.assertEqual(self.requester.send.call_count, 1)

    def test_unreachable_hosts_exception(self):
        self.requester.send.return_value = Response(300, {'foo': 'bar'})

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.read('get', 'endpoint/bar', {},
                                  self.request_options)

        self.assertEqual(self.requester.send.call_count, 4)

        self.requester.send.return_value = Response(100, {'foo': 'bar'})

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.read('get', 'endpoint/bar', {},
                                  self.request_options)
        # Remains 4, all hosts here down.
        self.assertEqual(self.requester.send.call_count, 4)

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.write('get', 'endpoint/bar', {},
                                   self.request_options)

        # It's now 5, write have one different host.
        self.assertEqual(self.requester.send.call_count, 5)

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.write('get', 'endpoint/bar', {},
                                   self.request_options)

        # Remains 5, all hosts here down.
        self.assertEqual(self.requester.send.call_count, 5)

    def test_algolia_exception(self):
        self.requester.send.return_value = Response(401, {'foo': 'bar'})

        with self.assertRaises(AlgoliaException) as _:
            self.transporter.read('get', 'endpoint/bar', {},
                                  self.request_options)
        self.assertEqual(self.requester.send.call_count, 1)
class TestTransporter(unittest.TestCase):
    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_success_write(self):
        response = self.transporter.write('post', 'endpoint/foo', self.data,
                                          self.request_options)

        host = self.config.hosts.write()[0]  # type: Host

        request = Request(
            'POST',
            self.request_options.headers,
            self.data,
            2,  # Default connect timeout
            30,  # Default timeout
        )

        request.url = 'https://{}/endpoint/foo?createIfNotExists=true'.format(
            host.url)

        self.requester.send.assert_called_once_with(request)

        self.assertEqual({'foo': 'bar'}, response)
        self.assertEqual(self.requester.send.call_count, 1)

    def test_success_read(self):
        response = self.transporter.read('get', 'endpoint/bar', {},
                                         self.request_options)

        host = self.config.hosts.read()[0]  # type: Host

        request = Request(
            'GET',  # Upper case letters
            self.request_options.headers,
            {'bodyParam': 'bar'},
            2,  # Default connect timeout
            109  # Customized timeout
        )
        request.url = 'https://{}/endpoint/bar?createIfNotExists=true'.format(
            host.url)

        self.requester.send.assert_called_once_with(request)

        self.assertEqual({'foo': 'bar'}, response)
        self.assertEqual(self.requester.send.call_count, 1)

    def test_unreachable_hosts_exception(self):
        self.requester.send.return_value = Response(300, {'foo': 'bar'})

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.read('get', 'endpoint/bar', {},
                                  self.request_options)

        self.assertEqual(self.requester.send.call_count, 4)

        self.requester.send.return_value = Response(100, {'foo': 'bar'})

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.read('get', 'endpoint/bar', {},
                                  self.request_options)
        # Remains 4, all hosts here down.
        self.assertEqual(self.requester.send.call_count, 4)

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.write('get', 'endpoint/bar', {},
                                   self.request_options)

        # It's now 5, write have one different host.
        self.assertEqual(self.requester.send.call_count, 5)

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.write('get', 'endpoint/bar', {},
                                   self.request_options)

        # Remains 5, all hosts here down.
        self.assertEqual(self.requester.send.call_count, 5)

    def test_algolia_exception(self):
        self.requester.send.return_value = Response(401, {'foo': 'bar'})

        with self.assertRaises(AlgoliaException) as _:
            self.transporter.read('get', 'endpoint/bar', {},
                                  self.request_options)
        self.assertEqual(self.requester.send.call_count, 1)
class TestTransporter(unittest.TestCase):
    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_success_write(self):
        response = self.transporter.write(
            "post", "endpoint/foo", self.data, self.request_options
        )

        host = self.config.hosts.write()[0]  # type: Host

        request = Request(
            "POST",
            self.request_options.headers,
            self.data,
            2,  # Default connect timeout
            30,  # Default timeout
        )

        request.url = "https://{}/endpoint/foo?createIfNotExists=true".format(host.url)

        self.requester.send.assert_called_once_with(request)

        self.assertEqual({"foo": "bar"}, response)
        self.assertEqual(self.requester.send.call_count, 1)

    def test_success_read(self):
        response = self.transporter.read(
            "get", "endpoint/bar", {}, self.request_options
        )

        host = self.config.hosts.read()[0]  # type: Host

        request = Request(
            "GET",  # Upper case letters
            self.request_options.headers,
            {"bodyParam": "bar"},
            2,  # Default connect timeout
            109,  # Customized timeout
        )
        request.url = "https://{}/endpoint/bar?createIfNotExists=true".format(host.url)

        self.requester.send.assert_called_once_with(request)

        self.assertEqual({"foo": "bar"}, response)
        self.assertEqual(self.requester.send.call_count, 1)

    def test_unreachable_hosts_exception(self):
        self.requester.send.return_value = Response(300, {"foo": "bar"})

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.read("get", "endpoint/bar", {}, self.request_options)

        self.assertEqual(self.requester.send.call_count, 4)

        self.requester.send.return_value = Response(100, {"foo": "bar"})

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.read("get", "endpoint/bar", {}, self.request_options)
        # Remains 4, all hosts here down.
        self.assertEqual(self.requester.send.call_count, 4)

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.write("get", "endpoint/bar", {}, self.request_options)

        # It's now 5, write have one different host.
        self.assertEqual(self.requester.send.call_count, 5)

        with self.assertRaises(AlgoliaUnreachableHostException) as _:
            self.transporter.write("get", "endpoint/bar", {}, self.request_options)

        # Remains 5, all hosts here down.
        self.assertEqual(self.requester.send.call_count, 5)

    def test_algolia_exception(self):
        self.requester.send.return_value = Response(401, {"foo": "bar"})

        with self.assertRaises(AlgoliaException) as _:
            self.transporter.read("get", "endpoint/bar", {}, self.request_options)
        self.assertEqual(self.requester.send.call_count, 1)