| | | 1 | | using System.Net; |
| | | 2 | | using Newtonsoft.Json; |
| | | 3 | | |
| | | 4 | | namespace Syki.Back.Shared; |
| | | 5 | | |
| | | 6 | | public static class HttpExtensions |
| | | 7 | | { |
| | | 8 | | public static async Task<ErrorOut> ToError(this HttpResponseMessage httpResponse) |
| | | 9 | | { |
| | 216 | 10 | | var responseAsString = await httpResponse.Content.ReadAsStringAsync(); |
| | 216 | 11 | | return JsonConvert.DeserializeObject<ErrorOut>(responseAsString)!; |
| | 216 | 12 | | } |
| | | 13 | | |
| | | 14 | | public static async Task<T> DeserializeTo<T>(this HttpResponseMessage httpResponse) |
| | | 15 | | { |
| | 756 | 16 | | var responseAsString = await httpResponse.Content.ReadAsStringAsync(); |
| | 756 | 17 | | return JsonConvert.DeserializeObject<T>(responseAsString)!; |
| | 756 | 18 | | } |
| | | 19 | | |
| | | 20 | | public static async Task<OneOf<T, ErrorOut>> Resolve<T>(this HttpResponseMessage httpResponse) |
| | | 21 | | { |
| | 972 | 22 | | if (httpResponse.IsSuccessStatusCode) |
| | 714 | 23 | | return await httpResponse.DeserializeTo<T>(); |
| | | 24 | | |
| | 258 | 25 | | if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) |
| | 24 | 26 | | return UnauthorizedErrorOut.I; |
| | | 27 | | |
| | 234 | 28 | | if (httpResponse.StatusCode == HttpStatusCode.Forbidden) |
| | 18 | 29 | | return ForbiddenErrorOut.I; |
| | | 30 | | |
| | 216 | 31 | | return await httpResponse.ToError(); |
| | 972 | 32 | | } |
| | | 33 | | } |