detailed a bit more the commentaries

This commit is contained in:
Djalim Simaila 2022-12-15 15:10:07 +01:00
parent 6527a155fe
commit 1cb1229d88
4 changed files with 19 additions and 11 deletions

View File

@ -33,12 +33,15 @@ public class LoggerManager {
* @return Logger, a new logger
*/
public static Logger getLogger(String name, Level level) {
// Create a logger and set its level
Logger logger = Logger.getLogger(name);
logger.setLevel(level);
// create an hanlder for standard error and add it to the logger
Handler handler = new StreamHandler(System.err, new SimpleFormatter());
logger.addHandler(handler);
logger.setUseParentHandlers(false);
return logger;
}

View File

@ -57,17 +57,18 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
*/
@Override
public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client
// create a new http client and he request for arch reseach api
HttpClient client = HttpClient.newHttpClient();
// and create its url
HttpRequest request = HttpRequest
.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build();
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
// send the request and when there's a response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
//parse the json response
JSONObject json = new JSONObject(result.body());
// check if the response contains something
JSONArray resultsArrayJson = json.getJSONArray("results");
if (resultsArrayJson.length() == 0) {
// unknown package, probably an abstract dependency
@ -75,13 +76,12 @@ public class ArchDistribution extends AsyncRequestsParser implements Distributio
return;
}
JSONObject resultJson = resultsArrayJson.getJSONObject(0);
// get infos
Set<String> dependenciesNames = new HashSet<>();
// parse depencies without version requirements (bash >= 3.0) -> (bash)
for (Object dependency : resultJson.getJSONArray("depends")) {
dependenciesNames.add(trimAfterCharacters((String) dependency, "<>="));
}
//Create the package and store it and its set of deps in a pair
futureResult.complete(new Pair<>(
new Package(
resultJson.getString("pkgname"),

View File

@ -99,7 +99,7 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
*/
@Override
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
// create an http client and the request for fedora's reseach api
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(
@ -109,7 +109,9 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
// send the request and when there's a response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
//parse the json response
JSONObject json = new JSONObject(result.body());
List<SearchedPackage> searchedPackagesList = new ArrayList<>();

View File

@ -48,7 +48,7 @@ public abstract class AsyncRequestsParser {
* @return Package, the fully completed package
*/
public CompletableFuture<Package> getPackageTree(String packageName, int depth) {
// parse the json
//Wrapper for the package that we'll return
var futurePackage = new CompletableFuture<Package>();
logger.fine("Querying package %s from API... (depth=%s)".formatted(packageName, depth));
@ -57,11 +57,14 @@ public abstract class AsyncRequestsParser {
try {
futureRequest = getPackageFromAPI(packageName);
} catch (IllegalArgumentException e) {
// If we can't get the package from the api return a null object
logger.warning("Caught exception for package %s :\n%s".formatted(packageName, e));
return CompletableFuture.completedFuture(null);
}
// When we get the response from the request
futureRequest.thenAccept(result -> {
if (result == null) {
// if there's no response return null object
logger.fine("Completing callback INVALID for package %s (depth=%s)".formatted(packageName, depth));
futurePackage.complete(null);
return;
@ -83,6 +86,8 @@ public abstract class AsyncRequestsParser {
// add package into Package List
futureDeps.add(getPackageTree(depPackageName, depth - 1));
}
// for each future in the list, get the actual package and store
// into the deps list of the packaqge
for (CompletableFuture<Package> future : futureDeps) {
Package dep;
try {
@ -94,8 +99,6 @@ public abstract class AsyncRequestsParser {
pack.addDep(dep);
}
}
// TODO this doesn't seem clean
logger.fine("Completing callback DEPS for package %s (depth=%s)".formatted(packageName, depth));
futurePackage.complete(pack);
}).exceptionally(error -> {