From 49cc592df9a44f536199d7d24470bbc8571d9e03 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Sun, 9 Jun 2024 16:45:55 +0100 Subject: [PATCH] Update ApiResponse to correct previous adjustment (#1711) --- Refit/ApiResponse.cs | 101 ++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/Refit/ApiResponse.cs b/Refit/ApiResponse.cs index 0cc851374..9e2786c06 100644 --- a/Refit/ApiResponse.cs +++ b/Refit/ApiResponse.cs @@ -29,43 +29,33 @@ internal static T Create( /// Implementation of that provides additional functionalities. /// /// - public sealed class ApiResponse : IApiResponse, IApiResponse + /// + /// Create an instance of with type . + /// + /// Original HTTP Response message. + /// Response content. + /// Refit settings used to send the request. + /// The ApiException, if the request failed. + /// + public sealed class ApiResponse( + HttpResponseMessage response, + T? content, + RefitSettings settings, + ApiException? error = null + ) : IApiResponse, IApiResponse { - readonly HttpResponseMessage response; + readonly HttpResponseMessage response = response ?? throw new ArgumentNullException(nameof(response)); bool disposed; - /// - /// Create an instance of with type . - /// - /// Original HTTP Response message. - /// Response content. - /// Refit settings used to send the request. - /// The ApiException, if the request failed. - /// - public ApiResponse( - HttpResponseMessage response, - T? content, - RefitSettings settings, - ApiException? error = null - ) - { - this.response = response ?? throw new ArgumentNullException(nameof(response)); - Error = error; - Content = content; - Settings = settings; - } - /// /// Deserialized request content as . /// - public T? Content { get; } - - object? IApiResponse.Content => Content; + public T? Content { get; } = content; /// /// Refit settings used to send the request. /// - public RefitSettings Settings { get; } + public RefitSettings Settings { get; } = settings; /// /// HTTP response headers. @@ -110,7 +100,7 @@ public ApiResponse( /// /// The object in case of unsuccessful response. /// - public ApiException? Error { get; private set; } + public ApiException? Error { get; private set; } = error; /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. @@ -160,18 +150,40 @@ void Dispose(bool disposing) } /// - public interface IApiResponse : IApiResponse + public interface IApiResponse : IApiResponseBase { /// /// Deserialized request content as . /// - new T? Content { get; } + T? Content { get; } + + /// + /// Indicates whether the request was successful. + /// +#if NET6_0_OR_GREATER + [MemberNotNullWhen(true, nameof(ContentHeaders))] + [MemberNotNullWhen(false, nameof(Error))] + [MemberNotNullWhen(true, nameof(Content))] +#endif + bool IsSuccessStatusCode { get; } + + /// + /// HTTP response content headers as defined in RFC 2616. + /// + HttpContentHeaders? ContentHeaders { get; } + + /// + /// The object in case of unsuccessful response. + /// + [SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "By Design")] + ApiException? Error { get; } } /// - /// Base interface used to represent an API response. + /// IApiResponse. /// - public interface IApiResponse : IDisposable + /// + public interface IApiResponse : IApiResponseBase { /// /// Indicates whether the request was successful. @@ -179,24 +191,30 @@ public interface IApiResponse : IDisposable #if NET6_0_OR_GREATER [MemberNotNullWhen(true, nameof(ContentHeaders))] [MemberNotNullWhen(false, nameof(Error))] - [MemberNotNullWhen(true, nameof(Content))] #endif bool IsSuccessStatusCode { get; } /// - /// Deserialized request content as an object. + /// HTTP response content headers as defined in RFC 2616. /// - object? Content { get; } + HttpContentHeaders? ContentHeaders { get; } /// - /// HTTP response headers. + /// The object in case of unsuccessful response. /// - HttpResponseHeaders Headers { get; } + [SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "By Design")] + ApiException? Error { get; } + } + /// + /// Base interface used to represent an API response. + /// + public interface IApiResponseBase : IDisposable + { /// - /// HTTP response content headers as defined in RFC 2616. + /// HTTP response headers. /// - HttpContentHeaders? ContentHeaders { get; } + HttpResponseHeaders Headers { get; } /// /// HTTP response status code. @@ -217,10 +235,5 @@ public interface IApiResponse : IDisposable /// HTTP Message version. /// Version Version { get; } - - /// - /// The object in case of unsuccessful response. - /// - ApiException? Error { get; } } }