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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]