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.time.Duration;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.ScheduledExecutorService;
  12. import java.util.concurrent.TimeUnit;
  13. public class ApiMetadataAttacher {
  14. private static final int METADATA_CHECK_INTERVAL = 60; // seconds
  15. private static final ConcurrentHashMap<String, Metadata> apiMetadataCache = new ConcurrentHashMap<>();
  16. private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  17. static {
  18. scheduler.scheduleAtFixedRate(this::checkApiMetadata, 0, METADATA_CHECK_INTERVAL, TimeUnit.SECONDS);
  19. }
  20. public static void attachMetadata(String apiUrl) {
  21. // Validate API URL
  22. URI uri = URI.create(apiUrl);
  23. if (!uri.isAbsolute()) {
  24. throw new IllegalArgumentException("API URL must be absolute.");
  25. }
  26. // Create an HTTP request
  27. HttpRequest request = HttpRequest.newBuilder()
  28. .method("GET")
  29. .uri(uri)
  30. .build();
  31. // Use HttpClient to make the request
  32. HttpClient client = HttpClient.newHttpClient();
  33. try {
  34. HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  35. if (response.statusCode() >= 200 && response.statusCode() < 300) {
  36. // Process the response body to extract metadata
  37. Metadata metadata = extractMetadata(response.body());
  38. apiMetadataCache.put(apiUrl, metadata);
  39. System.out.println("Metadata attached for: " + apiUrl);
  40. } else {
  41. System.err.println("API request failed for: " + apiUrl + " - Status Code: " + response.statusCode());
  42. }
  43. } catch (IOException e) {
  44. System.err.println("IOException while fetching metadata for: " + apiUrl + ": " + e.getMessage());
  45. } catch (InterruptedException e) {
  46. Thread.currentThread().interrupt();
  47. } catch (HttpResponseException e) {
  48. System.err.println("HttpResponseException while fetching metadata for: " + apiUrl + ": " + e.getMessage());
  49. }
  50. }
  51. private static Metadata extractMetadata(String responseBody) {
  52. // Implement your metadata extraction logic here.
  53. // This is a placeholder. Replace with your API's metadata format.
  54. Metadata metadata = new Metadata();
  55. metadata.lastUpdated = System.currentTimeMillis();
  56. metadata.statusCode = -1; // Placeholder. Implement actual parsing.
  57. metadata.headers = new HashMap<>(); //Placeholder. Implement actual parsing.
  58. return metadata;
  59. }
  60. public static Metadata getMetadata(String apiUrl) {
  61. return apiMetadataCache.get(apiUrl);
  62. }
  63. private static void checkApiMetadata() {
  64. // Periodically check and update metadata for all known APIs.
  65. for (Map.Entry<String, Metadata> entry : apiMetadataCache.entrySet()) {
  66. String apiUrl = entry.getKey();
  67. Metadata metadata = entry.getValue();
  68. try {
  69. // Re-fetch metadata. This can catch changes.
  70. attachMetadata(apiUrl);
  71. } catch (Exception e) {
  72. System.err.println("Error checking metadata for " + apiUrl + ": " + e.getMessage());
  73. }
  74. }
  75. }
  76. // Simple Metadata class
  77. public static class Metadata {
  78. public long lastUpdated;
  79. public int statusCode;
  80. public Map<String, String> headers;
  81. }
  82. public static void main(String[] args) throws InterruptedException {
  83. // Example Usage
  84. attachMetadata("https://api.example.com/data");
  85. attachMetadata("https://api.example.com/users");
  86. // ... more APIs
  87. Thread.sleep(10000); //Keep the program running for a while.
  88. }
  89. }

Add your comment