Skip to content

Commit 49cc592

Browse files
authored
Update ApiResponse to correct previous adjustment (#1711)
1 parent 1a20c27 commit 49cc592

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

Refit/ApiResponse.cs

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,33 @@ internal static T Create<T, TBody>(
2929
/// Implementation of <see cref="IApiResponse{T}"/> that provides additional functionalities.
3030
/// </summary>
3131
/// <typeparam name="T"></typeparam>
32-
public sealed class ApiResponse<T> : IApiResponse<T>, IApiResponse
32+
/// <remarks>
33+
/// Create an instance of <see cref="ApiResponse{T}"/> with type <typeparamref name="T"/>.
34+
/// </remarks>
35+
/// <param name="response">Original HTTP Response message.</param>
36+
/// <param name="content">Response content.</param>
37+
/// <param name="settings">Refit settings used to send the request.</param>
38+
/// <param name="error">The ApiException, if the request failed.</param>
39+
/// <exception cref="ArgumentNullException"></exception>
40+
public sealed class ApiResponse<T>(
41+
HttpResponseMessage response,
42+
T? content,
43+
RefitSettings settings,
44+
ApiException? error = null
45+
) : IApiResponse<T>, IApiResponse
3346
{
34-
readonly HttpResponseMessage response;
47+
readonly HttpResponseMessage response = response ?? throw new ArgumentNullException(nameof(response));
3548
bool disposed;
3649

37-
/// <summary>
38-
/// Create an instance of <see cref="ApiResponse{T}"/> with type <typeparamref name="T"/>.
39-
/// </summary>
40-
/// <param name="response">Original HTTP Response message.</param>
41-
/// <param name="content">Response content.</param>
42-
/// <param name="settings">Refit settings used to send the request.</param>
43-
/// <param name="error">The ApiException, if the request failed.</param>
44-
/// <exception cref="ArgumentNullException"></exception>
45-
public ApiResponse(
46-
HttpResponseMessage response,
47-
T? content,
48-
RefitSettings settings,
49-
ApiException? error = null
50-
)
51-
{
52-
this.response = response ?? throw new ArgumentNullException(nameof(response));
53-
Error = error;
54-
Content = content;
55-
Settings = settings;
56-
}
57-
5850
/// <summary>
5951
/// Deserialized request content as <typeparamref name="T"/>.
6052
/// </summary>
61-
public T? Content { get; }
62-
63-
object? IApiResponse.Content => Content;
53+
public T? Content { get; } = content;
6454

6555
/// <summary>
6656
/// Refit settings used to send the request.
6757
/// </summary>
68-
public RefitSettings Settings { get; }
58+
public RefitSettings Settings { get; } = settings;
6959

7060
/// <summary>
7161
/// HTTP response headers.
@@ -110,7 +100,7 @@ public ApiResponse(
110100
/// <summary>
111101
/// The <see cref="ApiException" /> object in case of unsuccessful response.
112102
/// </summary>
113-
public ApiException? Error { get; private set; }
103+
public ApiException? Error { get; private set; } = error;
114104

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

162152
/// <inheritdoc/>
163-
public interface IApiResponse<out T> : IApiResponse
153+
public interface IApiResponse<out T> : IApiResponseBase
164154
{
165155
/// <summary>
166156
/// Deserialized request content as <typeparamref name="T"/>.
167157
/// </summary>
168-
new T? Content { get; }
158+
T? Content { get; }
159+
160+
/// <summary>
161+
/// Indicates whether the request was successful.
162+
/// </summary>
163+
#if NET6_0_OR_GREATER
164+
[MemberNotNullWhen(true, nameof(ContentHeaders))]
165+
[MemberNotNullWhen(false, nameof(Error))]
166+
[MemberNotNullWhen(true, nameof(Content))]
167+
#endif
168+
bool IsSuccessStatusCode { get; }
169+
170+
/// <summary>
171+
/// HTTP response content headers as defined in RFC 2616.
172+
/// </summary>
173+
HttpContentHeaders? ContentHeaders { get; }
174+
175+
/// <summary>
176+
/// The <see cref="ApiException"/> object in case of unsuccessful response.
177+
/// </summary>
178+
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "By Design")]
179+
ApiException? Error { get; }
169180
}
170181

171182
/// <summary>
172-
/// Base interface used to represent an API response.
183+
/// IApiResponse.
173184
/// </summary>
174-
public interface IApiResponse : IDisposable
185+
/// <seealso cref="Refit.IApiResponseBase" />
186+
public interface IApiResponse : IApiResponseBase
175187
{
176188
/// <summary>
177189
/// Indicates whether the request was successful.
178190
/// </summary>
179191
#if NET6_0_OR_GREATER
180192
[MemberNotNullWhen(true, nameof(ContentHeaders))]
181193
[MemberNotNullWhen(false, nameof(Error))]
182-
[MemberNotNullWhen(true, nameof(Content))]
183194
#endif
184195
bool IsSuccessStatusCode { get; }
185196

186197
/// <summary>
187-
/// Deserialized request content as an object.
198+
/// HTTP response content headers as defined in RFC 2616.
188199
/// </summary>
189-
object? Content { get; }
200+
HttpContentHeaders? ContentHeaders { get; }
190201

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

209+
/// <summary>
210+
/// Base interface used to represent an API response.
211+
/// </summary>
212+
public interface IApiResponseBase : IDisposable
213+
{
196214
/// <summary>
197-
/// HTTP response content headers as defined in RFC 2616.
215+
/// HTTP response headers.
198216
/// </summary>
199-
HttpContentHeaders? ContentHeaders { get; }
217+
HttpResponseHeaders Headers { get; }
200218

201219
/// <summary>
202220
/// HTTP response status code.
@@ -217,10 +235,5 @@ public interface IApiResponse : IDisposable
217235
/// HTTP Message version.
218236
/// </summary>
219237
Version Version { get; }
220-
221-
/// <summary>
222-
/// The <see cref="ApiException"/> object in case of unsuccessful response.
223-
/// </summary>
224-
ApiException? Error { get; }
225238
}
226239
}

0 commit comments

Comments
 (0)