public async Task CreateLinkAsync(AnalyticsLink link, CreateAnalyticsLinkOptions?options = null) { link.ValidateForRequest(); options ??= new(); try { var builder = new UriBuilder(_serviceUriProvider.GetRandomAnalyticsUri()); builder.Path = link.ManagementPath; var uri = builder.Uri; var formContent = new FormUrlEncodedContent(link.FormData); var result = await _couchbaseHttpClient.PostAsync(uri, formContent, options.CancellationToken).ConfigureAwait(false); await HandleLinkManagementResultErrors(result, link); } catch (Exception exception) { _logger.LogError(exception, "Failed to create link."); throw; } }
public async Task <IAnalyticsResult <T> > QueryAsync <T>(IAnalyticsRequest queryRequest, CancellationToken token = default) { // try get Analytics node var analyticsUri = _serviceUriProvider.GetRandomAnalyticsUri(); AnalyticsResultBase <T> result; var body = queryRequest.GetFormValuesAsJson(); using (var content = new StringContent(body, Encoding.UTF8, MediaType.Json)) { try { var request = new HttpRequestMessage(HttpMethod.Post, analyticsUri) { Content = content }; if (queryRequest is AnalyticsRequest req && req.PriorityValue != 0) { request.Headers.Add(AnalyticsPriorityHeaderName, new[] { req.PriorityValue.ToString() }); } var response = await HttpClient.SendAsync(request, token).ConfigureAwait(false); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); if (_typeSerializer is IStreamingTypeDeserializer streamingTypeDeserializer) { result = new StreamingAnalyticsResult <T>(stream, streamingTypeDeserializer) { HttpStatusCode = response.StatusCode }; } else { result = new BlockAnalyticsResult <T>(stream, _typeSerializer) { HttpStatusCode = response.StatusCode }; } await result.InitializeAsync(token).ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.OK) { if (result.ShouldRetry()) { UpdateLastActivity(); return(result); } if (result.LinkNotFound()) { throw new LinkNotFoundException(); } if (result.DataverseExists()) { throw new DataverseExistsException(); } if (result.DatasetExists()) { throw new DatasetExistsException(); } if (result.DataverseNotFound()) { throw new DataverseNotFoundException(); } if (result.DataSetNotFound()) { throw new DatasetNotFoundException(); } if (result.JobQueueFull()) { throw new JobQueueFullException(); } if (result.CompilationFailure()) { throw new CompilationFailureException(); } if (result.InternalServerFailure()) { throw new InternalServerFailureException(); } if (result.AuthenticationFailure()) { throw new AuthenticationFailureException(); } if (result.TemporaryFailure()) { throw new TemporaryFailureException(); } if (result.ParsingFailure()) { throw new ParsingFailureException(); } if (result.IndexNotFound()) { throw new IndexNotFoundException(); } if (result.IndexExists()) { throw new IndexExistsException(); } } } catch (OperationCanceledException e) { _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request timeout."); if (queryRequest.ReadOnly) { throw new UnambiguousTimeoutException("The query was timed out via the Token.", e); } throw new AmbiguousTimeoutException("The query was timed out via the Token.", e); } catch (HttpRequestException e) { _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request cancelled."); throw new RequestCanceledException("The query was canceled.", e); } } UpdateLastActivity(); return(result); }
public async Task <IAnalyticsResult <T> > QueryAsync <T>(string statement, AnalyticsOptions options) { using var rootSpan = RootSpan(OuterRequestSpans.ServiceSpan.AnalyticsQuery, options) .WithOperationId(options) .WithLocalAddress(); // try get Analytics node var analyticsUri = _serviceUriProvider.GetRandomAnalyticsUri(); rootSpan.WithRemoteAddress(analyticsUri); _logger.LogDebug("Sending analytics query with a context id {contextId} to server {searchUri}", options.ClientContextIdValue, analyticsUri); using var encodingSpan = rootSpan.EncodingSpan(); AnalyticsResultBase <T> result; var body = options.GetFormValuesAsJson(statement); using (var content = new StringContent(body, Encoding.UTF8, MediaType.Json)) { try { var request = new HttpRequestMessage(HttpMethod.Post, analyticsUri) { Content = content }; if (options.PriorityValue != 0) { request.Headers.Add(AnalyticsPriorityHeaderName, new[] { options.PriorityValue.ToString() }); } encodingSpan.Dispose(); using var dispatchSpan = rootSpan.DispatchSpan(options); var response = await HttpClient.SendAsync(request, options.Token).ConfigureAwait(false); dispatchSpan.Dispose(); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); if (_typeSerializer is IStreamingTypeDeserializer streamingTypeDeserializer) { result = new StreamingAnalyticsResult <T>(stream, streamingTypeDeserializer) { HttpStatusCode = response.StatusCode }; } else { result = new BlockAnalyticsResult <T>(stream, _typeSerializer) { HttpStatusCode = response.StatusCode }; } await result.InitializeAsync(options.Token).ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.OK) { if (result.ShouldRetry()) { UpdateLastActivity(); return(result); } var context = new AnalyticsErrorContext { ClientContextId = options.ClientContextIdValue, HttpStatus = response.StatusCode, Statement = statement, Parameters = options.GetParametersAsJson(), Errors = result.Errors }; if (result.LinkNotFound()) { throw new LinkNotFoundException(context); } if (result.DataverseExists()) { throw new DataverseExistsException(context); } if (result.DatasetExists()) { throw new DatasetExistsException(); } if (result.DataverseNotFound()) { throw new DataverseNotFoundException(context); } if (result.DataSetNotFound()) { throw new DatasetNotFoundException(context); } if (result.JobQueueFull()) { throw new JobQueueFullException(context); } if (result.CompilationFailure()) { throw new CompilationFailureException(context); } if (result.InternalServerFailure()) { throw new InternalServerFailureException(context); } if (result.AuthenticationFailure()) { throw new AuthenticationFailureException(context); } if (result.TemporaryFailure()) { throw new TemporaryFailureException(context); } if (result.ParsingFailure()) { throw new ParsingFailureException(context); } if (result.IndexNotFound()) { throw new IndexNotFoundException(context); } if (result.IndexExists()) { throw new IndexExistsException(context); } } } catch (OperationCanceledException e) { //treat as an orphaned response rootSpan.LogOrphaned(); var context = new AnalyticsErrorContext { ClientContextId = options.ClientContextIdValue, Statement = statement, Parameters = options.GetParametersAsJson() }; _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request timeout."); if (options.ReadonlyValue) { throw new UnambiguousTimeoutException("The query was timed out via the Token.", e) { Context = context }; } throw new AmbiguousTimeoutException("The query was timed out via the Token.", e) { Context = context }; } catch (HttpRequestException e) { //treat as an orphaned response rootSpan.LogOrphaned(); var context = new AnalyticsErrorContext { ClientContextId = options.ClientContextIdValue, Statement = statement, Parameters = options.GetParametersAsJson() }; _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request cancelled."); throw new RequestCanceledException("The query was canceled.", e) { Context = context }; } } UpdateLastActivity(); return(result); }