public ElasticsearchClientException(PipelineFailure failure, string message, IApiCallDetails apiCall) : this(message) { Response = apiCall; FailureReason = failure; AuditTrail = apiCall?.AuditTrail; }
//TODO DictionaryResponse private GetIndexResponse DeserializeGetIndexResponse(IApiCallDetails response, Stream stream) { return new GetIndexResponse { Indices = this.Serializer.Deserialize<Dictionary<string, IndexState>>(stream) }; }
//TODO map the response properly, remove list flattening /// <inheritdoc/> private GetAliasesResponse DeserializeGetAliasesResponse(IApiCallDetails apiCallDetails, Stream stream) { if (!apiCallDetails.Success) return new GetAliasesResponse(); var dict = this.Serializer.Deserialize<CrazyAliasesResponse>(stream); var d = new Dictionary<string, IList<AliasDefinition>>(); foreach (var kv in dict) { var indexDict = kv.Key; var aliases = new List<AliasDefinition>(); if (kv.Value != null && kv.Value.ContainsKey("aliases")) { var aliasDict = kv.Value["aliases"]; if (aliasDict != null) aliases = aliasDict.Select(kva => { var alias = kva.Value; alias.Name = kva.Key; return alias; }).ToList(); } d.Add(indexDict, aliases); } return new GetAliasesResponse() { Indices = d }; }
//TODO this is too geared towards getting a single mapping private GetMappingResponse DeserializeGetMappingResponse(IApiCallDetails response, IGetMappingRequest d, Stream stream) { var dict = response.Success ? Serializer.Deserialize <GetRootObjectMappingWrapping>(stream) : null; return(new GetMappingResponse(response, dict)); }
internal GetFieldMappingResponse(IApiCallDetails status, IReadOnlyDictionary <string, TypeFieldMappings> dict, Inferrer inferrer) { this.Indices = dict ?? EmptyReadOnly <string, TypeFieldMappings> .Dictionary; this._inferrer = inferrer; //TODO can dict truely ever be null, whats the response look like when field mapping is not found. //does status.Success not already reflect this? //this.IsValid = status.Success && dict != null && dict.Count > 0; }
static string FormatApiCall(IApiCallDetails details) { int? status = details.HttpStatusCode; bool wasSuccess = details.Success; string path = details.Uri.AbsolutePath; return($"Response for '{path}', success: {wasSuccess}, status: {status}"); }
public static void Handle(IApiCallDetails apiCallDetails) { if (!apiCallDetails.Success) { HandleRequest(apiCallDetails); HandleResponse(apiCallDetails); } }
protected virtual void OnRequestCompleted(IApiCallDetails x) { if (x.RequestBodyInBytes != null && x.RequestBodyInBytes.Length > 1) { var body = Encoding.UTF8.GetString(x.RequestBodyInBytes); } this.requestUri = x.Uri.ToString(); }
internal GetFieldMappingResponse(IApiCallDetails status, IndexFieldMappings dict, ElasticInferrer inferrer) { this.Indices = dict ?? new IndexFieldMappings(); this._inferrer = inferrer; //TODO can dict truely ever be null, whats the response look like when field mapping is not found. //does status.Success not already reflect this? //this.IsValid = status.Success && dict != null && dict.Count > 0; }
private GetCertificatesResponse ToCertificatesResponse(IApiCallDetails apiCallDetails, Stream stream) { var result = RequestResponseSerializer.Deserialize <ClusterCertificateInformation[]>(stream); return(new GetCertificatesResponse { Certificates = result }); }
//c.Body ?? CreateInvalidInstance<TResponse>(c); private static TResponse CreateInvalidInstance <TResponse>(IApiCallDetails response) where TResponse : ResponseBase { var r = typeof(TResponse).CreateInstance <TResponse>(); ((IElasticsearchResponse)r).ApiCall = response; return(r); }
private CapturingUrlTester Assert(string typeOfCall, IApiCallDetails callDetails) { var url = callDetails.Uri.PathAndQuery; url.Should().Be(this.ExpectedUrl, $"when calling the {typeOfCall} Api"); callDetails.HttpMethod.Should().Be(this.ExpectedHttpMethod, typeOfCall); return(this); }
private UrlTester Assert(string typeOfCall, IApiCallDetails callDetails) { var url = callDetails.Uri.PathAndQuery; callDetails.Uri.PathEquals(this.ExpectedUrl, typeOfCall); callDetails.HttpMethod.Should().Be(this.ExpectedHttpMethod, $"{typeOfCall} to {url}"); return(this); }
internal CapturingUrlTester(HttpMethod method, string expectedUrl) { this.ExpectedHttpMethod = method; this.ExpectedUrl = expectedUrl; this.ConnectionSettingsModifier = (c => c .PrettyJson(false) .OnRequestCompleted(h => CallDetails = h)); }
//TODO DictionaryResponse! private GetFieldMappingResponse DeserializeGetFieldMappingResponse(IApiCallDetails response, IGetFieldMappingRequest d, Stream stream) { var dict = response.Success ? Serializer.Deserialize <IndexFieldMappings>(stream) : null; return(new GetFieldMappingResponse(response, dict, this.ConnectionSettings.Inferrer)); }
private TranslateSqlResponse ToTranslateSqlResponse(IApiCallDetails apiCallDetails, Stream stream) { var result = RequestResponseSerializer.Deserialize <ISearchRequest>(stream); return(new TranslateSqlResponse { Result = result }); }
private static TResponse CreateInvalidInstance <TResponse>(IApiCallDetails response) where TResponse : BaseResponse { var r = typeof(TResponse).CreateInstance <TResponse>(); ((IBodyWithApiCallDetails)r).CallDetails = response; return(r); }
public static void LogErrorRequest(this ILogger logger, Exception ex, IApiCallDetails response, string message, params object[] args) { if (response == null || !logger.IsEnabled(LogLevel.Error)) { return; } var sb = new StringBuilder(); var messageArguments = new List <object>(args); if (!String.IsNullOrEmpty(message)) { sb.AppendLine(message); } if (response.OriginalException != null) { sb.AppendLine("Original: [OriginalExceptionType] {OriginalExceptionMessage}"); messageArguments.Add(response.OriginalException.GetType().Name); messageArguments.Add(response.OriginalException.Message); } if (response.ServerError?.Error != null) { sb.AppendLine("Server Error (Index={ErrorIndex} Type={ErrorType}): {ErrorReason}"); messageArguments.Add(response.ServerError.Error.Index); messageArguments.Add(response.ServerError.Error.Type); messageArguments.Add(response.ServerError.Error.Reason); } if (response is IBulkResponse bulkResponse) { sb.AppendLine("Bulk: {BulkErrors}"); messageArguments.Add(String.Join("\r\n", bulkResponse.ItemsWithErrors.Select(i => i.Error))); } sb.AppendLine("[{HttpStatusCode}] {HttpMethod} {HttpPathAndQuery}"); messageArguments.Add(response.HttpStatusCode); messageArguments.Add(response.HttpMethod); messageArguments.Add(response.Uri.PathAndQuery); if (response.RequestBodyInBytes != null) { string body = Encoding.UTF8.GetString(response.RequestBodyInBytes); sb.AppendLine("{HttpBody}"); messageArguments.Add(body); } AggregateException aggEx = null; if (ex != null && response.OriginalException != null) { aggEx = new AggregateException(ex, response.OriginalException); } logger.LogError(aggEx ?? response.OriginalException, sb.ToString(), messageArguments.ToArray()); }
private SourceResponse <T> ToSourceResponse <T>(IApiCallDetails apiCallDetails, Stream stream) where T : class { var source = SourceSerializer.Deserialize <T>(stream); return(new SourceResponse <T> { Body = source, }); }
private CatResponse <TCatRecord> DeserializeCatResponse <TCatRecord>(IApiCallDetails response, Stream stream) where TCatRecord : ICatRecord { var records = this.Serializer.Deserialize <IEnumerable <TCatRecord> >(stream); return(new CatResponse <TCatRecord> { Records = records }); }
private CatResponse <TCatRecord> DeserializeCatResponse <TCatRecord>(IApiCallDetails response, Stream stream) where TCatRecord : ICatRecord { var records = this.RequestResponseSerializer.Deserialize <IReadOnlyCollection <TCatRecord> >(stream); return(new CatResponse <TCatRecord> { Records = records }); }
public ElasticsearchClientException CreateClientException <TResponse>( TResponse response, IApiCallDetails callDetails, RequestData data, List <PipelineException> pipelineExceptions ) where TResponse : class, IElasticsearchResponse, new() { if (callDetails?.Success ?? false) { return(null); } var innerException = pipelineExceptions.HasAny() ? pipelineExceptions.AsAggregateOrFirst() : callDetails?.OriginalException; var statusCode = callDetails?.HttpStatusCode != null?callDetails.HttpStatusCode.Value.ToString() : "unknown"; var resource = callDetails == null ? "unknown resource" : $"Status code {statusCode} from: {callDetails.HttpMethod} {callDetails.Uri.PathAndQuery}"; var exceptionMessage = innerException?.Message ?? $"Request failed to execute"; var pipelineFailure = data.OnFailurePipelineFailure; if (pipelineExceptions.HasAny()) { pipelineFailure = pipelineExceptions.Last().FailureReason; } if (this.IsTakingTooLong) { pipelineFailure = PipelineFailure.MaxTimeoutReached; this.Audit(MaxTimeoutReached); exceptionMessage = "Maximum timeout reached while retrying request"; } else if (this.Retried >= this.MaxRetries && this.MaxRetries > 0) { pipelineFailure = PipelineFailure.MaxRetriesReached; this.Audit(MaxRetriesReached); exceptionMessage = "Maximum number of retries reached"; } exceptionMessage += $". Call: {resource}"; if (response != null && response.TryGetServerErrorReason(out var reason)) { exceptionMessage += $". ServerError: {reason}"; } var clientException = new ElasticsearchClientException(pipelineFailure, exceptionMessage, innerException) { Request = data, Response = callDetails, AuditTrail = this.AuditTrail }; return(clientException); }
private static SourceResponse <T> ToSourceBody <T>(IApiCallDetails apiCallDetails, T source) where T : class { var r = new SourceResponse <T> { Body = source, }; ((IElasticsearchResponse)r).ApiCall = apiCallDetails; return(r); }
private static void RegisterStatement(ISpan span, IApiCallDetails response) { //make sure db exists var db = span.Context.Db ?? (span.Context.Db = new Database { Instance = response.Uri?.GetLeftPart(UriPartial.Authority), Type = Database.TypeElasticsearch }); db.Statement = response.DebugInformation; }
private void ThrowBadAuthPipelineExceptionWhenNeeded(IApiCallDetails response) { if (response.HttpStatusCode == 401) { throw new PipelineException(PipelineFailure.BadAuthentication, response.OriginalException) { Response = response } } ; }
protected override void OnRequestCompleted(IApiCallDetails x) { if (x.RequestBodyInBytes != null && x.RequestBodyInBytes.Length > 1) { var body = Encoding.UTF8.GetString(x.RequestBodyInBytes); var doc = JObject.Parse(body)["doc"].ToString(); var result = this.realMessageSerializer.Deserialize(doc); result.HasError.Should().BeFalse(result.ErrorMessage); this.resultTransEvent = result.Message; } }
private SearchResponse <TResult> FieldsSearchDeserializer <T, TResult>(IApiCallDetails response, Stream stream, ISearchTemplateRequest d) where T : class where TResult : class { var converter = this.CreateCovariantSearchSelector <T, TResult>(d); var dict = response.Success ? new NestSerializer(this.ConnectionSettings, converter).Deserialize <SearchResponse <TResult> >(stream) : null; return(dict); }
public override async Task <object> DeserializeResponseAsync( ITransportSerializer builtInSerializer, IApiCallDetails response, Stream stream, CancellationToken ctx = default ) => response.Success ? await builtInSerializer.CreateStateful(Formatter) .DeserializeAsync <MultiSearchResponse>(stream, ctx) .ConfigureAwait(false) : new MultiSearchResponse();
private GetMappingResponse DeserializeGetMappingResponse(IApiCallDetails response, IGetMappingRequest d, Stream stream) { if (!response.Success) { var emptyResponse = this.RequestResponseSerializer.Deserialize <GetMappingResponse>(stream); return(TransferCallDetails(emptyResponse, response)); } var dict = this.RequestResponseSerializer.Deserialize <GetRootObjectMappingWrapping>(stream); return(TransferCallDetails(new GetMappingResponse(dict), response)); }
private static void ThrowBadAuthPipelineExceptionWhenNeeded(IApiCallDetails details, IElasticsearchResponse response = null) { if (details?.HttpStatusCode == 401) { throw new PipelineException(PipelineFailure.BadAuthentication, details.OriginalException) { Response = response, ApiCall = details } } ; }
private RecoveryStatusResponse DeserializeRecoveryStatusResponse(IApiCallDetails response, Stream stream) { if (!response.Success) { return(CreateInvalidInstance <RecoveryStatusResponse>(response)); } var indices = this.Serializer.Deserialize <Dictionary <string, RecoveryStatus> >(stream); return(new RecoveryStatusResponse { Indices = indices }); }
public void BadResponse <TResponse>(ref TResponse response, IApiCallDetails callDetails, RequestData data, ElasticsearchClientException exception) where TResponse : class, IElasticsearchResponse, new() { if (response == null) { //make sure we copy over the error body in case we disabled direct streaming. var s = callDetails?.ResponseBodyInBytes == null ? Stream.Null : new MemoryStream(callDetails.ResponseBodyInBytes); var m = callDetails?.ResponseMimeType ?? RequestData.MimeType; response = ResponseBuilder.ToResponse <TResponse>(data, exception, callDetails?.HttpStatusCode, null, s, m); } response.ApiCall.AuditTrail = this.AuditTrail; }
public override async Task <object> DeserializeResponseAsync( ITransportSerializer builtInSerializer, IApiCallDetails response, Stream stream, CancellationToken ctx = default ) => response.Success ? new PreviewDatafeedResponse <TDocument> { Data = await builtInSerializer.DeserializeAsync <IReadOnlyCollection <TDocument> >(stream, ctx).ConfigureAwait(false) } : new PreviewDatafeedResponse <TDocument>();
private CatResponse<CatHelpRecord> DeserializeCatHelpResponse(IApiCallDetails response, Stream stream) { using (stream) using (var ms = new MemoryStream()) { stream.CopyTo(ms); var body = ms.ToArray().Utf8String(); return new CatResponse<CatHelpRecord> { Records = body.Split('\n').Skip(1) .Select(f => new CatHelpRecord { Endpoint = f.Trim() }) .ToList() }; } }
/// <summary> /// Because the nodes.hot_threads endpoint returns plain text instead of JSON, we have to /// manually parse the response text into a typed response object. /// </summary> private NodesHotThreadsResponse DeserializeNodesHotThreadResponse(IApiCallDetails response, Stream stream) { using (stream) using (var sr = new StreamReader(stream, Encoding.UTF8)) { var typedResponse = new NodesHotThreadsResponse(); var plainTextResponse = sr.ReadToEnd(); // If the response doesn't start with :::, which is the pattern that delimits // each node section in the response, then the response format isn't recognized. // Just return an empty response object. This is especially useful when unit // testing against an in-memory connection where you won't get a real response. if (!plainTextResponse.StartsWith(":::", StringComparison.Ordinal)) return typedResponse; var sections = plainTextResponse.Split(new string[] { ":::" }, StringSplitOptions.RemoveEmptyEntries); var info = from section in sections select section.Split(new string[] {"\n \n"}, StringSplitOptions.None) into sectionLines where sectionLines.Length > 0 let nodeLine = sectionLines.FirstOrDefault() where nodeLine != null let matches = NodeRegex.Match(nodeLine) where matches.Success let node = matches.Groups["name"].Value let nodeId = matches.Groups["id"].Value let hosts = matches.Groups["hosts"].Value.Split(new[] {'{', '}'}, StringSplitOptions.RemoveEmptyEntries) let threads = sectionLines.Skip(1).Take(sectionLines.Length - 1).ToList() select new HotThreadInformation { NodeName = node, NodeId = nodeId, Threads = threads, Hosts = hosts }; return new NodesHotThreadsResponse(info.ToList()); } }
private ExistsResponse DeserializeExistsResponse(IApiCallDetails response, Stream stream) => new ExistsResponse(response);
internal ExistsResponse(IApiCallDetails apiCallDetails) { this.Exists = apiCallDetails.Success & apiCallDetails.HttpStatusCode == 200; }
public ApiCallDetailsOverride(IApiCallDetails original, bool isValid) { _original = original; _isValid = isValid; }
public ApiCallDetailsOverride(IApiCallDetails original, ServerError error) { _original = original; _error = error; _isValid = false; }
private PingResponse DeserializePingResponse(IApiCallDetails response, Stream stream) => new PingResponse();
private MultiGetResponse DeserializeMultiGetResponse(IApiCallDetails response, Stream stream, JsonConverter converter) => this.ConnectionSettings.StatefulSerializer(converter).Deserialize<MultiGetResponse>(stream);
private RecoveryStatusResponse DeserializeRecoveryStatusResponse(IApiCallDetails response, Stream stream) { if (!response.Success) return CreateInvalidInstance<RecoveryStatusResponse>(response); var indices = this.Serializer.Deserialize<Dictionary<string, RecoveryStatus>>(stream); return new RecoveryStatusResponse { Indices = indices }; }