status attribute for gremlin dotnet
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/cf50f510 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/cf50f510 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/cf50f510 Branch: refs/heads/TINKERPOP-1913 Commit: cf50f510e9b876e016cfac73e71407684f005684 Parents: 87f963c Author: Ashwini Singh <ashwini.mn...@gmail.com> Authored: Mon May 14 11:42:54 2018 -0700 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Tue Sep 18 12:34:37 2018 -0400 ---------------------------------------------------------------------- .../src/Gremlin.Net/Driver/Connection.cs | 13 ++- .../src/Gremlin.Net/Driver/ResultSet.cs | 87 ++++++++++++++++++++ .../Driver/GremlinClientTests.cs | 20 +++++ 3 files changed, 117 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cf50f510/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs index 279c708..3663191 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs @@ -80,6 +80,7 @@ namespace Gremlin.Net.Driver private async Task<IReadOnlyCollection<T>> ReceiveAsync<T>() { + ResultSet<T> resultSet = new ResultSet<T>(); ResponseStatus status; IAggregator aggregator = null; var isAggregatingSideEffects = false; @@ -114,11 +115,17 @@ namespace Gremlin.Net.Driver result.Add(d); } } + + if (status.Code == ResponseStatusCode.Success || status.Code == ResponseStatusCode.NoContent) + { + resultSet.StatusAttributes = receivedMsg.Status.Attributes; + } + } while (status.Code == ResponseStatusCode.PartialContent || status.Code == ResponseStatusCode.Authenticate); - if (isAggregatingSideEffects) - return new List<T> {(T) aggregator.GetAggregatedResult()}; - return result; + resultSet.Data = isAggregatingSideEffects ? new List<T> {(T) aggregator.GetAggregatedResult()} : result; + + return resultSet; } private async Task AuthenticateAsync() http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cf50f510/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs new file mode 100644 index 0000000..1dd4d7b --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs @@ -0,0 +1,87 @@ +#region License + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#endregion + +using Gremlin.Net.Driver.Exceptions; +using System.Collections; +using System.Collections.Generic; + +namespace Gremlin.Net.Driver +{ + /// <summary> + /// Ashiwni + /// </summary> + /// <typeparam name="T"></typeparam> + public sealed class ResultSet<T> : IReadOnlyCollection<T> + { + /// <summary> + /// Gets and Sets the read only collection + /// </summary> + public IReadOnlyCollection<T> Data { get; set; } + + /// <summary> + /// Gets or Sets the status attributes from the gremlin response + /// </summary> + public Dictionary<string, object> StatusAttributes { get; set; } + + /// <summary>Returns an enumerator that iterates through the collection.</summary> + /// <returns>An enumerator that can be used to iterate through the collection.</returns> + public IEnumerator<T> GetEnumerator() + { + return this.Data.GetEnumerator(); + } + + /// <summary>Returns an enumerator that iterates through a collection.</summary> + /// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns> + IEnumerator IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + + /// <summary>Gets the number of elements in the collection.</summary> + /// <returns>The number of elements in the collection. </returns> + public int Count => this.Data.Count; + } + + /// <summary> + /// Extension for IReadOnlyCollection + /// </summary> + public static class ResultSetExtensions + { + /// <summary> + /// Casts a IReadOnlyCollection to ResultSet + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="data"></param> + /// <returns></returns> + public static ResultSet<T> AsResultSet<T>(this IReadOnlyCollection<T> data) + { + if (!(data is ResultSet<T> resultSet)) + { + throw new ResponseException($"IReadOnlyCollection is not of type ResultSet"); + } + + return resultSet; + } + } + +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cf50f510/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientTests.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientTests.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientTests.cs index 351b83d..a7e191f 100644 --- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientTests.cs +++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientTests.cs @@ -29,6 +29,7 @@ using Gremlin.Net.Driver.Exceptions; using Gremlin.Net.Driver.Messages; using Gremlin.Net.IntegrationTest.Util; using Xunit; +using Gremlin.Net.Structure.IO.GraphSON; namespace Gremlin.Net.IntegrationTest.Driver { @@ -168,6 +169,25 @@ namespace Gremlin.Net.IntegrationTest.Driver } [Fact] + public async Task ShouldReturnResponseAttributes() + { + var gremlinServer = new GremlinServer(TestHost, TestPort); + using (var gremlinClient = new GremlinClient(gremlinServer)) + { + var expectedResult = new List<int> { 1, 2, 3, 4, 5 }; + var requestMsg = $"{nameof(expectedResult)}"; + var bindings = new Dictionary<string, object> { { nameof(expectedResult), expectedResult } }; + + var response = await gremlinClient.SubmitAsync<int>(requestMsg, bindings); + + var resultSet = response.AsResultSet(); + + Assert.NotNull(resultSet.StatusAttributes); + + } + } + + [Fact] public async Task ShouldThrowOnExecutionOfSimpleInvalidScript() { var gremlinServer = new GremlinServer(TestHost, TestPort);