Skip to content

Commit 573854d

Browse files
committed
Add MovieApi and data models for movie responses
1 parent bd2f7ee commit 573854d

File tree

4 files changed

+541
-1
lines changed

4 files changed

+541
-1
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'movie_responses.g.dart';
4+
5+
@JsonSerializable()
6+
class MoviesResponse {
7+
final int page;
8+
final List<Movie> results;
9+
@JsonKey(name: 'total_pages')
10+
final int totalPages;
11+
@JsonKey(name: 'total_results')
12+
final int totalResults;
13+
14+
MoviesResponse({
15+
required this.page,
16+
required this.results,
17+
required this.totalPages,
18+
required this.totalResults,
19+
});
20+
21+
factory MoviesResponse.fromJson(Map<String, dynamic> json) =>
22+
_$MoviesResponseFromJson(json);
23+
24+
Map<String, dynamic> toJson() => _$MoviesResponseToJson(this);
25+
}
26+
27+
@JsonSerializable()
28+
class Movie {
29+
final int id;
30+
final String title;
31+
@JsonKey(name: 'poster_path')
32+
final String? posterPath;
33+
@JsonKey(name: 'backdrop_path')
34+
final String? backdropPath;
35+
@JsonKey(name: 'release_date')
36+
final String? releaseDate;
37+
@JsonKey(name: 'vote_average')
38+
final double voteAverage;
39+
final String overview;
40+
41+
Movie({
42+
required this.id,
43+
required this.title,
44+
this.posterPath,
45+
this.backdropPath,
46+
this.releaseDate,
47+
required this.voteAverage,
48+
required this.overview,
49+
});
50+
51+
factory Movie.fromJson(Map<String, dynamic> json) => _$MovieFromJson(json);
52+
53+
Map<String, dynamic> toJson() => _$MovieToJson(this);
54+
}
55+
56+
@JsonSerializable()
57+
class MovieDetailsResponse extends Movie {
58+
final List<Genre> genres;
59+
final int runtime;
60+
@JsonKey(name: 'production_companies')
61+
final List<ProductionCompany> productionCompanies;
62+
63+
MovieDetailsResponse({
64+
required super.id,
65+
required super.title,
66+
super.posterPath,
67+
super.backdropPath,
68+
super.releaseDate,
69+
required super.voteAverage,
70+
required super.overview,
71+
required this.genres,
72+
required this.runtime,
73+
required this.productionCompanies,
74+
});
75+
76+
factory MovieDetailsResponse.fromJson(Map<String, dynamic> json) =>
77+
_$MovieDetailsResponseFromJson(json);
78+
79+
@override
80+
Map<String, dynamic> toJson() => _$MovieDetailsResponseToJson(this);
81+
}
82+
83+
@JsonSerializable()
84+
class Genre {
85+
final int id;
86+
final String name;
87+
88+
Genre({required this.id, required this.name});
89+
90+
factory Genre.fromJson(Map<String, dynamic> json) => _$GenreFromJson(json);
91+
92+
Map<String, dynamic> toJson() => _$GenreToJson(this);
93+
}
94+
95+
@JsonSerializable()
96+
class ProductionCompany {
97+
final int id;
98+
final String name;
99+
@JsonKey(name: 'logo_path')
100+
final String? logoPath;
101+
@JsonKey(name: 'origin_country')
102+
final String originCountry;
103+
104+
ProductionCompany({
105+
required this.id,
106+
required this.name,
107+
this.logoPath,
108+
required this.originCountry,
109+
});
110+
111+
factory ProductionCompany.fromJson(Map<String, dynamic> json) =>
112+
_$ProductionCompanyFromJson(json);
113+
114+
Map<String, dynamic> toJson() => _$ProductionCompanyToJson(this);
115+
}
116+
117+
@JsonSerializable()
118+
class GenresResponse {
119+
final List<Genre> genres;
120+
121+
GenresResponse({required this.genres});
122+
123+
factory GenresResponse.fromJson(Map<String, dynamic> json) =>
124+
_$GenresResponseFromJson(json);
125+
126+
Map<String, dynamic> toJson() => _$GenresResponseToJson(this);
127+
}
128+
129+
@JsonSerializable()
130+
class CreditsResponse {
131+
final List<Cast> cast;
132+
final List<Crew> crew;
133+
134+
CreditsResponse({required this.cast, required this.crew});
135+
136+
factory CreditsResponse.fromJson(Map<String, dynamic> json) =>
137+
_$CreditsResponseFromJson(json);
138+
139+
Map<String, dynamic> toJson() => _$CreditsResponseToJson(this);
140+
}
141+
142+
@JsonSerializable()
143+
class Cast {
144+
final int id;
145+
final String name;
146+
final String character;
147+
@JsonKey(name: 'profile_path')
148+
final String? profilePath;
149+
150+
Cast({
151+
required this.id,
152+
required this.name,
153+
required this.character,
154+
this.profilePath,
155+
});
156+
157+
factory Cast.fromJson(Map<String, dynamic> json) => _$CastFromJson(json);
158+
159+
Map<String, dynamic> toJson() => _$CastToJson(this);
160+
}
161+
162+
@JsonSerializable()
163+
class Crew {
164+
final int id;
165+
final String name;
166+
final String department;
167+
final String job;
168+
@JsonKey(name: 'profile_path')
169+
final String? profilePath;
170+
171+
Crew({
172+
required this.id,
173+
required this.name,
174+
required this.department,
175+
required this.job,
176+
this.profilePath,
177+
});
178+
179+
factory Crew.fromJson(Map<String, dynamic> json) => _$CrewFromJson(json);
180+
181+
Map<String, dynamic> toJson() => _$CrewToJson(this);
182+
}

