/// <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>(IViewQuery query) { IViewResult <T> viewResult = new ViewResult <T>(); try { var result = await HttpClient.GetAsync(query.RawUri()); var content = result.Content; var stream = await content.ReadAsStreamAsync(); viewResult = Mapper.Map <ViewResult <T> >(stream); viewResult.Success = result.IsSuccessStatusCode; viewResult.StatusCode = result.StatusCode; } catch (AggregateException ae) { ae.Flatten().Handle(e => { Log.Error(e); return(true); }); } 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>(IViewQuery query) { IViewResult <T> viewResult = new ViewResult <T>(); var task = HttpClient.GetAsync(query.RawUri()); try { task.Wait(); var result = task.Result; var content = result.Content; var stream = content.ReadAsStreamAsync(); stream.Wait(); viewResult = Mapper.Map <ViewResult <T> >(stream.Result); viewResult.Success = result.IsSuccessStatusCode; viewResult.StatusCode = result.StatusCode; } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(e); return(true); }); } 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>(IViewQuery query) { var uri = query.RawUri(); var viewResult = new ViewResult <T>(); try { var result = await HttpClient.GetAsync(uri).ConfigureAwait(false); var content = result.Content; using (var stream = await content.ReadAsStreamAsync().ConfigureAwait(false)) { viewResult = Mapper.Map <ViewResult <T> >(stream); 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"/> 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>(IViewQuery query) { var viewResult = new ViewResult <T>(); try { var requestAwaiter = HttpClient.GetAsync(query.RawUri()).ConfigureAwait(false).GetAwaiter(); var request = requestAwaiter.GetResult(); var responseAwaiter = request.Content.ReadAsStreamAsync().ConfigureAwait(false).GetAwaiter(); var response = responseAwaiter.GetResult(); viewResult = Mapper.Map <ViewResult <T> >(response); viewResult.Success = request.IsSuccessStatusCode; viewResult.StatusCode = request.StatusCode; } catch (AggregateException ae) { ae.Flatten().Handle(e => { ProcessError(e, viewResult); Log.Error(e); return(true); }); } catch (HttpRequestException e) { ProcessError(e, viewResult); Log.Error(e); } 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>(IViewQuery 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(); 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); }
public async Task <IViewResult <TKey, TValue> > ExecuteAsync <TKey, TValue>(IViewQuery query) { using var rootSpan = _tracer.RootSpan(RequestTracing.ServiceIdentifier.View, OperationNames.ViewQuery) .WithTag(CouchbaseTags.Service, RequestTracing.ServiceIdentifier.View) .WithLocalAddress(); using var encodingSpan = rootSpan.StartPayloadEncoding(); var uri = query.RawUri(); rootSpan.WithRemoteAddress(uri); ViewResultBase <TKey, TValue> viewResult; var body = query.CreateRequestBody(); try { _logger.LogDebug("Sending view request to: {uri}", _redactor.SystemData(uri)); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); encodingSpan.Dispose(); using var dispatchSpan = rootSpan.StartDispatch(); var response = await HttpClient.PostAsync(uri, content).ConfigureAwait(false); dispatchSpan.Dispose(); var serializer = query.Serializer ?? _serializer; 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()) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = response.StatusCode, Errors = viewResult.Message } }; } } } catch (OperationCanceledException e) { _logger.LogDebug(LoggingEvents.ViewEvent, e, "View request timeout."); throw new AmbiguousTimeoutException("The view query was timed out via the Token.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } catch (HttpRequestException e) { _logger.LogDebug(LoggingEvents.QueryEvent, e, "View request cancelled."); throw new RequestCanceledException("The view query was canceled.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } UpdateLastActivity(); return(viewResult); }
public async Task <IViewResult <TKey, TValue> > ExecuteAsync <TKey, TValue>(IViewQuery query) { using var rootSpan = RootSpan(OuterRequestSpans.ServiceSpan.ViewQuery, query); rootSpan.WithLocalAddress() .WithOperation(query); var uri = query.RawUri(); rootSpan.WithRemoteAddress(uri); using var encodingSpan = rootSpan.EncodingSpan(); ViewResultBase <TKey, TValue> viewResult; var body = query.CreateRequestBody(); try { _logger.LogDebug("Sending view request to: {uri}", _redactor.SystemData(uri)); var content = new StringContent(body, Encoding.UTF8, MediaType.Json); encodingSpan.Dispose(); using var dispatchSpan = rootSpan.DispatchSpan(query); using var httpClient = CreateHttpClient(); // set timeout to infinite so we can stream results without the connection // closing part way through httpClient.Timeout = Timeout.InfiniteTimeSpan; var response = await httpClient.PostAsync(uri, content).ConfigureAwait(false); dispatchSpan.Dispose(); var serializer = query.Serializer ?? _serializer; 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()) { viewResult.NoRetryException = new CouchbaseException() { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = response.StatusCode, Errors = viewResult.Message } }; UpdateLastActivity(); return(viewResult); } if (viewResult.ViewNotFound()) { throw new ViewNotFoundException("The queried view is not found on the server.") { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = response.StatusCode, Errors = viewResult.Message } }; } } } catch (OperationCanceledException e) { //treat as an orphaned response rootSpan.LogOrphaned(); _logger.LogDebug(LoggingEvents.ViewEvent, e, "View request timeout."); throw new AmbiguousTimeoutException("The view query was timed out via the Token.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } catch (HttpRequestException e) { //treat as an orphaned response rootSpan.LogOrphaned(); _logger.LogDebug(LoggingEvents.QueryEvent, e, "View request cancelled."); throw new RequestCanceledException("The view query was canceled.", e) { Context = new ViewContextError { DesignDocumentName = query.DesignDocName, ViewName = query.ViewName, ClientContextId = query.ClientContextId, HttpStatus = HttpStatusCode.RequestTimeout } }; } UpdateLastActivity(); return(viewResult); }