mozilla / elasticutils

[deprecated] A friendly chainable ElasticSearch interface for python
http://elasticutils.rtfd.org
BSD 3-Clause "New" or "Revised" License
243 stars 76 forks source link

Elasticsearch 'urls' should accept dict (now throwing TypeError: unhashable type: 'dict') #277

Open ferdynice opened 9 years ago

ferdynice commented 9 years ago

The Elasticsearch connection used internally by ElasticUtils supports two ways of configuration:

By string and by dict. Usually using string method is good enough, although some connections require passing a dict (for exampe with auth). At the moment, when you pass a dict, ElasticUtils throws TypeError: unhashable type: 'dict' because it tries to cache the connection.

A current workaround for now is to wrap the dict in tuple, so that the hash function succeeds. For example:

def es_url_to_dict(url):
    parse = urlparse(url)
    port = parse.port if parse.port else (80 if parse.scheme == 'http' else 443)
    use_ssl = port is 443
    host = {'host': parse.hostname,
            'port': port,
            'use_ssl': use_ssl,
            'http_auth': '%s:%s' % (parse.username, parse.password)}
    return tuple(sorted(host.items()))
ES_URLS = [es_url_to_dict(os.environ.get('ES_URL', 'http://localhost:9200'))]

With this you can configure urls like https://myuser:mypass@myhost

willkg commented 9 years ago

If you want to do a PR for it, that'd be great. Otherwise I'll get to this at some point, but no promises as to when.