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