refactored for async use
This commit is contained in:
parent
2252063176
commit
c8eae1fb06
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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,11 +32,13 @@ 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();
|
||||||
@ -59,14 +67,14 @@ 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<>();
|
||||||
|
|
||||||
@ -85,19 +93,26 @@ public class FedoraParser {
|
|||||||
// 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user