fixed indentation

This commit is contained in:
Djalim Simaila 2022-12-14 12:05:00 +01:00
parent 279ad63891
commit a97aa19fef

View File

@ -26,7 +26,7 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
private static final Logger logger = LoggerManager.getLogger("ArchDistribution"); private static final Logger logger = LoggerManager.getLogger("ArchDistribution");
/** /**
* This function return a package from arch package api in the form of a Pair * 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 * Composed of a Package object, and a set of string containing the names of
* the dependecies of the package. * the dependecies of the package.
@ -35,20 +35,21 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
* @return Pair of Package and Set of String * @return Pair of Package and Set of String
*/ */
@Override @Override
public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) { public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client // create a new http client
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
// and create its url // and create its url
HttpRequest request = HttpRequest.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name="+packageName)).build(); HttpRequest request = HttpRequest
.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build();
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>(); CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result ->{ 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"); JSONArray resultsArrayJson = json.getJSONArray("results");
if(resultsArrayJson.length()==0){ if (resultsArrayJson.length() == 0) {
// unknown package, probably an abstract dependency // unknown package, probably an abstract dependency
futureResult.complete(null); futureResult.complete(null);
return; return;
@ -58,19 +59,17 @@ public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String pa
// get infos // get infos
Set<String> dependenciesNames = new HashSet<>(); Set<String> dependenciesNames = new HashSet<>();
for(Object dependency : resultJson.getJSONArray("depends")){ for (Object dependency : resultJson.getJSONArray("depends")) {
dependenciesNames.add((String)dependency); dependenciesNames.add((String) dependency);
} }
futureResult.complete(new Pair<>( futureResult.complete(new Pair<>(
new Package( new Package(
resultJson.getString("pkgname"), resultJson.getString("pkgname"),
resultJson.getString("pkgver"), resultJson.getString("pkgver"),
resultJson.getString("repo"), resultJson.getString("repo"),
resultJson.getString("pkgdesc") resultJson.getString("pkgdesc")),
), dependenciesNames));
dependenciesNames }).exceptionally(error -> {
));
}).exceptionally(error ->{
error.printStackTrace(); error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null); futureResult.complete(null);
@ -79,24 +78,24 @@ public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String pa
return futureResult; return futureResult;
} }
/** /**
* Search for a package matching a pattern and return a list of packages and * Search for a package matching a pattern and return a list of packages and
* return a list of string matching this pattern. * return a list of string matching this pattern.
* *
* @param packageName String, the pattern to search in the repositories * @param packageName String, the pattern to search in the repositories
* @return List of SearchedPackage objects * @return List of SearchedPackage objects
*/ */
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName){ public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() 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(); .build();
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>(); CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result->{ client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
List<SearchedPackage> searchedPackagesList = new ArrayList<>(); List<SearchedPackage> searchedPackagesList = new ArrayList<>();
JSONObject json = new JSONObject(result.body()); JSONObject json = new JSONObject(result.body());
@ -110,18 +109,15 @@ public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String pa
searchResultJson.getString("pkgname"), searchResultJson.getString("pkgname"),
searchResultJson.getString("pkgver"), searchResultJson.getString("pkgver"),
searchResultJson.getString("repo"), searchResultJson.getString("repo"),
searchResultJson.getString("pkgdesc") searchResultJson.getString("pkgdesc")));
));
} }
futureSearchedPackages.complete(searchedPackagesList); futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error->{ }).exceptionally(error -> {
error.printStackTrace(); error.printStackTrace();
futureSearchedPackages.complete(Collections.emptyList()); futureSearchedPackages.complete(Collections.emptyList());
return null; return null;
}); });
return futureSearchedPackages; return futureSearchedPackages;
} }