Skip to content

Commit

Permalink
Update ApiResponse to correct previous adjustment (#1711)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Jun 9, 2024
1 parent 1a20c27 commit 49cc592
Showing 1 changed file with 57 additions and 44 deletions.
101 changes: 57 additions & 44 deletions Refit/ApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,33 @@ internal static T Create<T, TBody>(
/// Implementation of <see cref="IApiResponse{T}"/> that provides additional functionalities.
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class ApiResponse<T> : IApiResponse<T>, IApiResponse
/// <remarks>
/// Create an instance of <see cref="ApiResponse{T}"/> with type <typeparamref name="T"/>.
/// </remarks>
/// <param name="response">Original HTTP Response message.</param>
/// <param name="content">Response content.</param>
/// <param name="settings">Refit settings used to send the request.</param>
/// <param name="error">The ApiException, if the request failed.</param>
/// <exception cref="ArgumentNullException"></exception>
public sealed class ApiResponse<T>(
HttpResponseMessage response,
T? content,
RefitSettings settings,
ApiException? error = null
) : IApiResponse<T>, IApiResponse
{
readonly HttpResponseMessage response;
readonly HttpResponseMessage response = response ?? throw new ArgumentNullException(nameof(response));
bool disposed;

/// <summary>
/// Create an instance of <see cref="ApiResponse{T}"/> with type <typeparamref name="T"/>.
/// </summary>
/// <param name="response">Original HTTP Response message.</param>
/// <param name="content">Response content.</param>
/// <param name="settings">Refit settings used to send the request.</param>
/// <param name="error">The ApiException, if the request failed.</param>
/// <exception cref="ArgumentNullException"></exception>
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;
}

/// <summary>
/// Deserialized request content as <typeparamref name="T"/>.
/// </summary>
public T? Content { get; }

object? IApiResponse.Content => Content;
public T? Content { get; } = content;

/// <summary>
/// Refit settings used to send the request.
/// </summary>
public RefitSettings Settings { get; }
public RefitSettings Settings { get; } = settings;

/// <summary>
/// HTTP response headers.
Expand Down Expand Up @@ -110,7 +100,7 @@ public ApiResponse(
/// <summary>
/// The <see cref="ApiException" /> object in case of unsuccessful response.
/// </summary>
public ApiException? Error { get; private set; }
public ApiException? Error { get; private set; } = error;

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Expand Down Expand Up @@ -160,43 +150,71 @@ void Dispose(bool disposing)
}

/// <inheritdoc/>
public interface IApiResponse<out T> : IApiResponse
public interface IApiResponse<out T> : IApiResponseBase
{
/// <summary>
/// Deserialized request content as <typeparamref name="T"/>.
/// </summary>
new T? Content { get; }
T? Content { get; }

/// <summary>
/// Indicates whether the request was successful.
/// </summary>
#if NET6_0_OR_GREATER
[MemberNotNullWhen(true, nameof(ContentHeaders))]
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Content))]
#endif
bool IsSuccessStatusCode { get; }

/// <summary>
/// HTTP response content headers as defined in RFC 2616.
/// </summary>
HttpContentHeaders? ContentHeaders { get; }

/// <summary>
/// The <see cref="ApiException"/> object in case of unsuccessful response.
/// </summary>
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "By Design")]
ApiException? Error { get; }
}

/// <summary>
/// Base interface used to represent an API response.
/// IApiResponse.
/// </summary>
public interface IApiResponse : IDisposable
/// <seealso cref="Refit.IApiResponseBase" />
public interface IApiResponse : IApiResponseBase
{
/// <summary>
/// Indicates whether the request was successful.
/// </summary>
#if NET6_0_OR_GREATER
[MemberNotNullWhen(true, nameof(ContentHeaders))]
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Content))]
#endif
bool IsSuccessStatusCode { get; }

/// <summary>
/// Deserialized request content as an object.
/// HTTP response content headers as defined in RFC 2616.
/// </summary>
object? Content { get; }
HttpContentHeaders? ContentHeaders { get; }

/// <summary>
/// HTTP response headers.
/// The <see cref="ApiException"/> object in case of unsuccessful response.
/// </summary>
HttpResponseHeaders Headers { get; }
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "By Design")]
ApiException? Error { get; }
}

/// <summary>
/// Base interface used to represent an API response.
/// </summary>
public interface IApiResponseBase : IDisposable
{
/// <summary>
/// HTTP response content headers as defined in RFC 2616.
/// HTTP response headers.
/// </summary>
HttpContentHeaders? ContentHeaders { get; }
HttpResponseHeaders Headers { get; }

/// <summary>
/// HTTP response status code.
Expand All @@ -217,10 +235,5 @@ public interface IApiResponse : IDisposable
/// HTTP Message version.
/// </summary>
Version Version { get; }

/// <summary>
/// The <see cref="ApiException"/> object in case of unsuccessful response.
/// </summary>
ApiException? Error { get; }
}
}

0 comments on commit 49cc592

Please sign in to comment.