public async Task <IQueryResult <T> > QueryAsync <T>(string statement, IQueryOptions options, CancellationToken cancellationToken) { //TODO make url selection round-robin var clusterNode = Configuration.GlobalNodes.GetRandom(x => x.HasQuery()); options.Statement(statement); var body = options.GetFormValuesAsJson(); StreamingQueryResult <T> queryResult; using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json)) { try { var response = await HttpClient.PostAsync(clusterNode.QueryUri, content, cancellationToken) .ConfigureAwait(false); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); queryResult = new StreamingQueryResult <T> { ResponseStream = stream, HttpStatusCode = response.StatusCode, Success = response.StatusCode == HttpStatusCode.OK, }; if (response.StatusCode != HttpStatusCode.OK) { //read the header and stop when we reach the queried rows queryResult.ReadToRows(); if (response.StatusCode != HttpStatusCode.OK || queryResult.Status != QueryStatus.Success) { throw new QueryException(queryResult.Message, queryResult.Status, queryResult.Errors); } } } catch (TaskCanceledException e) { throw new TimeoutException("The request has timed out.", e); } } return(queryResult); }
public async Task <IQueryResult <T> > QueryAsync <T>(string statement, IQueryOptions options, CancellationToken cancellationToken) { var uriBuilder = new UriBuilder(Configuration.Servers.GetRandom()) { Scheme = "http", Path = "/query", Port = 8093 }; options.Statement(statement); var body = options.GetFormValuesAsJson(); StreamingQueryResult <T> queryResult = null; using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json)) { try { var response = await HttpClient.PostAsync(uriBuilder.Uri, content, cancellationToken) .ConfigureAwait(false); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); queryResult = new StreamingQueryResult <T> { ResponseStream = stream, HttpStatusCode = response.StatusCode, Success = response.StatusCode == HttpStatusCode.OK, }; if (response.StatusCode != HttpStatusCode.OK) { //read the header and stop when we reach the queried rows queryResult.ReadToRows(); //A problem with the HTTP request itself if (response.StatusCode == HttpStatusCode.BadRequest) { throw new QueryErrorException(response.ReasonPhrase, queryResult.Status, queryResult.Errors); } //A problem with the service itself if (response.StatusCode == HttpStatusCode.ServiceUnavailable) { throw new QueryServiceException(response.ReasonPhrase, queryResult.Status, queryResult.Errors); } //A problem with the query iteself if (queryResult.Status != QueryStatus.Success) { throw new QueryException(queryResult.Message, queryResult.Status, queryResult.Errors); } } } catch (TaskCanceledException e) { throw new TimeoutException("The request has timed out."); } } return(queryResult); }