From b26bf79807e2bb21744493c3e6fc6192e7d9d1a7 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 13 Jul 2024 00:42:51 +0200 Subject: [PATCH] [frontend] Add non-generic nullable ApiClient abstractions --- .../Core/ControllerModels/FilterControllerModel.cs | 6 +++--- .../FollowRequestControllerModel.cs | 7 +++++-- .../Core/ControllerModels/NoteControllerModel.cs | 4 ++-- .../NotificationControllerModel.cs | 8 ++++---- .../ControllerModels/SettingsControllerModel.cs | 4 +++- .../Core/ControllerModels/UserControllerModel.cs | 5 ++--- Iceshrimp.Frontend/Core/Services/ApiClient.cs | 14 ++++++++++++++ 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Iceshrimp.Frontend/Core/ControllerModels/FilterControllerModel.cs b/Iceshrimp.Frontend/Core/ControllerModels/FilterControllerModel.cs index 5f9716bc..560421f3 100644 --- a/Iceshrimp.Frontend/Core/ControllerModels/FilterControllerModel.cs +++ b/Iceshrimp.Frontend/Core/ControllerModels/FilterControllerModel.cs @@ -11,8 +11,8 @@ internal class FilterControllerModel(ApiClient api) public Task CreateFilter(FilterRequest request) => api.Call(HttpMethod.Post, "/filters", data: request); - public Task UpdateFilter(long id, FilterRequest request) => - api.Call(HttpMethod.Put, $"/filters/{id}", data: request); + public Task UpdateFilter(long id, FilterRequest request) => + api.CallNullable(HttpMethod.Put, $"/filters/{id}", data: request); - public Task DeleteFilter(long id) => api.Call(HttpMethod.Delete, $"/filters/{id}"); + public Task DeleteFilter(long id) => api.CallNullable(HttpMethod.Delete, $"/filters/{id}"); } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Core/ControllerModels/FollowRequestControllerModel.cs b/Iceshrimp.Frontend/Core/ControllerModels/FollowRequestControllerModel.cs index c9728dbc..419ada4c 100644 --- a/Iceshrimp.Frontend/Core/ControllerModels/FollowRequestControllerModel.cs +++ b/Iceshrimp.Frontend/Core/ControllerModels/FollowRequestControllerModel.cs @@ -9,6 +9,9 @@ internal class FollowRequestControllerModel(ApiClient api) public Task> GetFollowRequests(PaginationQuery pq) => api.Call>(HttpMethod.Get, "/follow_requests", pq); - public Task AcceptFollowRequest(string id) => api.Call(HttpMethod.Post, $"/follow_requests/{id}/accept"); - public Task RejectFollowRequest(string id) => api.Call(HttpMethod.Post, $"/follow_requests/{id}/reject"); + public Task AcceptFollowRequest(string id) => + api.CallNullable(HttpMethod.Post, $"/follow_requests/{id}/accept"); + + public Task RejectFollowRequest(string id) => + api.CallNullable(HttpMethod.Post, $"/follow_requests/{id}/reject"); } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Core/ControllerModels/NoteControllerModel.cs b/Iceshrimp.Frontend/Core/ControllerModels/NoteControllerModel.cs index 3a3dad97..e7ad290c 100644 --- a/Iceshrimp.Frontend/Core/ControllerModels/NoteControllerModel.cs +++ b/Iceshrimp.Frontend/Core/ControllerModels/NoteControllerModel.cs @@ -11,8 +11,8 @@ internal class NoteControllerModel(ApiClient api) public Task GetNote(string id) => api.CallNullable(HttpMethod.Get, $"/notes/{id}"); - public Task DeleteNote(string id) => - api.Call(HttpMethod.Delete, $"/notes/{id}"); + public Task DeleteNote(string id) => + api.CallNullable(HttpMethod.Delete, $"/notes/{id}"); public Task?> GetNoteAscendants(string id, [DefaultValue(20)] [Range(1, 100)] int? limit) { diff --git a/Iceshrimp.Frontend/Core/ControllerModels/NotificationControllerModel.cs b/Iceshrimp.Frontend/Core/ControllerModels/NotificationControllerModel.cs index d1a1c024..bbc2e3b9 100644 --- a/Iceshrimp.Frontend/Core/ControllerModels/NotificationControllerModel.cs +++ b/Iceshrimp.Frontend/Core/ControllerModels/NotificationControllerModel.cs @@ -10,14 +10,14 @@ internal class NotificationControllerModel(ApiClient api) public Task> GetNotifications(PaginationQuery pq) => api.Call>(HttpMethod.Get, "/notifications", pq); - public Task MarkNotificationAsRead(string id) => - api.Call(HttpMethod.Post, $"/notifications/{id}/read"); + public Task MarkNotificationAsRead(string id) => + api.CallNullable(HttpMethod.Post, $"/notifications/{id}/read"); public Task MarkAllNotificationsAsRead() => api.Call(HttpMethod.Post, "/notifications/read"); - public Task DeleteNotification(string id) => - api.Call(HttpMethod.Delete, $"/notifications/{id}"); + public Task DeleteNotification(string id) => + api.CallNullable(HttpMethod.Delete, $"/notifications/{id}"); public Task DeleteAllNotifications() => api.Call(HttpMethod.Delete, "/notifications"); diff --git a/Iceshrimp.Frontend/Core/ControllerModels/SettingsControllerModel.cs b/Iceshrimp.Frontend/Core/ControllerModels/SettingsControllerModel.cs index c07be613..425694b9 100644 --- a/Iceshrimp.Frontend/Core/ControllerModels/SettingsControllerModel.cs +++ b/Iceshrimp.Frontend/Core/ControllerModels/SettingsControllerModel.cs @@ -6,5 +6,7 @@ namespace Iceshrimp.Frontend.Core.ControllerModels; internal class SettingsControllerModel(ApiClient api) { public Task GetSettings() => api.Call(HttpMethod.Get, "/settings"); - public Task UpdateSettings(UserSettingsEntity settings) => api.Call(HttpMethod.Put, "/settings", data: settings); + + public Task UpdateSettings(UserSettingsEntity settings) => + api.CallNullable(HttpMethod.Put, "/settings", data: settings); } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Core/ControllerModels/UserControllerModel.cs b/Iceshrimp.Frontend/Core/ControllerModels/UserControllerModel.cs index 95469c8f..0cecca20 100644 --- a/Iceshrimp.Frontend/Core/ControllerModels/UserControllerModel.cs +++ b/Iceshrimp.Frontend/Core/ControllerModels/UserControllerModel.cs @@ -25,7 +25,6 @@ internal class UserControllerModel(ApiClient api) return api.CallNullable(HttpMethod.Get, "/users/lookup", query); } - public Task FollowUser(string id) => api.Call(HttpMethod.Post, $"/users/{id}/follow"); - - public Task UnfollowUser(string id) => api.Call(HttpMethod.Post, $"/users/{id}/unfollow"); + public Task FollowUser(string id) => api.CallNullable(HttpMethod.Post, $"/users/{id}/follow"); + public Task UnfollowUser(string id) => api.CallNullable(HttpMethod.Post, $"/users/{id}/unfollow"); } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Core/Services/ApiClient.cs b/Iceshrimp.Frontend/Core/Services/ApiClient.cs index 5df942ee..777a321c 100644 --- a/Iceshrimp.Frontend/Core/Services/ApiClient.cs +++ b/Iceshrimp.Frontend/Core/Services/ApiClient.cs @@ -28,6 +28,20 @@ internal class ApiClient(HttpClient client) throw new Exception("Deserialized API error was null"); throw new ApiException(error); } + + public async Task CallNullable( + HttpMethod method, string path, QueryString? query = null, object? data = null + ) + { + var res = await MakeRequest(method, path, query, data); + if (res.IsSuccessStatusCode) + return true; + + var error = await res.Content.ReadFromJsonAsync(JsonSerialization.Options); + if (error == null) + throw new Exception("Deserialized API error was null"); + throw new ApiException(error); + } public async Task Call( HttpMethod method, string path, QueryString? query = null, object? data = null