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

gurwls223 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 5cb1c63d5b4 [SPARK-43172][CONNECT] Expose host and token from spark 
connect client
5cb1c63d5b4 is described below

commit 5cb1c63d5b4fa7dd6862d6add03344079bdb19b4
Author: Niranjan Jayakar <n...@databricks.com>
AuthorDate: Tue Apr 18 21:52:24 2023 +0900

    [SPARK-43172][CONNECT] Expose host and token from spark connect client
    
    ### What changes were proposed in this pull request?
    
    Expose the host and bearer token as properties of the
    `SparkConnectClient`.
    
    ### Why are the changes needed?
    
    This allows for querying the connecting host and bearer token
    given an instance of spark connect client.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Introduces two new properties `host` and `token` to the
    `SparkConnectClient` class
    
    ### How was this patch tested?
    
    Unit tests
    
    Closes #40836 from nija-at/expose-host-token.
    
    Authored-by: Niranjan Jayakar <n...@databricks.com>
    Signed-off-by: Hyukjin Kwon <gurwls...@apache.org>
---
 python/pyspark/sql/connect/client.py            | 15 +++++++++++++++
 python/pyspark/sql/tests/connect/test_client.py |  8 ++++++++
 2 files changed, 23 insertions(+)

diff --git a/python/pyspark/sql/connect/client.py 
b/python/pyspark/sql/connect/client.py
index 756b62fb06d..780c5702477 100644
--- a/python/pyspark/sql/connect/client.py
+++ b/python/pyspark/sql/connect/client.py
@@ -784,6 +784,21 @@ class SparkConnectClient(object):
         """
         self._channel.close()
 
+    @property
+    def host(self) -> str:
+        """
+        The hostname where this client intends to connect.
+        """
+        return self._builder.host
+
+    @property
+    def token(self) -> Optional[str]:
+        """
+        The authentication bearer token during connection.
+        If authentication is not using a bearer token, None will be returned.
+        """
+        return self._builder._token
+
     def _execute_plan_request_with_metadata(self) -> pb2.ExecutePlanRequest:
         req = pb2.ExecutePlanRequest()
         req.session_id = self._session_id
diff --git a/python/pyspark/sql/tests/connect/test_client.py 
b/python/pyspark/sql/tests/connect/test_client.py
index 6131e146363..182b7445891 100644
--- a/python/pyspark/sql/tests/connect/test_client.py
+++ b/python/pyspark/sql/tests/connect/test_client.py
@@ -50,6 +50,14 @@ class SparkConnectClientTestCase(unittest.TestCase):
         self.assertIsNotNone(mock.req, "ExecutePlan API was not called when 
expected")
         self.assertEqual(mock.req.client_type, "_SPARK_CONNECT_PYTHON")
 
+    def test_properties(self):
+        client = SparkConnectClient("sc://foo/;token=bar")
+        self.assertEqual(client.token, "bar")
+        self.assertEqual(client.host, "foo")
+
+        client = SparkConnectClient("sc://foo/")
+        self.assertIsNone(client.token)
+
 
 class MockService:
     # Simplest mock of the SparkConnectService.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to