This is an automated email from the ASF dual-hosted git repository.

stoty pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix-queryserver.git


The following commit(s) were added to refs/heads/master by this push:
     new f77151d  PHOENIX-6921 Add support for custom HTTP headers for phoenixdb
f77151d is described below

commit f77151d9f3a97d4c2b56f2ba3f705c82cc52160c
Author: Anton Fedotov <tosha.fedotov.2...@gmail.com>
AuthorDate: Thu Apr 6 04:36:08 2023 +0000

    PHOENIX-6921 Add support for custom HTTP headers for phoenixdb
---
 python-phoenixdb/NEWS.rst                    |  1 +
 python-phoenixdb/phoenixdb/__init__.py       |  7 +++++--
 python-phoenixdb/phoenixdb/avatica/client.py | 12 +++++++-----
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/python-phoenixdb/NEWS.rst b/python-phoenixdb/NEWS.rst
index ecadcab..838d960 100644
--- a/python-phoenixdb/NEWS.rst
+++ b/python-phoenixdb/NEWS.rst
@@ -11,6 +11,7 @@ Unreleased
 - SQLAlchemy is no longer an install dependency (PHOENIX-6892)
 - Run tests with all supported Python + SqlAlchemy versions (1.3, 1.4, 2.0) 
(PHOENIX-6892)
 - Replace deprecated failUnless methods in tests (PHOENIX-6892)
+- Add support for specifying custom HTTP headers (PHOENIX-6921)
 
 Version 1.2.1
 -------------
diff --git a/python-phoenixdb/phoenixdb/__init__.py 
b/python-phoenixdb/phoenixdb/__init__.py
index 44b4136..900e4da 100644
--- a/python-phoenixdb/phoenixdb/__init__.py
+++ b/python-phoenixdb/phoenixdb/__init__.py
@@ -56,7 +56,7 @@ For example::
 
 
 def connect(url, max_retries=None, auth=None, authentication=None, 
avatica_user=None, avatica_password=None,
-            truststore=None, verify=None, do_as=None, user=None, 
password=None, **kwargs):
+            truststore=None, verify=None, do_as=None, user=None, 
password=None, extra_headers=None, **kwargs):
     """Connects to a Phoenix query server.
 
     :param url:
@@ -109,6 +109,9 @@ def connect(url, max_retries=None, auth=None, 
authentication=None, avatica_user=
     :param truststore:
         Alias for verify
 
+    :param extra_headers:
+        Additional HTTP headers as a dictionary
+
     :returns:
         :class:`~phoenixdb.connection.Connection` object.
     """
@@ -118,7 +121,7 @@ def connect(url, max_retries=None, auth=None, 
authentication=None, avatica_user=
         avatica_user=avatica_user, avatica_password=avatica_password,
         truststore=truststore, verify=verify, do_as=do_as, user=user, 
password=password)
 
-    client = AvaticaClient(url, max_retries=max_retries, auth=auth, 
verify=verify)
+    client = AvaticaClient(url, max_retries=max_retries, auth=auth, 
verify=verify, extra_headers=extra_headers)
     client.connect()
     return Connection(client, **kwargs)
 
diff --git a/python-phoenixdb/phoenixdb/avatica/client.py 
b/python-phoenixdb/phoenixdb/avatica/client.py
index 48b6406..88e6b3f 100644
--- a/python-phoenixdb/phoenixdb/avatica/client.py
+++ b/python-phoenixdb/phoenixdb/avatica/client.py
@@ -138,7 +138,7 @@ class AvaticaClient(object):
     to a server using :func:`phoenixdb.connect`.
     """
 
-    def __init__(self, url, max_retries=None, auth=None, verify=None):
+    def __init__(self, url, max_retries=None, auth=None, verify=None, 
extra_headers=None):
         """Constructs a new client object.
 
         :param url:
@@ -148,6 +148,9 @@ class AvaticaClient(object):
         self.max_retries = max_retries if max_retries is not None else 3
         self.auth = auth
         self.verify = verify
+        self.headers = {'content-type': 'application/x-google-protobuf'}
+        if extra_headers:
+            self.headers.update(extra_headers)
         self.session = None
 
     def __del__(self):
@@ -163,12 +166,12 @@ class AvaticaClient(object):
             self.session.close()
             self.session = None
 
-    def _post_request(self, body, headers):
+    def _post_request(self, body):
         # Create the session if we haven't before
         if not self.session:
             logger.debug("Creating a new Session")
             self.session = requests.Session()
-            self.session.headers.update(headers)
+            self.session.headers.update(self.headers)
             self.session.stream = True
             if self.auth is not None:
                 self.session.auth = self.auth
@@ -213,9 +216,8 @@ class AvaticaClient(object):
         message.name = 
'org.apache.calcite.avatica.proto.Requests${}'.format(request_name)
         message.wrapped_message = request_data.SerializeToString()
         body = message.SerializeToString()
-        headers = {'content-type': 'application/x-google-protobuf'}
 
-        response = self._post_request(body, headers)
+        response = self._post_request(body)
         response_body = response.raw.read()
 
         if response.status_code != requests.codes.ok:

Reply via email to