/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> private async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest) { var queryResult = new QueryResult <T>(); using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, "application/json")) { try { var request = await HttpClient.PostAsync(queryRequest.GetBaseUri(), content); using (var response = await request.Content.ReadAsStreamAsync()) { queryResult = DataMapper.Map <QueryResult <T> >(response); queryResult.Success = queryResult.Status == QueryStatus.Success; } } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Error(e); ProcessError(e, queryResult); return(true); }); } } return(queryResult); }
protected override async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest) { ApplyCredentials(queryRequest); var baseUri = ConfigContextBase.GetQueryUri(); var queryResult = new StreamingQueryResult <T>(); using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, MediaType.Json)) { try { var requestMessage = new HttpRequestMessage(HttpMethod.Post, baseUri) { Content = content }; HttpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite); Log.TraceFormat("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); var response = await HttpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead).ContinueOnAnyContext(); var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext(); { queryResult = new StreamingQueryResult <T> { ResponseStream = stream, HttpStatusCode = response.StatusCode, Success = response.StatusCode == HttpStatusCode.OK, QueryTimer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfig.EnableQueryTiming) }; Log.TraceFormat("Received query cid{0}: {1}", queryRequest.CurrentContextId, queryResult.HttpStatusCode); } baseUri.ClearFailed(); } catch (HttpRequestException e) { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } return(queryResult); }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <param name="cancellationToken">Token which can cancel the query.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> protected virtual async Task<IQueryResult<T>> ExecuteQueryAsync<T>(IQueryRequest queryRequest, CancellationToken cancellationToken) { var queryResult = new QueryResult<T>(); FailureCountingUri baseUri; if (!TryGetQueryUri(out baseUri)) { Log.Error(ExceptionUtil.EmptyUriTryingSubmitN1qlQuery); ProcessError(new InvalidOperationException(ExceptionUtil.EmptyUriTryingSubmitN1QlQuery), queryResult); return queryResult; } ApplyCredentials(queryRequest); using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, MediaType.Json)) { try { using (var timer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfiguration.EnableQueryTiming)) { Log.Trace("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); var request = await HttpClient.PostAsync(baseUri, content, cancellationToken).ContinueOnAnyContext(); using (var response = await request.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { queryResult = GetDataMapper(queryRequest).Map<QueryResultData<T>>(response).ToQueryResult(); queryResult.Success = queryResult.Status == QueryStatus.Success; queryResult.HttpStatusCode = request.StatusCode; Log.Trace("Received query cid{0}: {1}", queryResult.ClientContextId, queryResult.ToString()); timer.ClusterElapsedTime = queryResult.Metrics.ElaspedTime; } } baseUri.ClearFailed(); } catch (HttpRequestException e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return true; }); } catch (Exception e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } return queryResult; }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> IQueryResult <T> ExecuteQuery <T>(IQueryRequest queryRequest) { var queryResult = new QueryResult <T>(); var baseUri = ConfigContextBase.GetQueryUri(); try { var request = WebRequest.Create(baseUri); request.Timeout = (int)_clientConfig.QueryRequestTimeout; request.Method = "POST"; request.ContentType = "application/json"; var authType = "Basic"; var credBytes = System.Text.Encoding.UTF8.GetBytes(string.Concat(_bucketConfig.Name, ":", _bucketConfig.Password)); var credentials = string.Concat(authType, " ", Convert.ToBase64String(credBytes)); request.Headers[HttpRequestHeader.Authorization] = credentials; var json = queryRequest.GetFormValuesAsJson(); var bytes = System.Text.Encoding.UTF8.GetBytes(json); request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } var response = request.GetResponse(); using (var stream = response.GetResponseStream()) { queryResult = GetDataMapper(queryRequest).Map <QueryResult <T> >(stream); queryResult.Success = queryResult.Status == QueryStatus.Success; queryResult.HttpStatusCode = queryResult.HttpStatusCode; } baseUri.ClearFailed(); } catch (HttpRequestException e) { baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (WebException e) { if (e.Response != null) { var stream = e.Response.GetResponseStream(); queryResult = GetDataMapper(queryRequest).Map <QueryResult <T> >(stream); queryResult.HttpStatusCode = ((HttpWebResponse)e.Response).StatusCode; } queryResult.Exception = e; Log.Error(e); } catch (Exception e) { ProcessError(e, queryResult); Log.Error(e); } return(queryResult); }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> protected virtual async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest) { ApplyCredentials(queryRequest); var baseUri = ConfigContextBase.GetQueryUri(); var queryResult = new QueryResult <T>(); using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, MediaType.Json)) { try { using (var timer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfig.EnableQueryTiming)) { Log.TraceFormat("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); var request = await HttpClient.PostAsync(baseUri, content).ContinueOnAnyContext(); using (var response = await request.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { queryResult = GetDataMapper(queryRequest).Map <QueryResultData <T> >(response).ToQueryResult(); queryResult.Success = queryResult.Status == QueryStatus.Success; queryResult.HttpStatusCode = request.StatusCode; Log.TraceFormat("Received query cid{0}: {1}", queryResult.ClientContextId, queryResult.ToString()); timer.ClusterElapsedTime = queryResult.Metrics.ElaspedTime; } } baseUri.ClearFailed(); } catch (HttpRequestException e) { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } return(queryResult); }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> private async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest) { var baseUri = ConfigContextBase.GetQueryUri(); var queryResult = new QueryResult <T>(); using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, "application/json")) { try { Log.TraceFormat("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); var request = await HttpClient.PostAsync(baseUri, content).ContinueOnAnyContext(); using (var response = await request.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { queryResult = GetDataMapper(queryRequest).Map <QueryResult <T> >(response); queryResult.Success = queryResult.Status == QueryStatus.Success; queryResult.HttpStatusCode = request.StatusCode; Log.TraceFormat("Received query cid{0}: {1}", queryResult.ClientContextId, queryResult.ToString()); } baseUri.ClearFailed(); } catch (HttpRequestException e) { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.InfoFormat("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } return(queryResult); }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> IQueryResult <T> ExecuteQuery <T>(IQueryRequest queryRequest) { var queryResult = new QueryResult <T>(); try { var request = WebRequest.Create(queryRequest.GetBaseUri()); request.Timeout = (int)_clientConfig.QueryRequestTimeout; request.Method = "POST"; request.ContentType = "application/json"; var json = queryRequest.GetFormValuesAsJson(); var bytes = System.Text.Encoding.UTF8.GetBytes(json); request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } var response = request.GetResponse(); using (var stream = response.GetResponseStream()) { queryResult = DataMapper.Map <QueryResult <T> >(stream); queryResult.Success = queryResult.Status == QueryStatus.Success; } } catch (WebException e) { if (e.Response != null) { var stream = e.Response.GetResponseStream(); queryResult = DataMapper.Map <QueryResult <T> >(stream); queryResult.HttpStatusCode = ((HttpWebResponse)e.Response).StatusCode; } queryResult.Exception = e; Log.Error(e); } catch (Exception e) { ProcessError(e, queryResult); Log.Error(e); } return(queryResult); }
protected override async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest, CancellationToken cancellationToken) { var queryResult = new StreamingQueryResult <T>(); FailureCountingUri baseUri; if (!TryGetQueryUri(out baseUri)) { Log.Error(ExceptionUtil.EmptyUriTryingSubmitN1qlQuery); ProcessError(new InvalidOperationException(ExceptionUtil.EmptyUriTryingSubmitN1QlQuery), queryResult); return(queryResult); } ApplyCredentials(queryRequest); string body; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.RequestEncoding).Start()) { body = queryRequest.GetFormValuesAsJson(); } using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json)) { try { var requestMessage = new HttpRequestMessage(HttpMethod.Post, baseUri) { Content = content }; Log.Trace("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); HttpResponseMessage response; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.DispatchToServer).Start()) { response = await HttpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ContinueOnAnyContext(); } var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext(); { queryResult = new StreamingQueryResult <T> { ResponseStream = stream, HttpStatusCode = response.StatusCode, Success = response.StatusCode == HttpStatusCode.OK, QueryTimer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfiguration.EnableQueryTiming), DecodeSpan = ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.ResponseDecoding).Start() }; Log.Trace("Received query cid{0}: {1}", queryRequest.CurrentContextId, queryResult.HttpStatusCode); } baseUri.ClearFailed(); } catch (OperationCanceledException e) { var operationContext = OperationContext.CreateQueryContext(queryRequest.CurrentContextId, Context.BucketName, baseUri?.Authority); if (queryRequest is QueryRequest request) { operationContext.TimeoutMicroseconds = request.TimeoutValue; } Log.Info(operationContext.ToString()); ProcessError(e, queryResult); } catch (HttpRequestException e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } UpdateLastActivity(); return(queryResult); }
protected override async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest, CancellationToken cancellationToken) { var queryResult = new StreamingQueryResult <T>(); FailureCountingUri baseUri; if (!TryGetQueryUri(out baseUri)) { Log.Error(ExceptionUtil.EmptyUriTryingSubmitN1qlQuery); ProcessError(new InvalidOperationException(ExceptionUtil.EmptyUriTryingSubmitN1QlQuery), queryResult); return(queryResult); } ApplyCredentials(queryRequest); using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, MediaType.Json)) { try { var requestMessage = new HttpRequestMessage(HttpMethod.Post, baseUri) { Content = content }; Log.Trace("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); var response = await HttpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ContinueOnAnyContext(); var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext(); { queryResult = new StreamingQueryResult <T> { ResponseStream = stream, HttpStatusCode = response.StatusCode, Success = response.StatusCode == HttpStatusCode.OK, QueryTimer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfiguration.EnableQueryTiming) }; Log.Trace("Received query cid{0}: {1}", queryRequest.CurrentContextId, queryResult.HttpStatusCode); } baseUri.ClearFailed(); } catch (HttpRequestException e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } return(queryResult); }
/// <summary> /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam> /// <param name="queryRequest">The query request.</param> /// <param name="cancellationToken">Token which can cancel the query.</param> /// <returns></returns> /// <remarks>The format for the querying is JSON</remarks> protected virtual async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest, CancellationToken cancellationToken) { var queryResult = new QueryResult <T>(); if (!TryGetQueryUri(out var baseUri)) { Log.Error(ExceptionUtil.EmptyUriTryingSubmitN1qlQuery); ProcessError(new InvalidOperationException(ExceptionUtil.EmptyUriTryingSubmitN1QlQuery), queryResult); return(queryResult); } ApplyCredentials(queryRequest); if (Log.IsDebugEnabled) { //need to remove the brackets or else string.format will fail in Log.Debug var req = queryRequest.ToString(); Log.Debug(req.Replace("{", "").Replace("}", "")); } string body; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.RequestEncoding).Start()) { body = queryRequest.GetFormValuesAsJson(); } using (var content = new StringContent(body, System.Text.Encoding.UTF8, MediaType.Json)) { try { using (var timer = new QueryTimer(queryRequest, new CommonLogStore(Log), ClientConfiguration.EnableQueryTiming)) { Log.Trace("Sending query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); HttpResponseMessage response; using (ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.DispatchToServer).Start()) { response = await HttpClient.PostAsync(baseUri, content, cancellationToken).ContinueOnAnyContext(); } using (var span = ClientConfiguration.Tracer.BuildSpan(queryRequest, CouchbaseOperationNames.ResponseDecoding).Start()) using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { queryResult = GetDataMapper(queryRequest).Map <QueryResultData <T> >(stream).ToQueryResult(); queryResult.Success = queryResult.Status == QueryStatus.Success; queryResult.HttpStatusCode = response.StatusCode; Log.Trace("Received query cid{0}: {1}", queryResult.ClientContextId, queryResult.ToString()); timer.ClusterElapsedTime = queryResult.Metrics.ElaspedTime; span.SetPeerLatencyTag(queryResult.Metrics.ElaspedTime); } } baseUri.ClearFailed(); } catch (HttpRequestException e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); baseUri.IncrementFailed(); ProcessError(e, queryResult); Log.Error(e); } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); ProcessError(e, queryResult); return(true); }); } catch (Exception e) { Log.Info("Failed query cid{0}: {1}", queryRequest.CurrentContextId, baseUri); Log.Info(e); ProcessError(e, queryResult); } } UpdateLastActivity(); return(queryResult); }