refactored for async use

This commit is contained in:
Djalim Simaila 2022-12-13 15:59:40 +01:00
parent 2252063176
commit c8eae1fb06
3 changed files with 31 additions and 85 deletions

View File

@ -1,31 +0,0 @@
package fr.packageviewer.FedoraParser;
import java.util.List;
public class Package extends SearchedPackage {
String version;
public List<Package> deps;
public Package(String name, String version, String repo, String description, List<Package> deps) {
super(name, repo, description);
this.version = version;
this.deps = deps;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public List<Package> getDeps() {
return deps;
}
public void setDeps(List<Package> deps) {
this.deps = deps;
}
}

View File

@ -1,38 +0,0 @@
package fr.packageviewer.FedoraParser;
public class SearchedPackage {
String name;
String repo;
String description;
public SearchedPackage(String name, String repo, String desciption) {
this.name = name;
this.repo = repo;
this.description = desciption;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRepo() {
return repo;
}
public void setRepo(String repo) {
this.repo = repo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -1,4 +1,4 @@
package fr.packageviewer.FedoraParser; package fr.packageviewer.distribution;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -10,10 +10,16 @@ import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.net.http.*; import java.net.http.*;
import org.json.*; import org.json.*;
import java.util.concurrent.CompletableFuture;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
import fr.packageviewer.LoggerManager;
import fr.packageviewer.pack.Package;
import fr.packageviewer.pack.SearchedPackage;
public class FedoraParser { public class FedoraDistribution implements Distribution {
public String getPackageFromAPI(String packageName) { private 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
@ -26,18 +32,20 @@ public class FedoraParser {
return response; return response;
} catch (IOException|InterruptedException e) { } catch (IOException|InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
// if there's an error, return an empty string // if there's an error, return an empty string
return ""; return "";
} }
@Override
public List<SearchedPackage> searchPackage(String packageName) { public List<SearchedPackage> searchPackage(String packageName) {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create( .uri(URI.create(
"https://src.fedoraproject.org/api/0/projects?namepace=rpms&per_page=100&short=true&pattern=*" "https://src.fedoraproject.org/api/0/projects?namepace=rpms&per_page=100&short=true&pattern=*"
+ packageName+"*")) + packageName + "*"))
.build(); .build();
HttpResponse<String> response; HttpResponse<String> response;
@ -59,45 +67,52 @@ public class FedoraParser {
// add package into to list // add package into to list
searchedPackagesList.add(new SearchedPackage( searchedPackagesList.add(new SearchedPackage(
searchResultJson.getString("neofetch"), searchResultJson.getString("neofetch"),
null,
searchResultJson.getString("fullname"), searchResultJson.getString("fullname"),
searchResultJson.getString("description"))); searchResultJson.getString("description")));
} }
return searchedPackagesList; return searchedPackagesList;
} }
public Package getPackageTreeInternal(String packageName, int depth) {
public Package getPackageTree(String packageName, int depth) {
String name, version, repo, description; String name, version, repo, description;
List<Package> deps = new ArrayList<>(); List<Package> deps = new ArrayList<>();
// parse the json // parse the json
String response = getPackageFromAPI(packageName); String response = getPackageFromAPI(packageName);
if(response == ""){ if (response == "") {
return new Package(packageName+"(not found)", "N/A", "N/A", "N/A", Collections.emptyList()); return new Package(packageName + "(not found)", "N/A", "N/A", "N/A", Collections.emptyList());
} }
JSONObject json = new JSONObject(response); JSONObject json = new JSONObject(response);
// get infos except dependencies // get infos except dependencies
name = json.getString("basename"); name = json.getString("basename");
version = json.getString("version"); version = json.getString("version");
repo = "rpms/"+packageName; repo = "rpms/" + packageName;
description = json.getString("description"); description = json.getString("description");
// if we're at the maximum depth, return the package without its dependencies // if we're at the maximum depth, return the package without its dependencies
if(depth==0){ if (depth == 0) {
return new Package(name, version, repo, description, Collections.emptyList()); return new Package(name, version, repo, description, Collections.emptyList());
} } else {
else {
// iterate for every package in the list // iterate for every package in the list
for (Object depPackageNameObj : json.getJSONArray("requires")) { for (Object depPackageNameObj : json.getJSONArray("requires")) {
// convert object into String // convert object into String
JSONObject depPackageJSONObj = (JSONObject) depPackageNameObj; JSONObject depPackageJSONObj = (JSONObject) depPackageNameObj;
// add package into Package List // add package into Package List
String depName = depPackageJSONObj.getString("name"); String depName = depPackageJSONObj.getString("name");
if(depName.contains(".so")) continue; if (depName.contains(".so"))
if(depName.contains("/")) continue; continue;
if (depName.contains("/"))
continue;
deps.add(getPackageTree(depName, depth - 1)); deps.add(getPackageTree(depName, depth - 1));
} }
return new Package(name, version, repo, description, deps); return new Package(name, version, repo, description, deps);
} }
} }
public CompletableFuture<Package> getPackageTree(String packageName, int depth){
return CompletableFuture.supplyAsync(()->{
return getPackageTreeInternal(packageName, depth);
});
}
} }