|
|
|
@@ -0,0 +1,70 @@
|
|
|
|
|
package movie.apis;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.http.HttpClient;
|
|
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
|
import java.net.http.HttpResponse;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
public class MovieService {
|
|
|
|
|
|
|
|
|
|
private final static String GENRES_URL = "https://moviesapi.ir/api/v1/genres";
|
|
|
|
|
private static String MOVIES_BY_GENRE_URL = "https://moviesapi.ir/api/v1/genres/";
|
|
|
|
|
private final static String MOVIE_INFO = "https://moviesapi.ir/api/v1/movies/{movie_id}";//todo fazel
|
|
|
|
|
private final static HttpClient client = HttpClient.newHttpClient();
|
|
|
|
|
private ArrayList<Movie> moviesList;
|
|
|
|
|
private ArrayList<Genre> genresList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getGenresList() {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
.uri(URI.create(GENRES_URL))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
|
|
|
|
|
if (response.statusCode() == 200) {
|
|
|
|
|
System.out.println("Genres List received successfully");
|
|
|
|
|
return response.body();
|
|
|
|
|
} else {
|
|
|
|
|
throw new IOException("HTTP error code: " + response.statusCode());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("!!Exception : " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getMoviesByGenreList(Long genreId, Long page) {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
MOVIES_BY_GENRE_URL = MOVIES_BY_GENRE_URL + genreId + "/movies?page=" + page;
|
|
|
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
.uri(URI.create(MOVIES_BY_GENRE_URL))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
|
|
|
|
|
if (response.statusCode() == 200) {
|
|
|
|
|
System.out.println("Movies By Genre received successfully");
|
|
|
|
|
return response.body();
|
|
|
|
|
} else {
|
|
|
|
|
throw new IOException("HTTP error code: " + response.statusCode());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("!!Exception : " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Write a method that parses the JSON returned by getGenresList() into a list of Genre objects.
|
|
|
|
|
// TODO: Write a method that displays the list of genres.
|
|
|
|
|
|
|
|
|
|
// TODO: Write a method whose inputs are genreId and page, and returns a list of movies for that genre and page.
|
|
|
|
|
// TODO: Write a method that displays the genres of a specific movie.
|
|
|
|
|
|
|
|
|
|
}
|