You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.9 KiB
Dart
106 lines
2.9 KiB
Dart
// lib/core/api/tmdb_api.dart
|
|
import 'package:dio/dio.dart';
|
|
import '../config.dart';
|
|
|
|
class TmdbApi {
|
|
final Dio _dio;
|
|
|
|
TmdbApi()
|
|
: _dio = Dio(
|
|
BaseOptions(
|
|
baseUrl: 'https://api.themoviedb.org/3',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
},
|
|
validateStatus: (code) => code != null && code < 500,
|
|
),
|
|
);
|
|
|
|
Map<String, dynamic> get _auth => {
|
|
'api_key': AppConfig.tmdbApiKey,
|
|
'language': 'de-DE',
|
|
};
|
|
|
|
Future<Map<String, dynamic>> getMovie(int id) async {
|
|
final res = await _dio.get(
|
|
'/movie/$id',
|
|
queryParameters: {
|
|
..._auth,
|
|
'append_to_response': 'images,credits',
|
|
},
|
|
);
|
|
if (res.statusCode != 200) {
|
|
throw Exception(
|
|
'TMDB getMovie($id) failed: ${res.statusCode} ${res.data}');
|
|
}
|
|
return Map<String, dynamic>.from(res.data);
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> searchMovies(String query, {int page = 1}) async {
|
|
final res = await _dio.get(
|
|
'/search/movie',
|
|
queryParameters: {
|
|
..._auth,
|
|
'query': query,
|
|
'page': page,
|
|
'include_adult': false,
|
|
},
|
|
);
|
|
if (res.statusCode != 200) {
|
|
throw Exception('TMDB searchMovies failed: ${res.statusCode} ${res.data}');
|
|
}
|
|
final data = res.data as Map<String, dynamic>;
|
|
final results = (data['results'] as List? ?? const [])
|
|
.map((e) => Map<String, dynamic>.from(e as Map))
|
|
.toList();
|
|
return results;
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> searchShows(String query, {int page = 1}) async {
|
|
final res = await _dio.get(
|
|
'/search/tv',
|
|
queryParameters: {
|
|
..._auth,
|
|
'query': query,
|
|
'page': page,
|
|
'include_adult': false,
|
|
},
|
|
);
|
|
if (res.statusCode != 200) {
|
|
throw Exception('TMDB searchShows failed: ${res.statusCode} ${res.data}');
|
|
}
|
|
final data = res.data as Map<String, dynamic>;
|
|
final results = (data['results'] as List? ?? const [])
|
|
.map((e) => Map<String, dynamic>.from(e as Map))
|
|
.toList();
|
|
return results;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getShow(int id) async {
|
|
final res = await _dio.get(
|
|
'/tv/$id',
|
|
queryParameters: {
|
|
..._auth,
|
|
'append_to_response': 'images,credits',
|
|
},
|
|
);
|
|
if (res.statusCode != 200) {
|
|
throw Exception(
|
|
'TMDB getShow($id) failed: ${res.statusCode} ${res.data}');
|
|
}
|
|
return Map<String, dynamic>.from(res.data);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getSeason(int showId, int seasonNumber) async {
|
|
final res = await _dio.get(
|
|
'/tv/$showId/season/$seasonNumber',
|
|
queryParameters: _auth,
|
|
);
|
|
if (res.statusCode != 200) {
|
|
throw Exception(
|
|
'TMDB getSeason($showId,S$seasonNumber) failed: ${res.statusCode} ${res.data}');
|
|
}
|
|
return Map<String, dynamic>.from(res.data);
|
|
}
|
|
}
|