Be more benevolent when dealing with hosts parameter

This was mostly inspired by work on haystack for backwards compatibility
This commit is contained in:
Honza Král
2013-10-22 01:22:47 +02:00
parent 73f170f23f
commit 78d5a0b5a8
2 changed files with 34 additions and 0 deletions
+16
View File
@@ -1,4 +1,5 @@
import weakref
import logging
from ..transport import Transport
from ..exceptions import NotFoundError, TransportError
@@ -6,6 +7,8 @@ from .indices import IndicesClient
from .cluster import ClusterClient
from .utils import query_params, _make_path
logger = logging.getLogger('elasticsearch')
def _normalize_hosts(hosts):
"""
Helper function to transform hosts argument to
@@ -15,10 +18,23 @@ def _normalize_hosts(hosts):
if hosts is None:
return [{}]
# passed in just one string
if isinstance(hosts, (type(''), type(u''))):
hosts = [hosts]
out = []
# normalize hosts to dicts
for i, host in enumerate(hosts):
if isinstance(host, (type(''), type(u''))):
host = host.strip('/')
# remove schema information
if '://' in host:
logger.warn(
"List of nodes should not include schema information (http://): %r.",
host
)
host = host[host.index('://') + 3:]
h = {"host": host}
if ':' in host:
# TODO: detect auth urls
@@ -1,3 +1,5 @@
from mock import patch
from elasticsearch.client import _normalize_hosts
from ..test_cases import TestCase, ElasticsearchTestCase
@@ -18,6 +20,22 @@ class TestNormalizeHosts(TestCase):
def test_dicts_are_left_unchanged(self):
self.assertEquals([{"host": "local", "extra": 123}], _normalize_hosts([{"host": "local", "extra": 123}]))
@patch('elasticsearch.client.logger')
def test_schema_is_stripped_out(self, logger):
self.assertEquals(
[{"host": "elasticsearch.org", "port": 9200}],
_normalize_hosts(["http://elasticsearch.org:9200/"])
)
# schema triggers a warning
self.assertEquals(1, logger.warn.call_count)
def test_single_string_is_wrapped_in_list(self):
self.assertEquals(
[{"host": "elasticsearch.org"}],
_normalize_hosts("elasticsearch.org")
)
class TestClient(ElasticsearchTestCase):
def test_from_in_search(self):
self.client.search(index='i', doc_type='t', from_=10)