Skip to content

Commit 100a2e6

Browse files
philkrafxdgear
authored andcommitted
Send User-Agent header identifying the Client (elastic#991)
* send user agent header identifing the client * no need to instantiate a new var * add python version to user-agent
1 parent 6dc6018 commit 100a2e6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

elasticsearch/transport.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import time
22
from itertools import chain
3+
from platform import python_version
34

45
from .connection import Urllib3HttpConnection
56
from .connection_pool import ConnectionPool, DummyConnectionPool
67
from .serializer import JSONSerializer, Deserializer, DEFAULT_SERIALIZERS
8+
from . import __versionstr__
79
from .exceptions import (
810
ConnectionError,
911
TransportError,
@@ -335,6 +337,10 @@ def perform_request(self, method, url, headers=None, params=None, body=None):
335337
if isinstance(ignore, int):
336338
ignore = (ignore,)
337339

340+
if headers is None:
341+
headers = {}
342+
headers["user-agent"] = "elasticsearch-py/%s (Python %s)" % (__versionstr__, python_version())
343+
338344
for attempt in range(self.max_retries + 1):
339345
connection = self.get_connection()
340346

test_elasticsearch/test_transport.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33
import time
4+
from platform import python_version
45

56
from elasticsearch.transport import Transport, get_host_info
67
from elasticsearch.connection import Connection
78
from elasticsearch.connection_pool import DummyConnectionPool
89
from elasticsearch.exceptions import ConnectionError, ImproperlyConfigured
10+
from elasticsearch import __versionstr__
911

1012
from .test_cases import TestCase
1113

@@ -82,7 +84,9 @@ def test_request_timeout_extracted_from_params_and_passed(self):
8284
self.assertEquals(1, len(t.get_connection().calls))
8385
self.assertEquals(("GET", "/", {}, None), t.get_connection().calls[0][0])
8486
self.assertEquals(
85-
{"timeout": 42, "ignore": (), "headers": None},
87+
{"timeout": 42, "ignore": (), "headers": {
88+
'user-agent':"elasticsearch-py/%s (Python %s)" % (__versionstr__, python_version())}
89+
},
8690
t.get_connection().calls[0][1],
8791
)
8892

0 commit comments

Comments
 (0)