public void TestSetup() { state = Substitute.For <IStreamState>(); state.Name.Returns(StreamName); state.BufferPool.Returns(pool = Substitute.For <IBufferPool>()); state.Statistics.Returns(stats = Substitute.For <IStatisticsCollector>()); state.Settings.Returns(settings = new StreamSettings()); batcher = new BufferSnapshotBatcher(1); contentFactory = new RequestContentFactory(new BufferPool()); responseAnalyzer = new ResponseAnalyzer(ResponseAnalysisContext.Stream); statusAnalyzer = new StatusAnalyzer(); requestSender = Substitute.For <IGateRequestSender>(); sender = new StreamSender( () => GlobalApiKey, state, batcher, contentFactory, requestSender, responseAnalyzer, statusAnalyzer, new SynchronousConsoleLog()); cancellation = new CancellationTokenSource(); SetupBuffers(50, 100, 150); SetupResponses(HerculesStatus.Success); }
public StreamSender( [NotNull] Func <string> globalApiKeyProvider, [NotNull] IStreamState streamState, [NotNull] IBufferSnapshotBatcher snapshotBatcher, [NotNull] IRequestContentFactory contentFactory, [NotNull] IGateRequestSender gateRequestSender, [NotNull] IResponseAnalyzer responseAnalyzer, [NotNull] IStatusAnalyzer statusAnalyzer, [NotNull] ILog log) { this.globalApiKeyProvider = globalApiKeyProvider; this.streamState = streamState; this.snapshotBatcher = snapshotBatcher; this.contentFactory = contentFactory; this.gateRequestSender = gateRequestSender; this.responseAnalyzer = responseAnalyzer; this.statusAnalyzer = statusAnalyzer; this.log = log; }
public StreamSenderFactory(HerculesSinkSettings settings, ILog log) { this.log = log; var bufferPool = new BufferPool(settings.MaximumBatchSize * 2, settings.MaxParallelStreams * 8); apiKeyProvider = settings.ApiKeyProvider; contentFactory = new RequestContentFactory(bufferPool); snapshotBatcher = new BufferSnapshotBatcher(settings.MaximumBatchSize); requestSender = new GateRequestSender( settings.Cluster, settings.SuppressVerboseLogging ? log.WithMinimumLevel(LogLevel.Warn).WithLevelsTransformation(SuppressVerboseLoggingLevelsTransformation) : log, bufferPool, settings.AdditionalSetup); responseAnalyzer = new ResponseAnalyzer(ResponseAnalysisContext.Stream); statusAnalyzer = new StatusAnalyzer(); }
private Task <TResult> SendAsync <TResult>(Request request, TimeSpan timeout, IResponseAnalyzer analyzer, Func <HerculesResult, TResult> resultFactory) => SendAsync(request, null, timeout, analyzer).ContinueWith(task => resultFactory(task.GetAwaiter().GetResult()));
private async Task <HerculesResult <TPayload> > SendAsync <TDto, TPayload>(Request request, TimeSpan timeout, IResponseAnalyzer analyzer, Func <TDto, TPayload> converter) { try { var result = await client.SendAsync(WithManagementApiTimeout(request, timeout), timeout).ConfigureAwait(false); var payload = default(TPayload); var status = analyzer.Analyze(result.Response, out var errorMessage); if (status == HerculesStatus.Success) { payload = converter(Serializer.Deserialize <TDto>(result.Response.Content.ToMemoryStream())); } return(new HerculesResult <TPayload>(status, payload, errorMessage)); } catch (Exception error) { log.Error(error); return(new HerculesResult <TPayload>(HerculesStatus.UnknownError, default, error.Message));
private async Task <HerculesResult> SendAsync(Request request, object requestDto, TimeSpan timeout, IResponseAnalyzer analyzer) { try { if (requestDto != null) { request = request.WithContentTypeHeader(Constants.ContentTypes.Json); request = request.WithContent(Serializer.Serialize(requestDto)); } var result = await client.SendAsync(WithManagementApiTimeout(request, timeout), timeout).ConfigureAwait(false); var status = analyzer.Analyze(result.Response, out var errorMessage); return(new HerculesResult(status, errorMessage)); } catch (Exception error) { log.Error(error); return(new HerculesResult(HerculesStatus.UnknownError, error.Message)); } }
private Task <HerculesResult <TPayload> > SendAsync <TPayload>(Request request, TimeSpan timeout, IResponseAnalyzer analyzer) => SendAsync <TPayload, TPayload>(request, timeout, analyzer, _ => _);