2025-07-13 14:01:29 +03:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:neomovies_mobile/data/models/movie.dart';
|
2025-10-02 17:09:36 +00:00
|
|
|
import 'package:neomovies_mobile/data/models/favorite.dart';
|
2025-07-13 14:01:29 +03:00
|
|
|
import 'package:neomovies_mobile/data/models/reaction.dart';
|
2025-10-02 17:09:36 +00:00
|
|
|
import 'package:neomovies_mobile/data/models/auth_response.dart';
|
2025-07-13 14:01:29 +03:00
|
|
|
import 'package:neomovies_mobile/data/models/user.dart';
|
2025-10-02 17:09:36 +00:00
|
|
|
import 'package:neomovies_mobile/data/api/neomovies_api_client.dart'; // новый клиент
|
2025-10-03 06:00:37 +00:00
|
|
|
import 'package:neomovies_mobile/data/exceptions/auth_exceptions.dart';
|
2025-07-13 14:01:29 +03:00
|
|
|
|
|
|
|
|
class ApiClient {
|
2025-10-02 17:09:36 +00:00
|
|
|
final NeoMoviesApiClient _neoClient;
|
2025-07-13 14:01:29 +03:00
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
ApiClient(http.Client client)
|
|
|
|
|
: _neoClient = NeoMoviesApiClient(client);
|
2025-07-13 14:01:29 +03:00
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
// ---- Movies ----
|
|
|
|
|
Future<List<Movie>> getPopularMovies({int page = 1}) {
|
|
|
|
|
return _neoClient.getPopularMovies(page: page);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<List<Movie>> getTopRatedMovies({int page = 1}) {
|
|
|
|
|
return _neoClient.getTopRatedMovies(page: page);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<List<Movie>> getUpcomingMovies({int page = 1}) {
|
|
|
|
|
return _neoClient.getUpcomingMovies(page: page);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<Movie> getMovieById(String id) {
|
|
|
|
|
return _neoClient.getMovieById(id);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<Movie> getTvById(String id) {
|
|
|
|
|
return _neoClient.getTvShowById(id);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
// ---- Search ----
|
|
|
|
|
Future<List<Movie>> searchMovies(String query, {int page = 1}) {
|
|
|
|
|
return _neoClient.search(query, page: page);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
// ---- Favorites ----
|
|
|
|
|
Future<List<Favorite>> getFavorites() {
|
|
|
|
|
return _neoClient.getFavorites();
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<void> addFavorite(
|
|
|
|
|
String mediaId,
|
|
|
|
|
String mediaType,
|
|
|
|
|
String title,
|
|
|
|
|
String posterPath,
|
|
|
|
|
) {
|
|
|
|
|
return _neoClient.addFavorite(
|
|
|
|
|
mediaId: mediaId,
|
|
|
|
|
mediaType: mediaType,
|
|
|
|
|
title: title,
|
|
|
|
|
posterPath: posterPath,
|
2025-07-13 14:01:29 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 19:54:32 +00:00
|
|
|
Future<void> removeFavorite(String mediaId, {String mediaType = 'movie'}) {
|
|
|
|
|
return _neoClient.removeFavorite(mediaId, mediaType: mediaType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool> checkIsFavorite(String mediaId, {String mediaType = 'movie'}) {
|
|
|
|
|
return _neoClient.checkIsFavorite(mediaId, mediaType: mediaType);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
// ---- Reactions ----
|
|
|
|
|
Future<Map<String, int>> getReactionCounts(
|
|
|
|
|
String mediaType, String mediaId) {
|
|
|
|
|
return _neoClient.getReactionCounts(
|
|
|
|
|
mediaType: mediaType,
|
|
|
|
|
mediaId: mediaId,
|
2025-07-13 14:01:29 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<void> setReaction(
|
|
|
|
|
String mediaType, String mediaId, String reactionType) {
|
|
|
|
|
return _neoClient.setReaction(
|
|
|
|
|
mediaType: mediaType,
|
|
|
|
|
mediaId: mediaId,
|
|
|
|
|
reactionType: reactionType,
|
2025-07-13 14:01:29 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<List<UserReaction>> getMyReactions() {
|
|
|
|
|
return _neoClient.getMyReactions();
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 18:28:52 +00:00
|
|
|
// Get single user reaction for specific media
|
|
|
|
|
Future<UserReaction?> getMyReaction(String mediaType, String mediaId) async {
|
|
|
|
|
final reactions = await _neoClient.getMyReactions();
|
|
|
|
|
try {
|
|
|
|
|
return reactions.firstWhere(
|
|
|
|
|
(r) => r.mediaType == mediaType && r.mediaId == mediaId,
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null; // No reaction found
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- External IDs (IMDb) ----
|
|
|
|
|
Future<String?> getImdbId(String mediaId, String mediaType) async {
|
2025-10-05 16:03:22 +00:00
|
|
|
return _neoClient.getExternalIds(mediaId, mediaType);
|
2025-10-02 18:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
// ---- Auth ----
|
|
|
|
|
Future<void> register(String name, String email, String password) {
|
|
|
|
|
return _neoClient.register(
|
|
|
|
|
name: name,
|
|
|
|
|
email: email,
|
|
|
|
|
password: password,
|
|
|
|
|
).then((_) {}); // старый код ничего не возвращал
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-03 06:00:37 +00:00
|
|
|
Future<AuthResponse> login(String email, String password) async {
|
|
|
|
|
try {
|
|
|
|
|
return await _neoClient.login(email: email, password: password);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
final errorMessage = e.toString();
|
|
|
|
|
if (errorMessage.contains('Account not activated') ||
|
|
|
|
|
errorMessage.contains('not verified') ||
|
|
|
|
|
errorMessage.contains('Please verify your email')) {
|
|
|
|
|
throw UnverifiedAccountException(email, message: errorMessage);
|
|
|
|
|
}
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-03 06:00:37 +00:00
|
|
|
Future<AuthResponse> verify(String email, String code) {
|
|
|
|
|
return _neoClient.verifyEmail(email: email, code: code);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<void> resendCode(String email) {
|
|
|
|
|
return _neoClient.resendVerificationCode(email);
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:09:36 +00:00
|
|
|
Future<void> deleteAccount() {
|
|
|
|
|
return _neoClient.deleteAccount();
|
2025-07-13 14:01:29 +03:00
|
|
|
}
|
2025-10-02 17:09:36 +00:00
|
|
|
}
|