You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/09/04 12:07:47 UTC

[tinkerpop] 01/02: TINKERPOP-2046 Gremlin-Python: Add support for custom request headers in WebSocket request

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

spmallette pushed a commit to branch TINKERPOP-2046
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 29e61312905301cb8f4f32469ab712821705ecf5
Author: Martin Kulhavy <ma...@f-secure.com>
AuthorDate: Wed Aug 28 13:14:53 2019 +0300

    TINKERPOP-2046 Gremlin-Python: Add support for custom request headers in WebSocket request
---
 docs/src/reference/gremlin-variants.asciidoc                | 13 ++++++++++---
 .../src/main/jython/gremlin_python/driver/client.py         |  7 +++++--
 .../src/main/jython/gremlin_python/driver/connection.py     |  5 +++--
 .../gremlin_python/driver/driver_remote_connection.py       |  6 ++++--
 .../main/jython/gremlin_python/driver/tornado/transport.py  |  5 ++++-
 .../src/main/jython/gremlin_python/driver/transport.py      |  2 +-
 6 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/docs/src/reference/gremlin-variants.asciidoc b/docs/src/reference/gremlin-variants.asciidoc
index ff75287..c356019 100644
--- a/docs/src/reference/gremlin-variants.asciidoc
+++ b/docs/src/reference/gremlin-variants.asciidoc
@@ -182,6 +182,13 @@ There are various ways to submit a traversal to a `RemoteConnection`. Just as in
 * `Traversal.toSet()`
 * `Traversal.iterate()`
 
+If you need to send additional headers in the websockets connection, you can pass an optional `headers` parameter
+to the `DriverRemoteConnection` constructor.
+
+[source,python]
+g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g',headers={'Header':'Value'}))
+
+
 === Gremlin-Python Sugar
 
 Python supports meta-programming and operator overloading. There are three uses of these techniques in Gremlin-Python that
@@ -307,7 +314,7 @@ g.V().out().map(lambda: "x: len(x.get().value('name'))").sum().toList()
 
 === Limitations
 
-* Traversals that return a `Set` *might* be coerced to a `List` in Python. In the case of Python, number equality 
+* Traversals that return a `Set` *might* be coerced to a `List` in Python. In the case of Python, number equality
 is different from JVM languages which produces different `Set` results when those types are in use. When this case
 is detected during deserialization, the `Set` is coerced to a `List` so that traversals return consistent
 results within a collection across different languages. If a `Set` is needed then convert `List` results
@@ -438,8 +445,8 @@ When a traversal bytecode is sent over a `IRemoteConnection` (e.g. Gremlin Serve
 and then executed. If the same traversal is sent again, translation and compilation can be skipped as the previously
 compiled version should be cached. Many traversals are unique up to some parameterization. For instance,
 `g.V(1).Out("created").Values("name")` is considered different from `g.V(4).Out("created").Values("Name")`
-as they have different script "string" representations. However, `g.V(x).Out("created").Values("name")` with bindings of 
-`{x : 1}` and `{x : 4}` are considered the same. If a traversal is going to be executed repeatedly, but with different 
+as they have different script "string" representations. However, `g.V(x).Out("created").Values("name")` with bindings of
+`{x : 1}` and `{x : 4}` are considered the same. If a traversal is going to be executed repeatedly, but with different
 parameters, then bindings should be used. In Gremlin.Net, bindings are objects that can be created as follows.
 
 [source,csharp]
diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/client.py b/gremlin-python/src/main/jython/gremlin_python/driver/client.py
index ec3d2ec..288bea0 100644
--- a/gremlin-python/src/main/jython/gremlin_python/driver/client.py
+++ b/gremlin-python/src/main/jython/gremlin_python/driver/client.py
@@ -38,8 +38,10 @@ class Client:
 
     def __init__(self, url, traversal_source, protocol_factory=None,
                  transport_factory=None, pool_size=None, max_workers=None,
-                 message_serializer=None, username="", password=""):
+                 message_serializer=None, username="", password="",
+                 headers=None):
         self._url = url
