make searchPackage return a CompletableFuture
This commit is contained in:
		
							parent
							
								
									ca6e940cae
								
							
						
					
					
						commit
						2196a9006f
					
				| @ -71,44 +71,46 @@ public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String pa | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * Search for a package and return a list of packages | ||||
|  * @param packageName the package name to search | ||||
|  * @return | ||||
|  */ | ||||
|     public List<SearchedPackage> searchPackage(String packageName){ | ||||
|     public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName){ | ||||
|         HttpClient client = HttpClient.newHttpClient(); | ||||
|         HttpRequest request = HttpRequest.newBuilder() | ||||
|                 .uri(URI.create("https://archlinux.org/packages/search/json/?q="+packageName)) | ||||
|                 .build(); | ||||
| 
 | ||||
|         HttpResponse<String> response; | ||||
|         try{ | ||||
|             response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||||
|         }catch(IOException|InterruptedException e){ | ||||
|             e.printStackTrace(); | ||||
|         CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>(); | ||||
| 
 | ||||
|         client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result->{ | ||||
| 
 | ||||
|             List<SearchedPackage> searchedPackagesList = new ArrayList<>(); | ||||
|             JSONObject json = new JSONObject(result.body()); | ||||
| 
 | ||||
|             // iterate for every package in the list | ||||
|             for (Object searchResultObj : json.getJSONArray("results")) { | ||||
|                 // convert object into String | ||||
|                 JSONObject searchResultJson = (JSONObject) searchResultObj; | ||||
|                 // add package into to list | ||||
|                 searchedPackagesList.add(new SearchedPackage( | ||||
|                     searchResultJson.getString("pkgname"), | ||||
|                     searchResultJson.getString("pkgver"), | ||||
|                     searchResultJson.getString("repo"), | ||||
|                     searchResultJson.getString("pkgdesc") | ||||
|                 )); | ||||
|             } | ||||
|            futureSearchedPackages.complete(searchedPackagesList); | ||||
|         }).exceptionally(error->{ | ||||
|             error.printStackTrace(); | ||||
|             futureSearchedPackages.complete(Collections.emptyList()); | ||||
|             return null; | ||||
|         } | ||||
|         }); | ||||
| 
 | ||||
|         JSONObject json = new JSONObject(response.body()); | ||||
| 
 | ||||
|         List<SearchedPackage> searchedPackagesList = new ArrayList<>(); | ||||
| 
 | ||||
|         // iterate for every package in the list | ||||
|         for (Object searchResultObj : json.getJSONArray("results")) { | ||||
|             // convert object into String | ||||
|             JSONObject searchResultJson = (JSONObject) searchResultObj; | ||||
|             // add package into to list | ||||
|             searchedPackagesList.add(new SearchedPackage( | ||||
|                 searchResultJson.getString("pkgname"), | ||||
|                 searchResultJson.getString("pkgver"), | ||||
|                 searchResultJson.getString("repo"), | ||||
|                 searchResultJson.getString("pkgdesc") | ||||
|             )); | ||||
|         } | ||||
| 
 | ||||
|         return searchedPackagesList; | ||||
|         return futureSearchedPackages; | ||||
| 
 | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -7,6 +7,6 @@ import java.util.List; | ||||
| import java.util.concurrent.CompletableFuture; | ||||
| 
 | ||||
| public interface Distribution { | ||||
| 	List<SearchedPackage> searchPackage(String packageName); | ||||
| 	CompletableFuture<List<SearchedPackage>> searchPackage(String packageName); | ||||
| 	CompletableFuture<Package> getPackageTree(String packageName, int depth); | ||||
| } | ||||
|  | ||||
| @ -75,7 +75,7 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut | ||||
|     } | ||||
|      | ||||
|     @Override | ||||
|     public List<SearchedPackage> searchPackage(String packageName) { | ||||
|     public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) { | ||||
| 
 | ||||
|         HttpClient client = HttpClient.newHttpClient(); | ||||
|         HttpRequest request = HttpRequest.newBuilder() | ||||
| @ -84,29 +84,31 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut | ||||
|                                 + packageName + "*")) | ||||
|                 .build(); | ||||
| 
 | ||||
|         HttpResponse<String> response; | ||||
|         try { | ||||
|             response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||||
|         } catch (IOException | InterruptedException e) { | ||||
|             e.printStackTrace(); | ||||
|         CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>(); | ||||
| 
 | ||||
|         client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result->{ | ||||
|             JSONObject json = new JSONObject(result.body()); | ||||
| 
 | ||||
|             List<SearchedPackage> searchedPackagesList = new ArrayList<>(); | ||||
| 
 | ||||
|             // iterate for every package in the list | ||||
|             for (Object searchResultObj : json.getJSONArray("projects")) { | ||||
|                 // convert object into String | ||||
|                 JSONObject searchResultJson = (JSONObject) searchResultObj; | ||||
|                 // add package into to list | ||||
|                 searchedPackagesList.add(new SearchedPackage( | ||||
|                         searchResultJson.getString("neofetch"), | ||||
|                         null, | ||||
|                         searchResultJson.getString("fullname"), | ||||
|                         searchResultJson.getString("description"))); | ||||
|             } | ||||
|             futureSearchedPackages.complete(searchedPackagesList); | ||||
|         }).exceptionally(error->{ | ||||
|             error.printStackTrace(); | ||||
|             futureSearchedPackages.complete(Collections.emptyList()); | ||||
|             return null; | ||||
|         } | ||||
|         }); | ||||
| 
 | ||||
|         JSONObject json = new JSONObject(response.body()); | ||||
| 
 | ||||
|         List<SearchedPackage> searchedPackagesList = new ArrayList<>(); | ||||
| 
 | ||||
|         // iterate for every package in the list | ||||
|         for (Object searchResultObj : json.getJSONArray("projects")) { | ||||
|             // convert object into String | ||||
|             JSONObject searchResultJson = (JSONObject) searchResultObj; | ||||
|             // add package into to list | ||||
|             searchedPackagesList.add(new SearchedPackage( | ||||
|                     searchResultJson.getString("neofetch"), | ||||
|                     null, | ||||
|                     searchResultJson.getString("fullname"), | ||||
|                     searchResultJson.getString("description"))); | ||||
|         } | ||||
|         return searchedPackagesList; | ||||
|         return futureSearchedPackages; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -67,14 +67,14 @@ public class ArchTest { | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void testThatBashSearchReturnsResults() { | ||||
| 	public void testThatBashSearchReturnsResults() throws ExecutionException, InterruptedException { | ||||
| 		Distribution arch = new ArchDistribution(); | ||||
| 		Assertions.assertNotEquals(arch.searchPackage("bash").size(), 0); | ||||
| 		Assertions.assertNotEquals(arch.searchPackage("bash").get().size(), 0); | ||||
| 	} | ||||
| 	@Test | ||||
| 	public void testThatBashSearchContainsBash() { | ||||
| 	public void testThatBashSearchContainsBash() throws ExecutionException, InterruptedException { | ||||
| 		Distribution arch = new ArchDistribution(); | ||||
| 		for(SearchedPackage pack : arch.searchPackage("bash")){ | ||||
| 		for(SearchedPackage pack : arch.searchPackage("bash").get()){ | ||||
| 			if(pack.getName().equals("bash")){ | ||||
| 				Assertions.assertTrue(true); | ||||
| 				return; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user