diff --git a/src/main/java/fr/packageviewer/distribution/ArchDistribution.java b/src/main/java/fr/packageviewer/distribution/ArchDistribution.java index 7f560e8..4d8585b 100644 --- a/src/main/java/fr/packageviewer/distribution/ArchDistribution.java +++ b/src/main/java/fr/packageviewer/distribution/ArchDistribution.java @@ -19,84 +19,83 @@ import org.json.*; * This class handles package requests for Arch linux. All return objects in * this class are wrapped by a CompletableFuture to ensure async workload. * - * @author C.Marla, R.Thomas - * @version 1.0 + * @author C.Marla, R.Thomas + * @version 1.0 */ public class ArchDistribution extends AsyncRequestsParser implements Distribution { private static final Logger logger = LoggerManager.getLogger("ArchDistribution"); -/** - * This function return a package from arch package api in the form of a Pair - * Composed of a Package object, and a set of string containing the names of - * the dependecies of the package. - * - * @param packageName String, The package's exact name - * @return Pair of Package and Set of String - */ + /** + * This function return a package from arch package api in the form of a Pair + * Composed of a Package object, and a set of string containing the names of + * the dependecies of the package. + * + * @param packageName String, The package's exact name + * @return Pair of Package and Set of String + */ -@Override -public CompletableFuture>> getPackageFromAPI(String packageName) { - // create a new http client - HttpClient client = HttpClient.newHttpClient(); - // and create its url - HttpRequest request = HttpRequest.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name="+packageName)).build(); + @Override + public CompletableFuture>> getPackageFromAPI(String packageName) { + // create a new http client + HttpClient client = HttpClient.newHttpClient(); + // and create its url + HttpRequest request = HttpRequest + .newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build(); - CompletableFuture>> futureResult = new CompletableFuture<>(); - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result ->{ + CompletableFuture>> futureResult = new CompletableFuture<>(); + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { - JSONObject json = new JSONObject(result.body()); + JSONObject json = new JSONObject(result.body()); - JSONArray resultsArrayJson = json.getJSONArray("results"); - if(resultsArrayJson.length()==0){ - // unknown package, probably an abstract dependency + JSONArray resultsArrayJson = json.getJSONArray("results"); + if (resultsArrayJson.length() == 0) { + // unknown package, probably an abstract dependency + futureResult.complete(null); + return; + } + JSONObject resultJson = resultsArrayJson.getJSONObject(0); + + // get infos + + Set dependenciesNames = new HashSet<>(); + for (Object dependency : resultJson.getJSONArray("depends")) { + dependenciesNames.add((String) dependency); + } + futureResult.complete(new Pair<>( + new Package( + resultJson.getString("pkgname"), + resultJson.getString("pkgver"), + resultJson.getString("repo"), + resultJson.getString("pkgdesc")), + dependenciesNames)); + }).exceptionally(error -> { + error.printStackTrace(); + logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); futureResult.complete(null); - return; - } - JSONObject resultJson = resultsArrayJson.getJSONObject(0); + return null; + }); - // get infos + return futureResult; - Set dependenciesNames = new HashSet<>(); - for(Object dependency : resultJson.getJSONArray("depends")){ - dependenciesNames.add((String)dependency); - } - futureResult.complete(new Pair<>( - new Package( - resultJson.getString("pkgname"), - resultJson.getString("pkgver"), - resultJson.getString("repo"), - resultJson.getString("pkgdesc") - ), - dependenciesNames - )); - }).exceptionally(error ->{ - error.printStackTrace(); - logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); - futureResult.complete(null); - return null; - }); + } - return futureResult; - -} - -/** - * Search for a package matching a pattern and return a list of packages and - * return a list of string matching this pattern. - * - * @param packageName String, the pattern to search in the repositories - * @return List of SearchedPackage objects - */ - public CompletableFuture> searchPackage(String packageName){ + /** + * Search for a package matching a pattern and return a list of packages and + * return a list of string matching this pattern. + * + * @param packageName String, the pattern to search in the repositories + * @return List of SearchedPackage objects + */ + public CompletableFuture> searchPackage(String packageName) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://archlinux.org/packages/search/json/?q="+packageName)) + .uri(URI.create("https://archlinux.org/packages/search/json/?q=" + packageName)) .build(); CompletableFuture> futureSearchedPackages = new CompletableFuture<>(); - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result->{ + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { List searchedPackagesList = new ArrayList<>(); JSONObject json = new JSONObject(result.body()); @@ -107,21 +106,18 @@ public CompletableFuture>> getPackageFromAPI(String pa JSONObject searchResultJson = (JSONObject) searchResultObj; // add package into to list searchedPackagesList.add(new SearchedPackage( - searchResultJson.getString("pkgname"), - searchResultJson.getString("pkgver"), - searchResultJson.getString("repo"), - searchResultJson.getString("pkgdesc") - )); + searchResultJson.getString("pkgname"), + searchResultJson.getString("pkgver"), + searchResultJson.getString("repo"), + searchResultJson.getString("pkgdesc"))); } - futureSearchedPackages.complete(searchedPackagesList); - }).exceptionally(error->{ + futureSearchedPackages.complete(searchedPackagesList); + }).exceptionally(error -> { error.printStackTrace(); futureSearchedPackages.complete(Collections.emptyList()); return null; }); - - return futureSearchedPackages; }