/// <summary> /// Executes a <see cref="IViewQuery"/> asynchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns> public async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query) { var uri = query.RawUri(); var viewResult = new ViewResult <T>(); try { var result = await HttpClient.GetAsync(uri).ContinueOnAnyContext(); var content = result.Content; using (var stream = await content.ReadAsStreamAsync().ContinueOnAnyContext()) { viewResult = Mapper.Map <ViewResultData <T> >(stream).ToViewResult(); viewResult.Success = result.IsSuccessStatusCode; viewResult.StatusCode = result.StatusCode; viewResult.Message = Success; } } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(uri, e); return(true); }); } catch (TaskCanceledException e) { const string error = "The request has timed out."; ProcessError(e, error, viewResult); Log.Error(uri, e); } return(viewResult); }
/// <summary> /// Executes a <see cref="IViewQuery"/> asynchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns> public override async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query) { var uri = query.RawUri(); var viewResult = new StreamingViewResult <T>(); try { var response = await HttpClient.GetAsync(uri).ContinueOnAnyContext(); viewResult = new StreamingViewResult <T>( response.IsSuccessStatusCode, response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ContinueOnAnyContext() ); } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); return(true); }); } catch (TaskCanceledException e) { const string error = "The request has timed out."; ProcessError(e, error, viewResult); Log.Error(uri.ToString(), e); } return(viewResult); }
/// <summary> /// Executes a <see cref="IViewQuery"/> asynchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns> public override async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query) { var uri = query.RawUri(); var viewResult = new ViewResult <T>(); var body = query.CreateRequestBody(); try { Log.Debug("Sending view request to: {0}", uri.ToString()); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); var response = await HttpClient.PostAsync(uri, content).ContinueOnAnyContext(); if (response.IsSuccessStatusCode) { using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { viewResult = DataMapper.Map <ViewResultData <T> >(stream).ToViewResult(); viewResult.Success = response.IsSuccessStatusCode; viewResult.StatusCode = response.StatusCode; viewResult.Message = Success; } } else { viewResult = new ViewResult <T> { Success = false, StatusCode = response.StatusCode, Message = response.ReasonPhrase }; } } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); return(true); }); } catch (TaskCanceledException e) { const string error = "The request has timed out."; ProcessError(e, error, viewResult); Log.Error(uri.ToString(), e); } catch (HttpRequestException e) { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); } UpdateLastActivity(); return(viewResult); }
/// <summary> /// Executes a <see cref="IViewQuery"/> synchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>The <see cref="IViewResult{T}"/> instance which is the results of the query.</returns> public IViewResult <T> Execute <T>(IViewQueryable query) { var viewResult = new ViewResult <T>(); try { var request = WebRequest.Create(query.RawUri()); request.Timeout = _clientConfig.ViewRequestTimeout; WriteAuthenticationHeaders(request, _bucketConfig.Name, _bucketConfig.Password); using (var response = request.GetResponse() as HttpWebResponse) { var stream = response.GetResponseStream(); viewResult = Mapper.Map <ViewResult <T> >(stream); response.Close(); viewResult.Success = response.StatusCode == HttpStatusCode.OK; viewResult.StatusCode = response.StatusCode; } } catch (WebException e) { if (e.Response != null) { var stream = e.Response.GetResponseStream(); if (stream != null && stream.Length > 0) { viewResult = Mapper.Map <ViewResult <T> >(stream); } } viewResult.Exception = e; viewResult.StatusCode = GetStatusCode(e.Message); Log.Error(e); } catch (Exception e) { ProcessError(e, viewResult); Log.Error(e); } return(viewResult); }
/// <summary> /// Executes a <see cref="IViewQuery"/> asynchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns> public override async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query) { var uri = query.RawUri(); var viewResult = new ViewResult <T>(); string body; using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.RequestEncoding).Start()) { body = query.CreateRequestBody(); } try { Log.Debug("Sending view request to: {0}", uri.ToString()); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); HttpResponseMessage response; using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.DispatchToServer).Start()) { response = await HttpClient.PostAsync(uri, content).ContinueOnAnyContext(); } if (response.IsSuccessStatusCode) { using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.ResponseDecoding).Start()) using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { viewResult = DataMapper.Map <ViewResultData <T> >(stream).ToViewResult(); viewResult.Success = response.IsSuccessStatusCode; viewResult.StatusCode = response.StatusCode; viewResult.Message = Success; } } else { viewResult = new ViewResult <T> { Success = false, StatusCode = response.StatusCode, Message = response.ReasonPhrase }; } } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); return(true); }); } catch (OperationCanceledException e) { var operationContext = OperationContext.CreateViewContext(query.BucketName, uri?.Authority); if (_viewTimeout.HasValue) { operationContext.TimeoutMicroseconds = _viewTimeout.Value; } ProcessError(e, operationContext.ToString(), viewResult); Log.Error(uri.ToString(), e); } catch (HttpRequestException e) { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); } UpdateLastActivity(); return(viewResult); }
/// <summary> /// Executes a <see cref="IViewQuery"/> asynchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns> public async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query) { var uri = query.RawUri(); ViewResult <T> viewResult = null; string body; //using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.RequestEncoding).StartActive()) //{ body = query.CreateRequestBody(); //} try { //Log.Debug("Sending view request to: {0}", uri.ToString()); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); HttpResponseMessage response; //using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.DispatchToServer).StartActive()) //{ response = await HttpClient.PostAsync(uri, content).ConfigureAwait(false); //} if (response.IsSuccessStatusCode) { viewResult = new ViewResult <T>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false) //ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.ResponseDecoding).StartActive().Span ); } else { viewResult = new ViewResult <T>(response.StatusCode, response.ReasonPhrase); } } catch (AggregateException ae) { ae.Flatten().Handle(e => { viewResult = CreateErrorResult <T>(e); //Log.Error(uri.ToString(), e); return(true); }); } catch (OperationCanceledException e) { var operationContext = OperationContext.CreateViewContext(query.BucketName, uri?.Authority); if (_viewTimeout.HasValue) { operationContext.TimeoutMicroseconds = _viewTimeout.Value; } viewResult = CreateErrorResult <T>(e, operationContext.ToString()); //Log.Error(uri.ToString(), e); } catch (HttpRequestException e) { viewResult = CreateErrorResult <T>(e); //Log.Error(uri.ToString(), e); } UpdateLastActivity(); return(viewResult); }
/// <summary> /// Executes a <see cref="IViewQuery"/> asynchronously against a View. /// </summary> /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam> /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param> /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns> public override async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query) { var uri = query.RawUri(); var viewResult = new StreamingViewResult <T>(); string body; using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.RequestEncoding).Start()) { body = query.CreateRequestBody(); } try { Log.Debug("Sending view request to: {0}", uri.ToString()); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); HttpResponseMessage response; using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.DispatchToServer).Start()) { response = await HttpClient.PostAsync(uri, content).ContinueOnAnyContext(); } if (response.IsSuccessStatusCode) { viewResult = new StreamingViewResult <T>( response.IsSuccessStatusCode, response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ContinueOnAnyContext(), ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.ResponseDecoding).Start() ); } else { viewResult = new StreamingViewResult <T> { Success = false, StatusCode = response.StatusCode, Message = response.ReasonPhrase }; } } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); return(true); }); } catch (TaskCanceledException e) { const string error = "The request has timed out."; ProcessError(e, error, viewResult); Log.Error(uri.ToString(), e); } catch (HttpRequestException e) { ProcessError(e, viewResult); Log.Error(uri.ToString(), e); } UpdateLastActivity(); return(viewResult); }
/// <inheritdoc /> public async Task <IViewResult <TKey, TValue> > ExecuteAsync <TKey, TValue>(IViewQueryable query) { var uri = query.RawUri(); ViewResultBase <TKey, TValue> viewResult; var body = query.CreateRequestBody(); try { Log.LogDebug("Sending view request to: {0}", uri.ToString()); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); var response = await HttpClient.PostAsync(uri, content).ConfigureAwait(false); if (response.IsSuccessStatusCode) { if (_serializer is IStreamingTypeDeserializer streamingTypeDeserializer) { viewResult = new StreamingViewResult <TKey, TValue>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), streamingTypeDeserializer ); } else { viewResult = new BlockViewResult <TKey, TValue>( response.StatusCode, Success, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), _serializer ); } await viewResult.InitializeAsync().ConfigureAwait(false); } else { if (_serializer is IStreamingTypeDeserializer streamingTypeDeserializer) { viewResult = new StreamingViewResult <TKey, TValue>( response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false), streamingTypeDeserializer ); } else { viewResult = new BlockViewResult <TKey, TValue>( response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false), _serializer ); } await viewResult.InitializeAsync().ConfigureAwait(false); if (viewResult.ShouldRetry()) { UpdateLastActivity(); return(viewResult); } if (viewResult.ViewNotFound()) { throw new ViewNotFoundException(uri.ToString()); } } } catch (OperationCanceledException e) { Log.LogDebug(LoggingEvents.ViewEvent, e, "View request timeout."); throw new AmbiguousTimeoutException("The view query was timed out via the Token.", e); } catch (HttpRequestException e) { Log.LogDebug(LoggingEvents.QueryEvent, e, "View request cancelled."); throw new RequestCanceledException("The view query was canceled.", e); } UpdateLastActivity(); return(viewResult); }