Skip to content

Commit 0f7b402

Browse files
authored
Refactoring responses (#53)
1 parent 38d8f54 commit 0f7b402

File tree

15 files changed

+38
-60
lines changed

15 files changed

+38
-60
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44

@@ -10,7 +10,7 @@
1010
<LangVersion>latest</LangVersion>
1111
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1212
<IsPackable>False</IsPackable>
13-
13+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
1414
<SignAssembly>true</SignAssembly>
1515
<AssemblyOriginatorKeyFile>$(SolutionRoot)\build\keys\keypair.snk</AssemblyOriginatorKeyFile>
1616

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "6.0.202",
3+
"version": "6.0.403",
44
"rollForward": "latestFeature",
55
"allowPrerelease": false
66
}

src/Elastic.Transport.VirtualizedCluster/Audit/Auditor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public async Task<Auditor> TraceElasticsearchExceptionOnResponse(ClientCall call
128128
Action call = () => { Response = _cluster.ClientCall(callTrace?.RequestOverrides); };
129129
call();
130130

131-
if (Response.ApiCallDetails.Success) throw new Exception("Expected call to not be valid");
131+
if (Response.ApiCallDetails.HasSuccessfulStatusCode) throw new Exception("Expected call to not be valid");
132132

133133
if (Response.ApiCallDetails.OriginalException is not TransportException exception)
134134
throw new Exception("OriginalException on response is not expected TransportException");
@@ -142,7 +142,7 @@ public async Task<Auditor> TraceElasticsearchExceptionOnResponse(ClientCall call
142142
_clusterAsync.ClientThrows(false);
143143
Func<Task> callAsync = async () => { ResponseAsync = await _clusterAsync.ClientCallAsync(callTrace?.RequestOverrides).ConfigureAwait(false); };
144144
await callAsync().ConfigureAwait(false);
145-
if (Response.ApiCallDetails.Success) throw new Exception("Expected call to not be valid");
145+
if (Response.ApiCallDetails.HasSuccessfulStatusCode) throw new Exception("Expected call to not be valid");
146146
exception = ResponseAsync.ApiCallDetails.OriginalException as TransportException;
147147
if (exception == null) throw new Exception("OriginalException on response is not expected TransportException");
148148
assert(exception);

src/Elastic.Transport/Components/Pipeline/DefaultRequestPipeline.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public override TResponse CallProductEndpoint<TResponse>(RequestData requestData
187187
apiCallDetails.AuditTrail = AuditTrail;
188188

189189
ThrowBadAuthPipelineExceptionWhenNeeded(response.ApiCallDetails, response);
190-
if (!response.ApiCallDetails.Success) audit.Event = requestData.OnFailureAuditEvent;
190+
if (!response.ApiCallDetails.HasSuccessfulStatusCode) audit.Event = requestData.OnFailureAuditEvent;
191191
return response;
192192
}
193193
catch (Exception e)
@@ -215,7 +215,7 @@ public override async Task<TResponse> CallProductEndpointAsync<TResponse>(Reques
215215
apiCallDetails.AuditTrail = AuditTrail;
216216

217217
ThrowBadAuthPipelineExceptionWhenNeeded(response.ApiCallDetails, response);
218-
if (!response.ApiCallDetails.Success) audit.Event = requestData.OnFailureAuditEvent;
218+
if (!response.ApiCallDetails.HasSuccessfulStatusCode) audit.Event = requestData.OnFailureAuditEvent;
219219
return response;
220220
}
221221
catch (Exception e)
@@ -230,7 +230,7 @@ public override async Task<TResponse> CallProductEndpointAsync<TResponse>(Reques
230230
public override TransportException CreateClientException<TResponse>(
231231
TResponse response, ApiCallDetails callDetails, RequestData data, List<PipelineException> pipelineExceptions)
232232
{
233-
if (callDetails?.Success ?? false) return null;
233+
if (callDetails?.HasSuccessfulStatusCode ?? false) return null;
234234

235235
var pipelineFailure = data.OnFailurePipelineFailure;
236236
var innerException = callDetails?.OriginalException;
@@ -424,7 +424,7 @@ public override void Ping(Node node)
424424
d.EndState = response.ApiCallDetails;
425425
ThrowBadAuthPipelineExceptionWhenNeeded(response.ApiCallDetails);
426426
//ping should not silently accept bad but valid http responses
427-
if (!response.ApiCallDetails.Success)
427+
if (!response.ApiCallDetails.HasSuccessfulStatusCode)
428428
throw new PipelineException(pingData.OnFailurePipelineFailure, response.ApiCallDetails.OriginalException) { Response = response };
429429
}
430430
catch (Exception e)
@@ -454,7 +454,7 @@ public override async Task PingAsync(Node node, CancellationToken cancellationTo
454454
d.EndState = response.ApiCallDetails;
455455
ThrowBadAuthPipelineExceptionWhenNeeded(response.ApiCallDetails);
456456
//ping should not silently accept bad but valid http responses
457-
if (!response.ApiCallDetails.Success)
457+
if (!response.ApiCallDetails.HasSuccessfulStatusCode)
458458
throw new PipelineException(pingData.OnFailurePipelineFailure, response.ApiCallDetails.OriginalException) { Response = response };
459459
}
460460
catch (Exception e)
@@ -488,7 +488,7 @@ public override void Sniff()
488488

489489
ThrowBadAuthPipelineExceptionWhenNeeded(response.ApiCallDetails);
490490
//sniff should not silently accept bad but valid http responses
491-
if (!response.ApiCallDetails.Success)
491+
if (!response.ApiCallDetails.HasSuccessfulStatusCode)
492492
throw new PipelineException(requestData.OnFailurePipelineFailure, response.ApiCallDetails.OriginalException) { Response = response };
493493

494494
_nodePool.Reseed(nodes);
@@ -528,7 +528,7 @@ public override async Task SniffAsync(CancellationToken cancellationToken)
528528

529529
ThrowBadAuthPipelineExceptionWhenNeeded(response.ApiCallDetails);
530530
//sniff should not silently accept bad but valid http responses
531-
if (!response.ApiCallDetails.Success)
531+
if (!response.ApiCallDetails.HasSuccessfulStatusCode)
532532
throw new PipelineException(requestData.OnFailurePipelineFailure, response.ApiCallDetails.OriginalException) { Response = response };
533533

534534
_nodePool.Reseed(nodes);

src/Elastic.Transport/Components/Pipeline/DefaultResponseBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ IReadOnlyDictionary<TcpState, int> tcpStats
131131

132132
var details = new ApiCallDetails
133133
{
134-
Success = success,
134+
HasSuccessfulStatusCode = success,
135135
OriginalException = exception,
136136
HttpStatusCode = statusCode,
137137
RequestBodyInBytes = requestData.PostData?.WrittenBytes,

src/Elastic.Transport/Configuration/ITransportConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public interface ITransportConfiguration : IDisposable
247247
UserAgent UserAgent { get; }
248248

249249
/// <summary>
250-
/// Allow you to override the status code inspection that sets <see cref="ApiCallDetails.Success"/>
250+
/// Allow you to override the status code inspection that sets <see cref="ApiCallDetails.HasSuccessfulStatusCode"/>
251251
/// <para>
252252
/// Defaults to validating the statusCode is greater or equal to 200 and less then 300
253253
/// </para>

src/Elastic.Transport/Elastic.Transport.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<ItemGroup>
2222
<InternalsVisibleTo Include="Elastic.Transport.VirtualizedCluster" Key="$(ExposedPublicKey)" />
2323
<InternalsVisibleTo Include="Elastic.Transport.Tests" Key="$(ExposedPublicKey)" />
24+
<InternalsVisibleTo Include="Elastic.Transport.Benchmarks" Key="$(ExposedPublicKey)" />
25+
<InternalsVisibleTo Include="Elastic.Transport.IntegrationTests" Key="$(ExposedPublicKey)" />
2426
</ItemGroup>
2527

2628
<ItemGroup>

src/Elastic.Transport/Products/Elasticsearch/ElasticsearchResponse.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ public IEnumerable<string> ElasticsearchWarnings
3434
/// <summary>
3535
///
3636
/// </summary>
37-
/// <returns></returns>
3837
[JsonIgnore]
3938
public string DebugInformation
4039
{
4140
get
4241
{
4342
var sb = new StringBuilder();
44-
sb.Append($"{(!IsValid ? "Inv" : "V")}alid Elastic.Clients.Elasticsearch response built from a ");
43+
sb.Append($"{(!IsValidResponse ? "Inv" : "V")}alid Elasticsearch response built from a ");
4544
sb.AppendLine(ApiCallDetails?.ToString().ToCamelCase() ??
4645
"null ApiCall which is highly exceptional, please open a bug if you see this");
47-
if (!IsValid)
46+
if (!IsValidResponse)
4847
DebugIsValid(sb);
4948

5049
if (ApiCallDetails.ParsedHeaders is not null && ApiCallDetails.ParsedHeaders.TryGetValue("warning", out var warnings))
@@ -57,34 +56,34 @@ public string DebugInformation
5756

5857
if (ApiCallDetails != null)
5958
Diagnostics.ResponseStatics.DebugInformationBuilder(ApiCallDetails, sb);
59+
6060
return sb.ToString();
6161
}
6262
}
6363

6464
/// <summary>
65-
///
65+
/// Shortcut to test if the response is considered to be successful.
6666
/// </summary>
67-
/// <returns></returns>
67+
/// <returns>A <see cref="bool"/> indicating success or failure.</returns>
6868
[JsonIgnore]
69-
public virtual bool IsValid
69+
public virtual bool IsValidResponse
7070
{
7171
get
7272
{
7373
var statusCode = ApiCallDetails?.HttpStatusCode;
7474

75-
// TODO - Review this on a request by reqeust basis
7675
if (statusCode == 404)
7776
return false;
7877

79-
return (ApiCallDetails?.Success ?? false) && (!ServerError?.HasError() ?? true);
78+
return (ApiCallDetails?.HasSuccessfulStatusCode ?? false) && (!ElasticsearchServerError?.HasError() ?? true);
8079
}
8180
}
8281

8382
/// <summary>
8483
///
8584
/// </summary>
8685
[JsonIgnore]
87-
public ElasticsearchServerError ServerError { get; internal set; }
86+
public ElasticsearchServerError ElasticsearchServerError { get; internal set; }
8887

8988
/// <summary>
9089
///
@@ -112,5 +111,5 @@ protected virtual void DebugIsValid(StringBuilder sb) { }
112111
/// </summary>
113112
/// <returns></returns>
114113
public override string ToString() =>
115-
$"{(!IsValid ? "Inv" : "V")}alid Elastic.Clients.Elasticsearch response built from a {ApiCallDetails?.ToString().ToCamelCase()}";
114+
$"{(!IsValidResponse ? "Inv" : "V")}alid Elastic.Clients.Elasticsearch response built from a {ApiCallDetails?.ToString().ToCamelCase()}";
116115
}

src/Elastic.Transport/Products/Elasticsearch/ElasticsearchResponseBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected override void SetErrorOnResponse<TResponse>(TResponse response, Elasti
1414
{
1515
if (response is ElasticsearchResponse elasticResponse)
1616
{
17-
elasticResponse.ServerError = error;
17+
elasticResponse.ElasticsearchServerError = error;
1818
}
1919
}
2020

src/Elastic.Transport/Products/ProductRegistration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public abstract RequestData CreateSniffRequestData(Node node, IRequestConfigurat
9292

9393
/// <summary>
9494
/// Used by <see cref="ResponseBuilder"/> to determine if it needs to return true or false for
95-
/// <see cref="ApiCallDetails.Success"/>
95+
/// <see cref="ApiCallDetails.HasSuccessfulStatusCode"/>
9696
/// </summary>
9797
public abstract bool HttpStatusCodeClassifier(HttpMethod method, int statusCode);
9898

0 commit comments

Comments
 (0)