finished documentation for class FedoraDistribution and fixed formating

This commit is contained in:
Djalim Simaila 2022-12-14 14:22:33 +01:00
parent 052ba40800
commit 6db7bc4b31

View File

@ -17,23 +17,41 @@ import fr.packageviewer.LoggerManager;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
/**
* This class handles package requests for Fedora. All return objects in
* this class are wrapped by a CompletableFuture to ensure async workload.
*
* @author S.Djalim, R.Thomas
* @version 1.0
*/
public class FedoraDistribution extends AsyncRequestsParser implements Distribution {
/**
* Logger object used to split debug output and the application output
*/
private static final Logger logger = LoggerManager.getLogger("FedoraDistribution");
/**
* This function return a package from Fedora metadata 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
*/
protected CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client
HttpClient client = HttpClient.newHttpClient();
// and create its url
String url = "https://mdapi.fedoraproject.org/rawhide/pkg/"+packageName+"";
String url = "https://mdapi.fedoraproject.org/rawhide/pkg/" + packageName + "";
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result->{
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
String body = result.body();
if(body.contains("404: Not Found")) {
if (body.contains("404: Not Found")) {
futureResult.complete(null);
return;
}
@ -60,11 +78,9 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
json.getString("basename"),
json.getString("version"),
json.getString("repo"),
json.getString("description")
),
dependenciesNames
));
}).exceptionally(error->{
json.getString("description")),
dependenciesNames));
}).exceptionally(error -> {
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
@ -74,6 +90,13 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
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
*/
@Override
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
@ -86,7 +109,7 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result->{
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
JSONObject json = new JSONObject(result.body());
List<SearchedPackage> searchedPackagesList = new ArrayList<>();
@ -103,7 +126,7 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
searchResultJson.getString("description")));
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error->{
}).exceptionally(error -> {
error.printStackTrace();
futureSearchedPackages.complete(Collections.emptyList());
return null;