[frontend] Add non-generic nullable ApiClient abstractions
This commit is contained in:
parent
f42aeee2fd
commit
b26bf79807
7 changed files with 33 additions and 15 deletions
|
@ -11,8 +11,8 @@ internal class FilterControllerModel(ApiClient api)
|
||||||
public Task<FilterResponse> CreateFilter(FilterRequest request) =>
|
public Task<FilterResponse> CreateFilter(FilterRequest request) =>
|
||||||
api.Call<FilterResponse>(HttpMethod.Post, "/filters", data: request);
|
api.Call<FilterResponse>(HttpMethod.Post, "/filters", data: request);
|
||||||
|
|
||||||
public Task UpdateFilter(long id, FilterRequest request) =>
|
public Task<bool> UpdateFilter(long id, FilterRequest request) =>
|
||||||
api.Call(HttpMethod.Put, $"/filters/{id}", data: request);
|
api.CallNullable(HttpMethod.Put, $"/filters/{id}", data: request);
|
||||||
|
|
||||||
public Task DeleteFilter(long id) => api.Call(HttpMethod.Delete, $"/filters/{id}");
|
public Task<bool> DeleteFilter(long id) => api.CallNullable(HttpMethod.Delete, $"/filters/{id}");
|
||||||
}
|
}
|
|
@ -9,6 +9,9 @@ internal class FollowRequestControllerModel(ApiClient api)
|
||||||
public Task<List<FollowRequestResponse>> GetFollowRequests(PaginationQuery pq) =>
|
public Task<List<FollowRequestResponse>> GetFollowRequests(PaginationQuery pq) =>
|
||||||
api.Call<List<FollowRequestResponse>>(HttpMethod.Get, "/follow_requests", pq);
|
api.Call<List<FollowRequestResponse>>(HttpMethod.Get, "/follow_requests", pq);
|
||||||
|
|
||||||
public Task AcceptFollowRequest(string id) => api.Call(HttpMethod.Post, $"/follow_requests/{id}/accept");
|
public Task<bool> AcceptFollowRequest(string id) =>
|
||||||
public Task RejectFollowRequest(string id) => api.Call(HttpMethod.Post, $"/follow_requests/{id}/reject");
|
api.CallNullable(HttpMethod.Post, $"/follow_requests/{id}/accept");
|
||||||
|
|
||||||
|
public Task<bool> RejectFollowRequest(string id) =>
|
||||||
|
api.CallNullable(HttpMethod.Post, $"/follow_requests/{id}/reject");
|
||||||
}
|
}
|
|
@ -11,8 +11,8 @@ internal class NoteControllerModel(ApiClient api)
|
||||||
public Task<NoteResponse?> GetNote(string id) =>
|
public Task<NoteResponse?> GetNote(string id) =>
|
||||||
api.CallNullable<NoteResponse>(HttpMethod.Get, $"/notes/{id}");
|
api.CallNullable<NoteResponse>(HttpMethod.Get, $"/notes/{id}");
|
||||||
|
|
||||||
public Task DeleteNote(string id) =>
|
public Task<bool> DeleteNote(string id) =>
|
||||||
api.Call(HttpMethod.Delete, $"/notes/{id}");
|
api.CallNullable(HttpMethod.Delete, $"/notes/{id}");
|
||||||
|
|
||||||
public Task<List<NoteResponse>?> GetNoteAscendants(string id, [DefaultValue(20)] [Range(1, 100)] int? limit)
|
public Task<List<NoteResponse>?> GetNoteAscendants(string id, [DefaultValue(20)] [Range(1, 100)] int? limit)
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,14 +10,14 @@ internal class NotificationControllerModel(ApiClient api)
|
||||||
public Task<List<NotificationResponse>> GetNotifications(PaginationQuery pq) =>
|
public Task<List<NotificationResponse>> GetNotifications(PaginationQuery pq) =>
|
||||||
api.Call<List<NotificationResponse>>(HttpMethod.Get, "/notifications", pq);
|
api.Call<List<NotificationResponse>>(HttpMethod.Get, "/notifications", pq);
|
||||||
|
|
||||||
public Task MarkNotificationAsRead(string id) =>
|
public Task<bool> MarkNotificationAsRead(string id) =>
|
||||||
api.Call(HttpMethod.Post, $"/notifications/{id}/read");
|
api.CallNullable(HttpMethod.Post, $"/notifications/{id}/read");
|
||||||
|
|
||||||
public Task MarkAllNotificationsAsRead() =>
|
public Task MarkAllNotificationsAsRead() =>
|
||||||
api.Call(HttpMethod.Post, "/notifications/read");
|
api.Call(HttpMethod.Post, "/notifications/read");
|
||||||
|
|
||||||
public Task DeleteNotification(string id) =>
|
public Task<bool> DeleteNotification(string id) =>
|
||||||
api.Call(HttpMethod.Delete, $"/notifications/{id}");
|
api.CallNullable(HttpMethod.Delete, $"/notifications/{id}");
|
||||||
|
|
||||||
public Task DeleteAllNotifications() =>
|
public Task DeleteAllNotifications() =>
|
||||||
api.Call(HttpMethod.Delete, "/notifications");
|
api.Call(HttpMethod.Delete, "/notifications");
|
||||||
|
|
|
@ -6,5 +6,7 @@ namespace Iceshrimp.Frontend.Core.ControllerModels;
|
||||||
internal class SettingsControllerModel(ApiClient api)
|
internal class SettingsControllerModel(ApiClient api)
|
||||||
{
|
{
|
||||||
public Task<UserSettingsEntity> GetSettings() => api.Call<UserSettingsEntity>(HttpMethod.Get, "/settings");
|
public Task<UserSettingsEntity> GetSettings() => api.Call<UserSettingsEntity>(HttpMethod.Get, "/settings");
|
||||||
public Task UpdateSettings(UserSettingsEntity settings) => api.Call(HttpMethod.Put, "/settings", data: settings);
|
|
||||||
|
public Task<bool> UpdateSettings(UserSettingsEntity settings) =>
|
||||||
|
api.CallNullable(HttpMethod.Put, "/settings", data: settings);
|
||||||
}
|
}
|
|
@ -25,7 +25,6 @@ internal class UserControllerModel(ApiClient api)
|
||||||
return api.CallNullable<UserResponse>(HttpMethod.Get, "/users/lookup", query);
|
return api.CallNullable<UserResponse>(HttpMethod.Get, "/users/lookup", query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task FollowUser(string id) => api.Call(HttpMethod.Post, $"/users/{id}/follow");
|
public Task<bool> FollowUser(string id) => api.CallNullable(HttpMethod.Post, $"/users/{id}/follow");
|
||||||
|
public Task<bool> UnfollowUser(string id) => api.CallNullable(HttpMethod.Post, $"/users/{id}/unfollow");
|
||||||
public Task UnfollowUser(string id) => api.Call(HttpMethod.Post, $"/users/{id}/unfollow");
|
|
||||||
}
|
}
|
|
@ -29,6 +29,20 @@ internal class ApiClient(HttpClient client)
|
||||||
throw new ApiException(error);
|
throw new ApiException(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> 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<ErrorResponse>(JsonSerialization.Options);
|
||||||
|
if (error == null)
|
||||||
|
throw new Exception("Deserialized API error was null");
|
||||||
|
throw new ApiException(error);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<T> Call<T>(
|
public async Task<T> Call<T>(
|
||||||
HttpMethod method, string path, QueryString? query = null, object? data = null
|
HttpMethod method, string path, QueryString? query = null, object? data = null
|
||||||
) where T : class
|
) where T : class
|
||||||
|
|
Loading…
Add table
Reference in a new issue