lib/app/api/movie_api.dart

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import 'package:dio/dio.dart';
2+
import 'models/movie_responses.dart';
3+
4+
class MovieApi {
5+
final Dio _dio;
6+
static const String _baseUrl = 'https://api.themoviedb.org/3/';
7+
8+
MovieApi({Dio? dio}) : _dio = dio ?? Dio() {
9+
_dio.options.baseUrl = _baseUrl;
10+
}
11+
12+
static const int STARTING_PAGE_INDEX = 1;
13+
static const String LANGUAGE = "en-US";
14+
static const String API_KEY = ""; // Replace with your actual API key
15+
16+
Future<MoviesResponse> getTrendingTodayMovies({
17+
int page = STARTING_PAGE_INDEX,
18+
String apiKey = API_KEY,
19+
String language = LANGUAGE,
20+
}) async {
21+
final response = await _dio.get(
22+
'trending/movie/day',
23+
queryParameters: {
24+
'page': page,
25+
'api_key': apiKey,
26+
'language': language,
27+
},
28+
);
29+
return MoviesResponse.fromJson(response.data);
30+
}
31+
32+
Future<MoviesResponse> getPopularMovies({
33+
int page = STARTING_PAGE_INDEX,
34+
String apiKey = API_KEY,
35+
String language = LANGUAGE,
36+
}) async {
37+
final response = await _dio.get(
38+
'movie/popular',
39+
queryParameters: {
40+
'page': page,
41+
'api_key': apiKey,
42+
'language': language,
43+
},
44+
);
45+
return MoviesResponse.fromJson(response.data);
46+
}
47+
48+
Future<MoviesResponse> getUpcomingMovies({
49+
int page = STARTING_PAGE_INDEX,
50+
String apiKey = API_KEY,
51+
String language = LANGUAGE,
52+
}) async {
53+
final response = await _dio.get(
54+
'movie/upcoming',
55+
queryParameters: {
56+
'page': page,
57+
'api_key': apiKey,
58+
'language': language,
59+
},
60+
);
61+
return MoviesResponse.fromJson(response.data);
62+
}
63+
64+
Future<MoviesResponse> getTopRatedMovies({
65+
int page = STARTING_PAGE_INDEX,
66+
String apiKey = API_KEY,
67+
String language = LANGUAGE,
68+
}) async {
69+
final response = await _dio.get(
70+
'movie/top_rated',
71+
queryParameters: {
72+
'page': page,
73+
'api_key': apiKey,
74+
'language': language,
75+
},
76+
);
77+
return MoviesResponse.fromJson(response.data);
78+
}
79+
80+
Future<MoviesResponse> getNowPlayingMovies({
81+
int page = STARTING_PAGE_INDEX,
82+
String apiKey = API_KEY,
83+
String language = LANGUAGE,
84+
}) async {
85+
final response = await _dio.get(
86+
'movie/now_playing',
87+
queryParameters: {
88+
'page': page,
89+
'api_key': apiKey,
90+
'language': language,
91+
},
92+
);
93+
return MoviesResponse.fromJson(response.data);
94+
}
95+
96+
Future<GenresResponse> getMovieGenres({
97+
String apiKey = API_KEY,
98+
String language = LANGUAGE,
99+
}) async {
100+
final response = await _dio.get(
101+
'genre/movie/list',
102+
queryParameters: {
103+
'api_key': apiKey,
104+
'language': language,
105+
},
106+
);
107+
return GenresResponse.fromJson(response.data);
108+
}
109+
110+
Future<MovieDetailsResponse> getMovieDetails({
111+
required int movieId,
112+
String apiKey = API_KEY,
113+
String language = LANGUAGE,
114+
}) async {
115+
final response = await _dio.get(
116+
'movie/$movieId',
117+
queryParameters: {
118+
'api_key': apiKey,
119+
'language': language,
120+
},
121+
);
122+
return MovieDetailsResponse.fromJson(response.data);
123+
}
124+
125+
Future<CreditsResponse> getCast({
126+
required int movieId,
127+
String apiKey = API_KEY,
128+
}) async {
129+
final response = await _dio.get(
130+
'movie/$movieId/credits',
131+
queryParameters: {
132+
'api_key': apiKey,
133+
},
134+
);
135+
return CreditsResponse.fromJson(response.data);
136+
}
137+
138+
Future<MoviesResponse> getRecommendations({
139+
required int movieId,
140+
int page = STARTING_PAGE_INDEX,
141+
String apiKey = API_KEY,
142+
String language = LANGUAGE,
143+
}) async {
144+
final response = await _dio.get(
145+
'movie/$movieId/recommendations',
146+
queryParameters: {
147+
'page': page,
148+
'api_key': apiKey,
149+
'language': language,
150+
},
151+
);
152+
return MoviesResponse.fromJson(response.data);
153+
}
154+
155+
Future<MoviesResponse> searchMovie({
156+
required String query,
157+
int page = STARTING_PAGE_INDEX,
158+
String apiKey = API_KEY,
159+
}) async {
160+
final response = await _dio.get(
161+
'search/movie',
162+
queryParameters: {
163+
'query': query,
164+
'page': page,
165+
'api_key': apiKey,
166+
},
167+
);
168+
return MoviesResponse.fromJson(response.data);
169+
}
170+
}

0 commit comments

Comments
 (0)