You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by "xiazcy (via GitHub)" <gi...@apache.org> on 2026/03/19 20:48:52 UTC

[PR] Add interceptors for .NET HTTP [tinkerpop]

xiazcy opened a new pull request, #3334:
URL: https://github.com/apache/tinkerpop/pull/3334

   Currently targeting the feature branch for easy diff checking, will rebase and update once the non-streaming HTTP PR get merged. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "kenhuuu (via GitHub)" <gi...@apache.org>.
kenhuuu commented on code in PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#discussion_r2976941187


##########
gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs:
##########
@@ -80,44 +103,90 @@ public Connection(Uri uri, IMessageSerializer serializer,
         /// <summary>
         ///     Constructor that accepts a pre-configured HttpClient (for testing).
         /// </summary>
-        internal Connection(Uri uri, IMessageSerializer serializer,
-            ConnectionSettings settings, HttpClient httpClient)
+        internal Connection(Uri uri, IMessageSerializer? requestSerializer,
+            IMessageSerializer responseSerializer,
+            ConnectionSettings settings, HttpClient httpClient,
+            IReadOnlyList<Func<HttpRequestContext, Task>>? interceptors = null)
         {
             _uri = uri;
-            _serializer = serializer;
+            _requestSerializer = requestSerializer;
+            _responseSerializer = responseSerializer;
             _settings = settings;
             _httpClient = httpClient;
+            _interceptors = interceptors ?? Array.Empty<Func<HttpRequestContext, Task>>();
         }
 
         public async Task<ResultSet<T>> SubmitAsync<T>(RequestMessage requestMessage,
             CancellationToken cancellationToken = default)
         {
-            var requestBytes = await _serializer.SerializeMessageAsync(requestMessage, cancellationToken)
-                .ConfigureAwait(false);
-
-            using var content = new ByteArrayContent(requestBytes);
-            content.Headers.ContentType = new MediaTypeHeaderValue(GraphBinaryMimeType);
-
-            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, _uri);
-            httpRequest.Content = content;
-            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(GraphBinaryMimeType));
+            // Build HttpRequestContext with default headers
+            var headers = new Dictionary<string, string>();
+            headers["Accept"] = GraphBinaryMimeType;

Review Comment:
   Nit: This should be based on `responseSerializer` not hardcoded, but I guess it works for now since GraphBinary4 is the only serializer. I imagine there will be future versions of GraphBinary though.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "Cole-Greer (via GitHub)" <gi...@apache.org>.
Cole-Greer commented on code in PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#discussion_r2977040487


##########
gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs:
##########
@@ -80,44 +103,90 @@ public Connection(Uri uri, IMessageSerializer serializer,
         /// <summary>
         ///     Constructor that accepts a pre-configured HttpClient (for testing).
         /// </summary>
-        internal Connection(Uri uri, IMessageSerializer serializer,
-            ConnectionSettings settings, HttpClient httpClient)
+        internal Connection(Uri uri, IMessageSerializer? requestSerializer,
+            IMessageSerializer responseSerializer,
+            ConnectionSettings settings, HttpClient httpClient,
+            IReadOnlyList<Func<HttpRequestContext, Task>>? interceptors = null)
         {
             _uri = uri;
-            _serializer = serializer;
+            _requestSerializer = requestSerializer;
+            _responseSerializer = responseSerializer;
             _settings = settings;
             _httpClient = httpClient;
+            _interceptors = interceptors ?? Array.Empty<Func<HttpRequestContext, Task>>();
         }
 
         public async Task<ResultSet<T>> SubmitAsync<T>(RequestMessage requestMessage,
             CancellationToken cancellationToken = default)
         {
-            var requestBytes = await _serializer.SerializeMessageAsync(requestMessage, cancellationToken)
-                .ConfigureAwait(false);
-
-            using var content = new ByteArrayContent(requestBytes);
-            content.Headers.ContentType = new MediaTypeHeaderValue(GraphBinaryMimeType);
-
-            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, _uri);
-            httpRequest.Content = content;
-            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(GraphBinaryMimeType));
+            // Build HttpRequestContext with default headers
+            var headers = new Dictionary<string, string>();
+            headers["Accept"] = GraphBinaryMimeType;
 
             if (_settings.EnableCompression)
             {
-                httpRequest.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
+                headers["Accept-Encoding"] = "deflate";
             }
 
             if (_settings.EnableUserAgentOnConnect)
             {
-                httpRequest.Headers.TryAddWithoutValidation("User-Agent", Utils.UserAgent);
+                headers["User-Agent"] = Utils.UserAgent;
             }
 
             if (_settings.BulkResults)
             {
-                httpRequest.Headers.Add("bulkResults", "true");
+                headers["bulkResults"] = "true";
             }
 
