| | | 1 | | using System.Net; |
| | | 2 | | using System.Net.Http.Headers; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.Http; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a typed HTTP response output, including status, headers, and a deserialized body. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="TBody">The type of the deserialized response body.</typeparam> |
| | 7 | 11 | | public class HttpOutput<TBody>(HttpResponseMessage responseMessage) |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets or sets the HTTP status code returned by the server. |
| | | 15 | | /// </summary> |
| | 13 | 16 | | public HttpStatusCode StatusCode { get; set; } = responseMessage.StatusCode; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets or sets the HTTP response headers. |
| | | 20 | | /// </summary> |
| | 7 | 21 | | public HttpResponseHeaders Headers { get; set; } = responseMessage.Headers; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets or sets the deserialized response body. |
| | | 25 | | /// </summary> |
| | 21 | 26 | | public TBody? Body { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Reads the response content as a string and deserializes it into <typeparamref name="TBody"/>. |
| | | 30 | | /// </summary> |
| | | 31 | | public async Task ReadContent() |
| | 7 | 32 | | { |
| | 7 | 33 | | var body = await responseMessage.Content.ReadAsStringAsync(); |
| | | 34 | | |
| | | 35 | | try |
| | 7 | 36 | | { |
| | 7 | 37 | | Body = JsonConvert.DeserializeObject<TBody>(body); |
| | 6 | 38 | | } |
| | 1 | 39 | | catch (JsonReaderException) |
| | 1 | 40 | | { |
| | 1 | 41 | | Body = default; |
| | 1 | 42 | | } |
| | 7 | 43 | | } |
| | | 44 | | } |