+        self._headers = headers
         self._traversal_source = traversal_source
         if message_serializer is None:
             message_serializer = serializer.GraphSONSerializersV3d0()
@@ -102,7 +104,8 @@ class Client:
         protocol = self._protocol_factory()
         return connection.Connection(
             self._url, self._traversal_source, protocol,
-            self._transport_factory, self._executor, self._pool)
+            self._transport_factory, self._executor, self._pool,
+            headers=self._headers)
 
     def submit(self, message, bindings=None):
         return self.submitAsync(message, bindings=bindings).result()
diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/connection.py b/gremlin-python/src/main/jython/gremlin_python/driver/connection.py
index df6a57e..d2ef4e7 100644
--- a/gremlin-python/src/main/jython/gremlin_python/driver/connection.py
+++ b/gremlin-python/src/main/jython/gremlin_python/driver/connection.py
@@ -28,8 +28,9 @@ __author__ = 'David M. Brown (davebshow@gmail.com)'
 class Connection:
 
     def __init__(self, url, traversal_source, protocol, transport_factory,
-                 executor, pool):
+                 executor, pool, headers=None):
         self._url = url
+        self._headers = headers
         self._traversal_source = traversal_source
         self._protocol = protocol
         self._transport_factory = transport_factory
@@ -43,7 +44,7 @@ class Connection:
         if self._transport:
             self._transport.close()
         self._transport = self._transport_factory()
-        self._transport.connect(self._url)
+        self._transport.connect(self._url, self._headers)
         self._protocol.connection_made(self._transport)
         self._inited = True
 
diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py
index d447cbd..deedd4e 100644
--- a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py
+++ b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py
@@ -30,7 +30,8 @@ class DriverRemoteConnection(RemoteConnection):
     def __init__(self, url, traversal_source, protocol_factory=None,
                  transport_factory=None, pool_size=None, max_workers=None,
                  username="", password="", message_serializer=None,
-                 graphson_reader=None, graphson_writer=None):
+                 graphson_reader=None, graphson_writer=None,
+                 headers=None):
         if message_serializer is None:
             message_serializer = serializer.GraphSONMessageSerializer(
                 reader=graphson_reader,
@@ -42,7 +43,8 @@ class DriverRemoteConnection(RemoteConnection):
                                      max_workers=max_workers,
                                      message_serializer=message_serializer,
                                      username=username,
-                                     password=password)
+                                     password=password,
+                                     headers=headers)
         self._url = self._client._url
         self._traversal_source = self._client._traversal_source
 
diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/tornado/transport.py b/gremlin-python/src/main/jython/gremlin_python/driver/tornado/transport.py
index 94d6f18..fcc6ac7 100644
--- a/gremlin-python/src/main/jython/gremlin_python/driver/tornado/transport.py
+++ b/gremlin-python/src/main/jython/gremlin_python/driver/tornado/transport.py
@@ -17,6 +17,7 @@ specific language governing permissions and limitations
 under the License.
 """
 from tornado import ioloop, websocket
+from tornado import httpclient
 
 from gremlin_python.driver.transport import AbstractBaseTransport
 
@@ -28,7 +29,9 @@ class TornadoTransport(AbstractBaseTransport):
     def __init__(self):
         self._loop = ioloop.IOLoop(make_current=False)
 
-    def connect(self, url):
+    def connect(self, url, headers=None):
+        if headers:
+            url = httpclient.HTTPRequest(url, headers=headers)
         self._ws = self._loop.run_sync(
             lambda: websocket.websocket_connect(url))
 
diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/transport.py b/gremlin-python/src/main/jython/gremlin_python/driver/transport.py
index 9181956..9641636 100644
--- a/gremlin-python/src/main/jython/gremlin_python/driver/transport.py
+++ b/gremlin-python/src/main/jython/gremlin_python/driver/transport.py
@@ -26,7 +26,7 @@ __author__ = 'David M. Brown (davebshow@gmail.com)'
 class AbstractBaseTransport:
 
     @abc.abstractmethod
-    def connect(self, url):
+    def connect(self, url, headers=None):
         pass
 
     @abc.abstractmethod