| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extension methods for HttpClient that encapsulate common API patterns. |
| | | 7 | | /// These handle try/catch, logging, and response deserialization consistently. |
| | | 8 | | /// |
| | | 9 | | /// Design note: These could later be moved into an ApiServiceBase class |
| | | 10 | | /// if we decide to use inheritance for service classes. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class HttpClientExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// GET request that returns a single entity or null. |
| | | 16 | | /// Logs errors and returns null on failure (including 404). |
| | | 17 | | /// </summary> |
| | | 18 | | public static async Task<T?> GetEntityAsync<T>( |
| | | 19 | | this HttpClient http, |
| | | 20 | | string url, |
| | | 21 | | ILogger logger, |
| | | 22 | | string? entityDescription = null) where T : class |
| | | 23 | | { |
| | | 24 | | try |
| | | 25 | | { |
| | 0 | 26 | | return await http.GetFromJsonAsync<T>(url); |
| | | 27 | | } |
| | 0 | 28 | | catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | | 29 | | { |
| | 0 | 30 | | logger.LogWarning("{Entity} not found at {Url}", entityDescription ?? typeof(T).Name, url); |
| | 0 | 31 | | return null; |
| | | 32 | | } |
| | 0 | 33 | | catch (Exception ex) |
| | | 34 | | { |
| | 0 | 35 | | logger.LogError(ex, "Error fetching {Entity} from {Url}", entityDescription ?? typeof(T).Name, url); |
| | 0 | 36 | | return null; |
| | | 37 | | } |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// GET request that returns a list of entities. |
| | | 42 | | /// Logs errors and returns empty list on failure. |
| | | 43 | | /// </summary> |
| | | 44 | | public static async Task<List<T>> GetListAsync<T>( |
| | | 45 | | this HttpClient http, |
| | | 46 | | string url, |
| | | 47 | | ILogger logger, |
| | | 48 | | string? entityDescription = null) |
| | | 49 | | { |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | var result = await http.GetFromJsonAsync<List<T>>(url); |
| | 0 | 53 | | return result ?? new List<T>(); |
| | | 54 | | } |
| | 0 | 55 | | catch (Exception ex) |
| | | 56 | | { |
| | 0 | 57 | | logger.LogError(ex, "Error fetching {Entity} list from {Url}", entityDescription ?? typeof(T).Name, url); |
| | 0 | 58 | | return new List<T>(); |
| | | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// POST request that creates an entity and returns it. |
| | | 64 | | /// Returns null on failure. |
| | | 65 | | /// </summary> |
| | | 66 | | public static async Task<T?> PostEntityAsync<T>( |
| | | 67 | | this HttpClient http, |
| | | 68 | | string url, |
| | | 69 | | object dto, |
| | | 70 | | ILogger logger, |
| | | 71 | | string? entityDescription = null) where T : class |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | 0 | 75 | | var response = await http.PostAsJsonAsync(url, dto); |
| | 0 | 76 | | if (response.IsSuccessStatusCode) |
| | | 77 | | { |
| | 0 | 78 | | return await response.Content.ReadFromJsonAsync<T>(); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | logger.LogWarning("Failed to create {Entity}: {StatusCode}", |
| | 0 | 82 | | entityDescription ?? typeof(T).Name, response.StatusCode); |
| | 0 | 83 | | return null; |
| | | 84 | | } |
| | 0 | 85 | | catch (Exception ex) |
| | | 86 | | { |
| | 0 | 87 | | logger.LogError(ex, "Error creating {Entity} at {Url}", entityDescription ?? typeof(T).Name, url); |
| | 0 | 88 | | return null; |
| | | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// PUT request that updates an entity and returns it. |
| | | 94 | | /// Returns null on failure. |
| | | 95 | | /// </summary> |
| | | 96 | | public static async Task<T?> PutEntityAsync<T>( |
| | | 97 | | this HttpClient http, |
| | | 98 | | string url, |
| | | 99 | | object dto, |
| | | 100 | | ILogger logger, |
| | | 101 | | string? entityDescription = null) where T : class |
| | | 102 | | { |
| | | 103 | | try |
| | | 104 | | { |
| | 0 | 105 | | var response = await http.PutAsJsonAsync(url, dto); |
| | 0 | 106 | | if (response.IsSuccessStatusCode) |
| | | 107 | | { |
| | 0 | 108 | | return await response.Content.ReadFromJsonAsync<T>(); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | logger.LogWarning("Failed to update {Entity}: {StatusCode}", |
| | 0 | 112 | | entityDescription ?? typeof(T).Name, response.StatusCode); |
| | 0 | 113 | | return null; |
| | | 114 | | } |
| | 0 | 115 | | catch (Exception ex) |
| | | 116 | | { |
| | 0 | 117 | | logger.LogError(ex, "Error updating {Entity} at {Url}", entityDescription ?? typeof(T).Name, url); |
| | 0 | 118 | | return null; |
| | | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// DELETE request that returns success/failure. |
| | | 124 | | /// </summary> |
| | | 125 | | public static async Task<bool> DeleteEntityAsync( |
| | | 126 | | this HttpClient http, |
| | | 127 | | string url, |
| | | 128 | | ILogger logger, |
| | | 129 | | string? entityDescription = null) |
| | | 130 | | { |
| | | 131 | | try |
| | | 132 | | { |
| | 0 | 133 | | var response = await http.DeleteAsync(url); |
| | 0 | 134 | | if (!response.IsSuccessStatusCode) |
| | | 135 | | { |
| | 0 | 136 | | logger.LogWarning("Failed to delete {Entity}: {StatusCode}", |
| | 0 | 137 | | entityDescription ?? "entity", response.StatusCode); |
| | | 138 | | } |
| | 0 | 139 | | return response.IsSuccessStatusCode; |
| | | 140 | | } |
| | 0 | 141 | | catch (Exception ex) |
| | | 142 | | { |
| | 0 | 143 | | logger.LogError(ex, "Error deleting {Entity} at {Url}", entityDescription ?? "entity", url); |
| | 0 | 144 | | return false; |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// PATCH request that returns success/failure. |
| | | 150 | | /// Useful for partial updates like moving articles. |
| | | 151 | | /// </summary> |
| | | 152 | | public static async Task<bool> PatchEntityAsync( |
| | | 153 | | this HttpClient http, |
| | | 154 | | string url, |
| | | 155 | | object dto, |
| | | 156 | | ILogger logger, |
| | | 157 | | string? entityDescription = null) |
| | | 158 | | { |
| | | 159 | | try |
| | | 160 | | { |
| | 0 | 161 | | var response = await http.PatchAsJsonAsync(url, dto); |
| | 0 | 162 | | if (!response.IsSuccessStatusCode) |
| | | 163 | | { |
| | 0 | 164 | | var errorContent = await response.Content.ReadAsStringAsync(); |
| | 0 | 165 | | logger.LogWarning("Failed to patch {Entity}: {StatusCode} - {Error}", |
| | 0 | 166 | | entityDescription ?? "entity", response.StatusCode, errorContent); |
| | | 167 | | } |
| | 0 | 168 | | return response.IsSuccessStatusCode; |
| | | 169 | | } |
| | 0 | 170 | | catch (Exception ex) |
| | | 171 | | { |
| | 0 | 172 | | logger.LogError(ex, "Error patching {Entity} at {Url}", entityDescription ?? "entity", url); |
| | 0 | 173 | | return false; |
| | | 174 | | } |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// PUT request that returns success/failure (no response body expected). |
| | | 179 | | /// Useful for operations like moving entities. |
| | | 180 | | /// </summary> |
| | | 181 | | public static async Task<bool> PutBoolAsync( |
| | | 182 | | this HttpClient http, |
| | | 183 | | string url, |
| | | 184 | | object dto, |
| | | 185 | | ILogger logger, |
| | | 186 | | string? entityDescription = null) |
| | | 187 | | { |
| | | 188 | | try |
| | | 189 | | { |
| | 0 | 190 | | var response = await http.PutAsJsonAsync(url, dto); |
| | 0 | 191 | | if (!response.IsSuccessStatusCode) |
| | | 192 | | { |
| | 0 | 193 | | var errorContent = await response.Content.ReadAsStringAsync(); |
| | 0 | 194 | | logger.LogWarning("Failed to update {Entity}: {StatusCode} - {Error}", |
| | 0 | 195 | | entityDescription ?? "entity", response.StatusCode, errorContent); |
| | | 196 | | } |
| | 0 | 197 | | return response.IsSuccessStatusCode; |
| | | 198 | | } |
| | 0 | 199 | | catch (Exception ex) |
| | | 200 | | { |
| | 0 | 201 | | logger.LogError(ex, "Error updating {Entity} at {Url}", entityDescription ?? "entity", url); |
| | 0 | 202 | | return false; |
| | | 203 | | } |
| | 0 | 204 | | } |
| | | 205 | | } |