11import 'dart:convert' ;
22import 'package:flutter/foundation.dart' ;
33import 'package:http/http.dart' as http;
4+ import 'package:firebase_auth/firebase_auth.dart' ;
45import 'package:app_streaming/models/movie.dart' ;
56
67class ApiService {
78 final String apiKey = '36fd01cdc2a54f3273838051a253d2f5' ;
89 final String baseUrl = 'https://api.themoviedb.org/3' ;
910 final String language = 'pt-BR' ; // Define o idioma para português do Brasil
11+ final String flaskApiUrl = "http://172.17.0.2:5000/" ;
12+
13+ Future <Map <String , String >> _getHeaders () async {
14+ String ? token = await FirebaseAuth .instance.currentUser? .getIdToken ();
15+ return {
16+ 'Authorization' : 'Bearer $token ' ,
17+ 'Content-Type' : 'application/json' ,
18+ };
19+ }
20+
21+ Future <List <Movie >> fetchMoviesFromFlaskAPI (int profileId) async {
22+ final headers = await _getHeaders ();
23+ final response = await http.get (
24+ Uri .parse ('$flaskApiUrl /profiles/$profileId /movies' ),
25+ headers: headers);
26+
27+ if (response.statusCode == 200 ) {
28+ final List moviesJson = jsonDecode (response.body);
29+ return moviesJson.map ((json) => Movie .fromJson (json)).toList ();
30+ } else {
31+ throw Exception ('Failed to load movies from Flask API' );
32+ }
33+ }
34+
35+ // Adicionar um filme à lista de filmes do perfil
36+ Future <void > addMovieToList (int profileId, int movieId, bool liked) async {
37+ final headers = await _getHeaders ();
38+ final response = await http.post (
39+ Uri .parse ('$flaskApiUrl /profiles/$profileId /movies' ),
40+ headers: headers,
41+ body: json.encode ({
42+ 'title' : movieId.toString (),
43+ 'liked' : liked,
44+ }),
45+ );
46+
47+ if (response.statusCode != 201 ) {
48+ throw Exception ('Failed to add movie to list' );
49+ }
50+ }
51+
52+ // Método para verificar ou criar o perfil no backend Flask
53+ Future <void > verifyOrCreateUserProfile (String email) async {
54+ final headers = await _getHeaders ();
55+ final response = await http.post (
56+ Uri .parse ('$flaskApiUrl /verify_or_create_profile' ),
57+ headers: headers,
58+ body: json.encode ({'email' : email}),
59+ );
60+
61+ if (response.statusCode != 200 ) {
62+ throw Exception ('Failed to verify or create profile' );
63+ }
64+ }
1065
1166 Future <List <Movie >> fetchMovies (String genreId) async {
1267 final response = await http.get (Uri .parse (
1368 '$baseUrl /discover/movie?api_key=$apiKey &language=$language &with_genres=$genreId ' ));
1469
15- // Add debugging information
1670 if (kDebugMode) {
1771 print ('Fetching movies for genre ID: $genreId ' );
1872 print (
@@ -26,24 +80,18 @@ class ApiService {
2680
2781 if (jsonResponse['results' ] != null && jsonResponse['results' ] is List ) {
2882 final List results = jsonResponse['results' ];
29-
30- if (results.isNotEmpty) {
31- return results.map ((movie) => Movie .fromJson (movie)).toList ();
32- } else {
33- return []; // No results
34- }
83+ return results.map ((movie) => Movie .fromJson (movie)).toList ();
3584 } else {
3685 if (kDebugMode) {
3786 print ('Results is null or not a list' );
3887 }
39- return []; // Results is null or not a list
88+ return [];
4089 }
4190 } else {
4291 throw Exception ('Failed to load movies' );
4392 }
4493 }
4594
46- // Função para buscar os detalhes de um filme
4795 Future <Movie > fetchMovieDetails (int movieId) async {
4896 final response = await http.get (Uri .parse (
4997 '$baseUrl /movie/$movieId ?api_key=$apiKey &language=$language &append_to_response=videos' ));
@@ -56,7 +104,6 @@ class ApiService {
56104 }
57105 }
58106
59- // Função para buscar o trailer de um filme
60107 Future <String > fetchTrailer (int movieId) async {
61108 final response = await http.get (Uri .parse (
62109 '$baseUrl /movie/$movieId /videos?api_key=$apiKey &language=$language ' ));
@@ -73,22 +120,19 @@ class ApiService {
73120 }
74121 }
75122
76- // Método para buscar filmes relacionados
77123 Future <List <Movie >> fetchRelatedMovies (int movieId) async {
78124 final response = await http.get (Uri .parse (
79125 '$baseUrl /movie/$movieId /similar?api_key=$apiKey &language=$language ' ));
80126
81127 if (response.statusCode == 200 ) {
82128 final jsonResponse = jsonDecode (response.body);
83129 final List results = jsonResponse['results' ];
84-
85130 return results.map ((movie) => Movie .fromJson (movie)).toList ();
86131 } else {
87132 throw Exception ('Failed to load related movies' );
88133 }
89134 }
90135
91- // Função para buscar todos os gêneros de filmes
92136 Future <Map <int , String >> fetchGenres () async {
93137 final response = await http.get (Uri .parse (
94138 '$baseUrl /genre/movie/list?api_key=$apiKey &language=$language ' ));
@@ -97,7 +141,7 @@ class ApiService {
97141 final jsonResponse = jsonDecode (response.body);
98142 final List genres = jsonResponse['genres' ];
99143
100- // Converta a lista de gêneros em um mapa de ID para nome
144+ // Convert the list of genres into a map of ID to name
101145 return {for (var genre in genres) genre['id' ]: genre['name' ]};
102146 } else {
103147 throw Exception ('Failed to load genres' );
@@ -109,11 +153,9 @@ class ApiService {
109153 String url;
110154
111155 if (isGenre) {
112- // If it's a genre, use the discover endpoint with genres
113156 url =
114157 '$baseUrl /discover/movie?api_key=$apiKey &language=$language &with_genres=$categoryOrGenreId ' ;
115158 } else {
116- // If it's a predefined category (e.g., popular), use the specific endpoint
117159 url =
118160 '$baseUrl /movie/$categoryOrGenreId ?api_key=$apiKey &language=$language ' ;
119161 }
@@ -132,12 +174,7 @@ class ApiService {
132174
133175 if (jsonResponse['results' ] != null && jsonResponse['results' ] is List ) {
134176 final List results = jsonResponse['results' ];
135-
136- if (results.isNotEmpty) {
137- return results.map ((movie) => Movie .fromJson (movie)).toList ();
138- } else {
139- return [];
140- }
177+ return results.map ((movie) => Movie .fromJson (movie)).toList ();
141178 } else {
142179 return [];
143180 }
@@ -146,14 +183,15 @@ class ApiService {
146183 }
147184 }
148185
149- Future <List <Movie >> searchMoviesByTitle (
150- {required String title,
151- bool includeAdult = false ,
152- String language = 'pt-BR' ,
153- int page = 1 ,
154- String ? region,
155- String ? year,
156- String ? primaryReleaseYear}) async {
186+ Future <List <Movie >> searchMoviesByTitle ({
187+ required String title,
188+ bool includeAdult = false ,
189+ String language = 'pt-BR' ,
190+ int page = 1 ,
191+ String ? region,
192+ String ? year,
193+ String ? primaryReleaseYear,
194+ }) async {
157195 final Uri uri = Uri .parse (
158196 '$baseUrl /search/movie' ,
159197 ).replace (queryParameters: {
0 commit comments