Skip to content

Commit

Permalink
got rid of all ContinueWith in Neo4jClient project and used async/awa…
Browse files Browse the repository at this point in the history
…it instead
  • Loading branch information
tugberkugurlu committed Jul 29, 2015
1 parent 1ab04f1 commit 8ba4615
Showing 1 changed file with 51 additions and 75 deletions.
126 changes: 51 additions & 75 deletions Neo4jClient/GraphClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ HttpResponseMessage SendHttpRequest(HttpRequestMessage request, string commandDe
return task.Result;
}

Task<HttpResponseMessage> SendHttpRequestAsync(HttpRequestMessage request, string commandDescription, params HttpStatusCode[] expectedStatusCodes)
async Task<HttpResponseMessage> SendHttpRequestAsync(HttpRequestMessage request, string commandDescription, params HttpStatusCode[] expectedStatusCodes)
{
if (UseJsonStreamingIfAvailable && jsonStreamingAvailable)
{
Expand All @@ -167,14 +167,10 @@ Task<HttpResponseMessage> SendHttpRequestAsync(HttpRequestMessage request, strin
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
}

var baseTask = httpClient.SendAsync(request);
var continuationTask = baseTask.ContinueWith(requestTask =>
{
var response = requestTask.Result;
response.EnsureExpectedStatusCode(commandDescription, expectedStatusCodes);
return response;
});
return continuationTask;
var response = await httpClient.SendAsync(request).ConfigureAwait(false);
response.EnsureExpectedStatusCode(commandDescription, expectedStatusCodes);

return response;
}

T SendHttpRequestAndParseResultAs<T>(HttpRequestMessage request, params HttpStatusCode[] expectedStatusCodes) where T : new()
Expand Down Expand Up @@ -459,25 +455,20 @@ public virtual Node<TNode> Get<TNode>(NodeReference reference)
return task.Result;
}

public virtual Task<Node<TNode>> GetAsync<TNode>(NodeReference reference)
public virtual async Task<Node<TNode>> GetAsync<TNode>(NodeReference reference)
{
CheckRoot();

var nodeEndpoint = ResolveEndpoint(reference);
return
SendHttpRequestAsync(HttpGet(nodeEndpoint), HttpStatusCode.OK, HttpStatusCode.NotFound)
.ContinueWith(responseTask =>
{
var response = responseTask.Result;
var response = await SendHttpRequestAsync(HttpGet(nodeEndpoint), HttpStatusCode.OK, HttpStatusCode.NotFound).ConfigureAwait(false);

if (response.StatusCode == HttpStatusCode.NotFound)
return (Node<TNode>)null;
if (response.StatusCode == HttpStatusCode.NotFound)
return (Node<TNode>)null;

return response
.Content
.ReadAsJson<NodeApiResponse<TNode>>(JsonConverters,JsonContractResolver)
.ToNode(this);
});
return response
.Content
.ReadAsJson<NodeApiResponse<TNode>>(JsonConverters, JsonContractResolver)
.ToNode(this);
}

public virtual Node<TNode> Get<TNode>(NodeReference<TNode> reference)
Expand All @@ -497,25 +488,20 @@ public virtual Node<TNode> Get<TNode>(NodeReference<TNode> reference)
return task.Result;
}

public virtual Task<RelationshipInstance<TData>> GetAsync<TData>(RelationshipReference reference) where TData : class, new()
public virtual async Task<RelationshipInstance<TData>> GetAsync<TData>(RelationshipReference reference) where TData : class, new()
{
CheckRoot();

var endpoint = ResolveEndpoint(reference);
return
SendHttpRequestAsync(HttpGet(endpoint), HttpStatusCode.OK, HttpStatusCode.NotFound)
.ContinueWith(responseTask =>
{
var response = responseTask.Result;
var response = await SendHttpRequestAsync(HttpGet(endpoint), HttpStatusCode.OK, HttpStatusCode.NotFound).ConfigureAwait(false);

if (response.StatusCode == HttpStatusCode.NotFound)
return (RelationshipInstance<TData>)null;
if (response.StatusCode == HttpStatusCode.NotFound)
return (RelationshipInstance<TData>)null;

return response
.Content
.ReadAsJson<RelationshipApiResponse<TData>>(JsonConverters,JsonContractResolver)
.ToRelationshipInstance(this);
});
return response
.Content
.ReadAsJson<RelationshipApiResponse<TData>>(JsonConverters, JsonContractResolver)
.ToRelationshipInstance(this);
}

