Добавьте файлы проекта.
This commit is contained in:
77
PlaylistShared.PWA2123/Services/ApiClient.cs
Normal file
77
PlaylistShared.PWA2123/Services/ApiClient.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using PlaylistShared.Shared.DTO;
|
||||
using PlaylistShared.Shared.Models;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace PlaylistShared.PWA.Services;
|
||||
|
||||
public class ApiClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly TokenStorage _tokenStorage;
|
||||
|
||||
public ApiClient(HttpClient http, TokenStorage tokenStorage)
|
||||
{
|
||||
_http = http;
|
||||
_tokenStorage = tokenStorage;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse?> RefreshTokenAsync(string? refreshToken)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/account/refresh-token", new RefreshTokenRequest { RefreshToken = refreshToken });
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<LoginResponse>>();
|
||||
return apiResponse?.Data;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse?> LoginAsync(string username, string password)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/account/login", new LoginRequest { Username = username, Password = password });
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<LoginResponse>>();
|
||||
return apiResponse?.Data;
|
||||
}
|
||||
|
||||
public async Task<LoginResponse?> RegisterAsync(string username, string email, string password)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/account/register", new RegisterRequest { Username = username, Email = email, Password = password });
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<LoginResponse>>();
|
||||
return apiResponse?.Data;
|
||||
}
|
||||
|
||||
public async Task<SharedPlaylistDto?> CreateSharedPlaylistAsync(SharePlaylistDto dto)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/sharedplaylist", dto);
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<SharedPlaylistDto>>();
|
||||
return apiResponse?.Data;
|
||||
}
|
||||
|
||||
public async Task<SharedPlaylistDto?> GetSharedPlaylistAsync(string token)
|
||||
{
|
||||
var response = await _http.GetAsync($"/api/sharedplaylist/{token}");
|
||||
if (!response.IsSuccessStatusCode) return null;
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<ApiResponse<SharedPlaylistDto>>();
|
||||
return apiResponse?.Data;
|
||||
}
|
||||
|
||||
public async Task<bool> AddTracksAsync(string sharedPlaylistToken, List<string> trackIds)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/playlist/add-tracks", new AddTrackRequest
|
||||
{
|
||||
SharedPlaylistToken = sharedPlaylistToken,
|
||||
TrackIds = trackIds
|
||||
});
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveTracksAsync(string sharedPlaylistToken, List<string> trackIds)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("/api/playlist/remove-tracks", new AddTrackRequest
|
||||
{
|
||||
SharedPlaylistToken = sharedPlaylistToken,
|
||||
TrackIds = trackIds
|
||||
});
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user