1. import java.io.IOException;
  2. import java.net.URI;
  3. import java.net.http.HttpClient;
  4. import java.net.http.HttpRequest;
  5. import java.net.http.HttpResponse;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. public class ApiIndexer {
  9. /**
  10. * Indexes content from API endpoints with optional flags.
  11. * @param baseUrl The base URL of the API.
  12. * @param flags A map of flags to values.
  13. * @return A map containing the indexed data. Returns an empty map on error.
  14. */
  15. public static Map<String, String> indexApiContent(String baseUrl, Map<String, String> flags) {
  16. Map<String, String> indexedData = new HashMap<>();
  17. try {
  18. URI uri = new URI(baseUrl);
  19. HttpRequest request = buildRequest(uri, flags);
  20. HttpClient client = HttpClient.newHttpClient();
  21. HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  22. if (response.statusCode() != 200) {
  23. System.err.println("API request failed with status code: " + response.statusCode());
  24. return indexedData; // Return empty map on error
  25. }
  26. String content = response.body();
  27. indexedData.put("content", content);
  28. } catch (IOException | Exception e) {
  29. System.err.println("An error occurred: " + e.getMessage());
  30. return indexedData; // Return empty map on error
  31. }
  32. return indexedData;
  33. }
  34. /**
  35. * Builds the HTTP request with optional flags.
  36. * @param uri The URI of the API endpoint.
  37. * @param flags A map of flags to values.
  38. * @return The HttpRequest object.
  39. */
  40. private static HttpRequest buildRequest(URI uri, Map<String, String> flags) throws Exception {
  41. StringBuilder sb = new StringBuilder(uri.toString());
  42. // Add query parameters
  43. if (flags != null && !flags.isEmpty()) {
  44. String queryParams = buildQueryParams(flags);
  45. sb.append("?").append(queryParams);
  46. }
  47. return HttpRequest.newBuilder()
  48. .uri(URI.create(sb.toString()))
  49. .build();
  50. }
  51. /**
  52. * Builds the query parameters string.
  53. * @param flags A map of flags to values.
  54. * @return The query parameters string.
  55. */
  56. private static String buildQueryParams(Map<String, String> flags) {
  57. StringBuilder queryParams = new StringBuilder();
  58. for (Map.Entry<String, String> entry : flags.entrySet()) {
  59. queryParams.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
  60. }
  61. // Remove trailing &
  62. if (queryParams.length() > 0) {
  63. queryParams.deleteCharAt(queryParams.length() - 1);
  64. }
  65. return queryParams.toString();
  66. }
  67. public static void main(String[] args) {
  68. // Example usage
  69. String baseUrl = "https://jsonplaceholder.typicode.com/todos";
  70. Map<String, String> flags = new HashMap<>();
  71. flags.put("limit", "10"); // Example flag
  72. flags.put("offset", "20");
  73. Map<String, String> indexedData = indexApiContent(baseUrl, flags);
  74. if (!indexedData.isEmpty()) {
  75. System.out.println("Indexed data:");
  76. System.out.println(indexedData);
  77. }
  78. }
  79. }

Add your comment