public void Update<TNode>(NodeReference<TNode> nodeReference, TNode replacementData, IEnumerable<IndexEntry> indexEntries = null)
Expand Down Expand Up @@ -806,36 +792,32 @@ IEnumerable<TResult> IRawGraphClient.ExecuteGetCypherResults<TResult>(CypherQuer
return task.Result;
}

Task<IEnumerable<TResult>> IRawGraphClient.ExecuteGetCypherResultsAsync<TResult>(CypherQuery query)
async Task<IEnumerable<TResult>> IRawGraphClient.ExecuteGetCypherResultsAsync<TResult>(CypherQuery query)
{
CheckRoot();

var stopwatch = new Stopwatch();
stopwatch.Start();

return
SendHttpRequestAsync(
HttpPostAsJson(RootApiResponse.Cypher, new CypherApiQuery(query)),
string.Format("The query was: {0}", query.QueryText),
HttpStatusCode.OK)
.ContinueWith(responseTask =>
{
var response = responseTask.Result;
var deserializer = new CypherJsonDeserializer<TResult>(this, query.ResultMode);
var results = deserializer
.Deserialize(response.Content.ReadAsString())
.ToList();

stopwatch.Stop();
OnOperationCompleted(new OperationCompletedEventArgs
{
QueryText = query.DebugQueryText,
ResourcesReturned = results.Count(),
TimeTaken = stopwatch.Elapsed
});
var response = await SendHttpRequestAsync(
HttpPostAsJson(RootApiResponse.Cypher, new CypherApiQuery(query)),
string.Format("The query was: {0}", query.QueryText),
HttpStatusCode.OK).ConfigureAwait(false);

return (IEnumerable<TResult>)results;
});
var deserializer = new CypherJsonDeserializer<TResult>(this, query.ResultMode);
var results = deserializer
.Deserialize(response.Content.ReadAsString())
.ToList();

stopwatch.Stop();
OnOperationCompleted(new OperationCompletedEventArgs
{
QueryText = query.DebugQueryText,
ResourcesReturned = results.Count(),
TimeTaken = stopwatch.Elapsed
});

return (IEnumerable<TResult>)results;
}

void IRawGraphClient.ExecuteCypher(CypherQuery query)
Expand All @@ -859,31 +841,25 @@ void IRawGraphClient.ExecuteCypher(CypherQuery query)
});
}

Task IRawGraphClient.ExecuteCypherAsync(CypherQuery query)
async Task IRawGraphClient.ExecuteCypherAsync(CypherQuery query)
{
CheckRoot();

var stopwatch = new Stopwatch();
stopwatch.Start();

return SendHttpRequestAsync(
await SendHttpRequestAsync(
HttpPostAsJson(RootApiResponse.Cypher, new CypherApiQuery(query)),
string.Format("The query was: {0}", query.QueryText),
HttpStatusCode.OK)
.ContinueWith(t =>
{
// Rethrow any exception (instead of using TaskContinuationOptions.OnlyOnRanToCompletion, which for failures, returns a canceled task instead of a faulted task)
var _ = t.Result;

stopwatch.Stop();
OnOperationCompleted(new OperationCompletedEventArgs
{
QueryText = query.DebugQueryText,
ResourcesReturned = 0,
TimeTaken = stopwatch.Elapsed
});
})
;
HttpStatusCode.OK).ConfigureAwait(false);

stopwatch.Stop();
OnOperationCompleted(new OperationCompletedEventArgs
{
QueryText = query.DebugQueryText,
ResourcesReturned = 0,
TimeTaken = stopwatch.Elapsed
});
}

[Obsolete("Gremlin support gets dropped with Neo4j 2.0. Please move to equivalent (but much more powerful and readable!) Cypher.")]
Expand Down

0 comments on commit 8ba4615

Please sign in to comment.