Skip to content

Commit

Permalink
Ass send_get_body_as parameter for situations where GET cannot have a…
Browse files Browse the repository at this point in the history
… body
  • Loading branch information
honzakral committed Dec 2, 2013
1 parent 69b8023 commit cf03f98
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
21 changes: 20 additions & 1 deletion elasticsearch/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, hosts, connection_class=Urllib3HttpConnection,
connection_pool_class=ConnectionPool, host_info_callback=get_host_info,
sniff_on_start=False, sniffer_timeout=None,
sniff_on_connection_fail=False, serializer=JSONSerializer(),
max_retries=3, **kwargs):
max_retries=3, send_get_body_as='GET', **kwargs):
"""
:arg hosts: list of dictionaries, each containing keyword arguments to
create a `connection_class` instance
Expand All @@ -51,13 +51,19 @@ def __init__(self, hosts, connection_class=Urllib3HttpConnection,
:arg sniff_on_connection_fail: flag controlling if connection failure triggers a sniff
:arg serializer: serializer instance
:arg max_retries: maximum number of retries before an exception is propagated
:arg send_get_body_as: for GET requests with body this option allows
you to specify an alternate way of execution for environments that
don't support passing bodies with GET requests. If you set this to
'POST' a POST method will be used instead, if to 'source' then the body
will be serialized and passed as a query parameter `source`.
Any extra keyword arguments will be passed to the `connection_class`
when creating and instance unless overriden by that connection's
options provided as part of the hosts parameter.
"""

self.max_retries = max_retries
self.send_get_body_as = send_get_body_as

# data serializer
self.serializer = serializer
Expand Down Expand Up @@ -215,6 +221,19 @@ def perform_request(self, method, url, params=None, body=None):
if body is not None:
body = self.serializer.dumps(body)

# some clients or environments don't support sending GET with body
if method == 'GET' and self.send_get_body_as != 'GET':
# send it as post instead
if self.send_get_body_as == 'POST':
method = 'POST'

# or as source parameter
elif self.send_get_body_as == 'source':
if params is None:
params = {}
params['source'] = body
body = None

ignore = ()
if params and 'ignore' in params:
ignore = params.pop('ignore')
Expand Down
14 changes: 14 additions & 0 deletions test_elasticsearch/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def perform_request(self, *args, **kwargs):
}'''

class TestTransport(TestCase):
def test_send_get_body_as_source(self):
t = Transport([{}], send_get_body_as='source', connection_class=DummyConnection)

t.perform_request('GET', '/', body={})
self.assertEquals(1, len(t.get_connection().calls))
self.assertEquals(('GET', '/', {'source': '{}'}, None), t.get_connection().calls[0][0])

def test_send_get_body_as_post(self):
t = Transport([{}], send_get_body_as='POST', connection_class=DummyConnection)

t.perform_request('GET', '/', body={})
self.assertEquals(1, len(t.get_connection().calls))
self.assertEquals(('POST', '/', None, '{}'), t.get_connection().calls[0][0])

def test_kwargs_passed_on_to_connections(self):
t = Transport([{'host': 'google.com'}], port=123)
self.assertEquals(1, len(t.connection_pool.connections))
Expand Down

0 comments on commit cf03f98

Please sign in to comment.