-            // Future: apply interceptors here
+            object body;
+            if (_requestSerializer != null)
+            {
+                // Default path: serialize before interceptors
+                var requestBytes = await _requestSerializer.SerializeMessageAsync(requestMessage, cancellationToken)
+                    .ConfigureAwait(false);
+                body = requestBytes;
+                headers["Content-Type"] = GraphBinaryMimeType;

Review Comment:
   Similar to the above comment, this should be based on `_requestSerializer` itself



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "Cole-Greer (via GitHub)" <gi...@apache.org>.
Cole-Greer commented on code in PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#discussion_r2977187005


##########
gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs:
##########
@@ -80,44 +103,90 @@ public Connection(Uri uri, IMessageSerializer serializer,
         /// <summary>
         ///     Constructor that accepts a pre-configured HttpClient (for testing).
         /// </summary>
-        internal Connection(Uri uri, IMessageSerializer serializer,
-            ConnectionSettings settings, HttpClient httpClient)
+        internal Connection(Uri uri, IMessageSerializer? requestSerializer,
+            IMessageSerializer responseSerializer,
+            ConnectionSettings settings, HttpClient httpClient,
+            IReadOnlyList<Func<HttpRequestContext, Task>>? interceptors = null)
         {
             _uri = uri;
-            _serializer = serializer;
+            _requestSerializer = requestSerializer;
+            _responseSerializer = responseSerializer;
             _settings = settings;
             _httpClient = httpClient;
+            _interceptors = interceptors ?? Array.Empty<Func<HttpRequestContext, Task>>();
         }
 
         public async Task<ResultSet<T>> SubmitAsync<T>(RequestMessage requestMessage,
             CancellationToken cancellationToken = default)
         {
-            var requestBytes = await _serializer.SerializeMessageAsync(requestMessage, cancellationToken)
-                .ConfigureAwait(false);
-
-            using var content = new ByteArrayContent(requestBytes);
-            content.Headers.ContentType = new MediaTypeHeaderValue(GraphBinaryMimeType);
-
-            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, _uri);
-            httpRequest.Content = content;
-            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(GraphBinaryMimeType));
+            // Build HttpRequestContext with default headers
+            var headers = new Dictionary<string, string>();
+            headers["Accept"] = GraphBinaryMimeType;
 
             if (_settings.EnableCompression)
             {
-                httpRequest.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
+                headers["Accept-Encoding"] = "deflate";
             }
 
             if (_settings.EnableUserAgentOnConnect)
             {
-                httpRequest.Headers.TryAddWithoutValidation("User-Agent", Utils.UserAgent);
+                headers["User-Agent"] = Utils.UserAgent;
             }
 
             if (_settings.BulkResults)
             {
-                httpRequest.Headers.Add("bulkResults", "true");
+                headers["bulkResults"] = "true";
             }
 
-            // Future: apply interceptors here
+            object body;
+            if (_requestSerializer != null)
+            {
+                // Default path: serialize before interceptors
+                var requestBytes = await _requestSerializer.SerializeMessageAsync(requestMessage, cancellationToken)
+                    .ConfigureAwait(false);
+                body = requestBytes;
+                headers["Content-Type"] = GraphBinaryMimeType;
+            }
+            else
+            {
+                // Interceptor-managed path: body is RequestMessage, interceptor must serialize
+                body = requestMessage;
+            }
+
+            var context = new HttpRequestContext("POST", _uri, headers, body);
+
+            // Apply interceptors in order
+            foreach (var interceptor in _interceptors)
+            {
+                await interceptor(context).ConfigureAwait(false);
+            }
+
+            // Validate body is byte[] after all interceptors
+            if (context.Body is not byte[] bodyBytes)
+            {
+                throw new InvalidOperationException(
+                    "Request body must be byte[] after all interceptors complete, " +
+                    "but found " + (context.Body?.GetType().Name ?? "null") +
+                    ". Either provide a requestSerializer or add an interceptor " +
+                    "that serializes the RequestMessage to byte[].");
+            }
+
+            // Convert HttpRequestContext to HttpRequestMessage
+            using var httpRequest = new HttpRequestMessage(new HttpMethod(context.Method), context.Uri);
+            httpRequest.Content = new ByteArrayContent(bodyBytes);

Review Comment:
   Nit: I believe this constraint can be relaxed to give users more flexibility with interceptors.
   ```suggestion
               // Validate body is byte[] or HttpContentafter all interceptors
               if (context.Body is not byte[] && context.Body is not HttpContent)
               {
                   throw new InvalidOperationException(
                       "Request body must be byte[] or HttpContent after all interceptors complete, " +
                       "but found " + (context.Body?.GetType().Name ?? "null") +
                       ". Either provide a requestSerializer or add an interceptor " +
                       "that serializes the RequestMessage to byte[].");
               }
   
               // Convert HttpRequestContext to HttpRequestMessage
               using var httpRequest = new HttpRequestMessage(new HttpMethod(context.Method), context.Uri);
               httpRequest.Content = context.Body is byte[] bodyBytes ? new ByteArrayContent(bodyBytes) : (HttpContent) context.Body;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "kenhuuu (via GitHub)" <gi...@apache.org>.
kenhuuu commented on PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#issuecomment-4099345520

   There's a bit of a design issue with the way this is currently implemented. The `HttpRequestContext` allows for setting/getting the body, but the body is immediately serialized before the interceptors run and the only supported serializer is GraphBinary. This means that users really have no chance to modify the body at all and they can only read it, otherwise, their interceptor would need to deserialize the body, make changes and then re-serialize.
   
   In my opinion, one of two things need to be changed:
   - split `Auth` away from interceptors and remove Body from interceptors to make it clear that only headers can change in interceptors but Auth can read the body
   - Don't serialize immediately and put the body in a form that is easily modifiable, but this will require more understanding from the user about how the system works. This is effectively what the Java driver does.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "xiazcy (via GitHub)" <gi...@apache.org>.
xiazcy commented on PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#issuecomment-4112031816

   VOTE +1


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "xiazcy (via GitHub)" <gi...@apache.org>.
xiazcy commented on PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#issuecomment-4111943421

   > There's a bit of a design issue with the way this is currently implemented. The `HttpRequestContext` allows for setting/getting the body, but the body is immediately serialized before the interceptors run and the only supported serializer is GraphBinary. This means that users really have no chance to modify the body at all and they can only read it, otherwise, their interceptor would need to deserialize the body, make changes and then re-serialize.
   
   Updated to be similar to python, where request and response serializers are separate options and a nullable request serializer allows the request to not be serialized when null, and leave to the interceptors to serialize. 
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "kenhuuu (via GitHub)" <gi...@apache.org>.
kenhuuu commented on PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#issuecomment-4113783649

   VOTE +1


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "xiazcy (via GitHub)" <gi...@apache.org>.
xiazcy merged PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "Cole-Greer (via GitHub)" <gi...@apache.org>.
Cole-Greer commented on PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#issuecomment-4113247571

   VOTE +1 pending resolution of minor comments above.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add interceptors for .NET HTTP [tinkerpop]

Posted by "Cole-Greer (via GitHub)" <gi...@apache.org>.
Cole-Greer commented on code in PR #3334:
URL: https://github.com/apache/tinkerpop/pull/3334#discussion_r2977031249


##########
gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs:
##########
@@ -80,44 +103,90 @@ public Connection(Uri uri, IMessageSerializer serializer,
         /// <summary>
         ///     Constructor that accepts a pre-configured HttpClient (for testing).
         /// </summary>
-        internal Connection(Uri uri, IMessageSerializer serializer,
-            ConnectionSettings settings, HttpClient httpClient)
+        internal Connection(Uri uri, IMessageSerializer? requestSerializer,
+            IMessageSerializer responseSerializer,
+            ConnectionSettings settings, HttpClient httpClient,
+            IReadOnlyList<Func<HttpRequestContext, Task>>? interceptors = null)
         {
             _uri = uri;
-            _serializer = serializer;
+            _requestSerializer = requestSerializer;
+            _responseSerializer = responseSerializer;
             _settings = settings;
             _httpClient = httpClient;
+            _interceptors = interceptors ?? Array.Empty<Func<HttpRequestContext, Task>>();
         }
 
         public async Task<ResultSet<T>> SubmitAsync<T>(RequestMessage requestMessage,
             CancellationToken cancellationToken = default)
         {
-            var requestBytes = await _serializer.SerializeMessageAsync(requestMessage, cancellationToken)
-                .ConfigureAwait(false);
-
-            using var content = new ByteArrayContent(requestBytes);
-            content.Headers.ContentType = new MediaTypeHeaderValue(GraphBinaryMimeType);
-
-            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, _uri);
-            httpRequest.Content = content;
-            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(GraphBinaryMimeType));
+            // Build HttpRequestContext with default headers
+            var headers = new Dictionary<string, string>();
+            headers["Accept"] = GraphBinaryMimeType;

Review Comment:
   This will also be problematic if any users who provide their own custom serializer implementations.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org